The programm falls in the infinite loop. But arr->count
printf
prints a normal value (4, for example). count
has a type unsigned int
and arr
is a pointer to int. What's the problem here? loop prints arr values at first and then continues to print trash values
In arrat_get
it prints array just fine
struct _arr {
size_t count;
int* arr;
} ;
typedef struct _arr array_t;
array_t* array_get(FILE* file){
int* arr = NULL;
size_t count = 0;
array_t* arr_t;
array_t temp;
int i = 0;
if (!file) {
fprintf(stderr, "there is no such file\n");
return;
}
if (fscanf(file, "%u", &count) == EOF) {
fprintf(stderr, "can't read count from file\n");
return;
}
temp = array_create(arr, count);
arr_t = &temp;
printf("%i\n", arr_t->count);
for (i = 0; i < arr_t->count; i++){
if (fscanf(file, "%d", &arr_t->arr[i]) == EOF) {
fprintf(stderr, "can't read arr from file\n");
return;
}
}
for (i = 0; i<arr_t->count; i++)
printf("%d ", arr_t->arr[i]);
printf("\n");
return arr_t;
}
int main(){
array_t* arr_t;
int i = 0;
printf("enter count and arr:\n");
arr_t = array_get(stdin);
printf("count in main=%u\n", arr_t->count);
for (i = 0; i<arr_t->count; i++)
printf("%d ", arr_t->arr[i]);
getch();
return 0;
}