다소 큰 코드에 문제가 있습니다. 자신을 아는 것은 일종의 어리석은 실수이거나 포인터에 대한 이해 부족입니다. 정말 도움이 필요해 누군가가 그것을 볼 수 있다면 정말 감사 할 것입니다! 나는 그것을 지금 설명 할 것이다.C - 구조의 동적 배열
내 프로그래밍 클래스 용 프로그램입니다. 선생님은 txt 파일에 숫자 (N)와 문자 (X)를 입력하고 세 개의 필드 (int, char 및 float)와 네 개의 함수가있는 구조를 만들고 싶습니다.
function # 1 숫자 N을 인수로 취하여 N 구조에 대한 포인터 배열에 대한 메모리를 동적으로 할당합니다. 구조체의 필드에 값을 할당합니다. int 및 char은 임의의 값으로 설정되고 float 필드는 구조체의 번호로 설정됩니다. 이 함수는 배열의 주소를 반환합니다.
함수 # 2는 생성 된 배열 (그 안에있는 포인터의 수)과 인수로 배열에 대한 포인터의 크기를 취하여 배열을 삭제하여 메모리를 비 웁니다.
기능 # 3은 생성 된 배열의 크기를 인수로 배열 포인터를 취하고, 다음 4 탐색 구조 및 계산을 통해 거품을 정렬
함수 번호를 사용 INT 필드에 기초하여 상기 구조를 정렬 문자 (X)가 구조체의 char 필드에서 반복되는 횟수.
다음은 댓글과 오류가있는 코드입니다. 제발, 내가 뭘 잘못하고 있는지 설명 할 수 있니? 솔직히 나는 거의 시간이 없지만 이것을 이해하고 고치기 위해 밤새도록 기꺼이 머물겠습니다.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
struct Foo {
int fieldint;
char fieldchar;
float fieldfloat;
};
Foo *initialize(int N);
int sort(int N, Foo *tablica);
int count(int N, Foo *tablica, char*X);
int deleting(int N, Foo **tablica);
int main() {
//this reads the number N and the letter to find from the .txt file:
FILE *file = fopen("inlab01.txt", "r");
int number;
char letter[1];
if (file == NULL) {
printf("Error opening file");
exit(-1);
}
while (fscanf(file, "%d%s", &number, letter) != EOF);
fclose(file);
//creating the array
//again, it's supposed to be an array of pointers to N structures:
Foo *arr[number];
*arr = initialize(number);
//sorting:
sort(number, *arr); //the program crashes at this function
//counting how many times the given letter appears:
//count(number, *arr, letter);
//we're supposed to print the first 20 of the structures
//this loop prints one structure and then the program crashes
for(int i=0;i<20;i++) {
printf("Structure %d:\nfield int:%d\nfield char:%c\nfield float:\f\n\n", i+1, arr[i]->fieldint, arr[i]->fieldchar, arr[i]->fieldfloat);
}
//deleting:
deleting(number, arr);
getch();
return 0;
}
Foo *initialize(int N) {
Foo **array;
array = (Foo **)malloc(sizeof(Foo) * N);
srand(time(NULL));
for(int i=0; i<N; i++) {
array[i] = (Foo*)malloc(sizeof(Foo));
array[i] -> fieldint = rand(); //random number
array[i] -> fieldchar = (char)(rand() % 24) + 65; //random letter
array[i] -> fieldfloat=i;
}
return *array;
}
int sort(int N, Foo *array) {
int temp;
for (int i=0;i<N;i++){
for (int j=N-1;j>=j;j--) {
if(array[j].fieldint < array[j-1].fieldint) {
temp = array[j-1].fieldint;
array[j-1].fieldint = array[j].fieldint;
array[j].fieldint = temp;
}
}
}
return 0;
}
int count(int N, Foo *array, char*X){
int counter = 0;
for(int i=0;i<N;i++) {
if (array[i].fieldchar == 'X') {
counter = counter+1;
}
}
return counter;
}
int deleting(int N, Foo **array) {
for (int i=0;i<N;i++) {
free(array[i]);
}
free(array);
return 0;
}
모든 것이 컴파일되지만, 실제로 아무것도 수행하는 대신 프로그램이 충돌합니다.
도와주세요.
직면 한 오류를 보여주는 최소, 완전하며 인증 가능한 예를 게시하십시오. 현재 귀하의 질문은 TL, DR 자료이며 대부분의 사람들은 신경 쓰지 않을 것입니다. – DeiDei
코드가 컴파일되지 않습니다. 예를 들어,'* arr = initialize'가 잘못되었거나,'arr [i] -> fieldint'가 잘못되었으므로 수정해야합니다. –
그건 그렇고, 그것은 C + +에서 C 코드를 컴파일하는 것, 아마 당신은 * .cpp 확장자를 사용하고 있습니다. –