I am trying to learn C programming, but I got stuck when I was trying to pass an uninitialized pointer of structure to the function.
This is my structure:
typedef struct {
char name[30];
char surname[30];
char gender;
char DOB[10];
int waist;
int hip;
}healthcare_table;
my main
int main() {
healthcare_table *records;
int step = 1;
load_healthcare_table(records,&step);
return 0;
}
My function will open the file and according to the number of lines in the text file, it will initialize the pointers, but I cannot do it. Any suggestion?
UPDATE: I am taking such that error, I do not know what is wrong. Error message is :Exception thrown at 0x00D7529C in ConsoleApplication1.exe: 0xC0000005: Access violation reading location 0x0000004C.
And this is my updated code:
typedef struct {
char name[30];
char surname[30];
char gender;
char DOB[10];
int waist;
int hip;
}healthcare_table;
void load_healthcare_table(healthcare_table **t, int *step) {
int i;
FILE *infile;
infile = fopen("records.txt", "r");
if (infile == NULL)
printf("File not opened successfully");
else
printf("File opened successfully");
for (i = 0; fscanf(infile, "%s %s %c %s %d %d", *t[*step]->name, *t[*step]->surname, t[*step]->gender, *t[*step]->DOB, t[*step]->waist, t[*step]->hip) != EOF; i++) {
if (fgetc(infile) == '\n') {
*step++;
//*t = (healthcare_table*)realloc(*t, *step);
//fprintf(infile, "%s %s %c %s %d %d", *t[*step]->name, *t[*step]->surname, t[i]->gender, *t[*step]->DOB, t[*step]->waist, t[*step]->hip);
}
}
*t = (healthcare_table*)malloc((*step) * sizeof(healthcare_table));
for (i = 0; fscanf(infile, "%s %s %c %s %d %d", *t[i]->name, *t[i]->surname, t[i]->gender, *t[i]->DOB, t[i]->waist, t[i]->hip) != EOF; i++) {
if (fgetc(infile) == '\n')
fprintf(infile, "%s %s %c %s %d %d", *t[i]->name, *t[i]->surname, t[i]->gender, *t[i]->DOB, t[i]->waist, t[i]->hip);
}
}
void display_healthcare_table(healthcare_table *records) {
}
void search() {
}
void WHR_interpreter() {
}
int main() {
healthcare_table *records=NULL;
int step = 0;
load_healthcare_table(&records, &step);
return 0;
}
UPDATE 2: I think problems are fscanf and fprintf. how can i solve the problem if I am wrong?
If you do not want to allocate the pointer in the main function, you have two possible solutions:
Solution 1
Do not pass the healthcare_table pointer to the load_healthcare_table() function, but rather return the initialized pointer from the function itself. Something along these lines:
int main() {
healthcare_table *records;
int step = 1;
records = load_healthcare_table(&step);
return 0;
}
In this case your load_healthcare_table() function would be:
healthcare_table* load_healthcare_table(int* step) {
healthcare_table *table;
table = (healthcare_table*)malloc(sizeof(healthcare_table));
// Fill table here
return table;
}
Important note In this case make sure you do not create the table locally to the load_healthcare_table() function. This would destroy the table once the function returns, resulting in garbage data. A function should never return a pointer to its local data. So the following example would be wrong:
healthcare_table* load_healthcare_table(int* step) {
healthcare_table table;
// Fill table here
return &table;
}
Solution 2
Pass a pointer to a pointer of the healthcare_table to the function load_healthcare_table(). It would look something along these lines:
int main() {
healthcare_table *records;
int step = 1;
load_healthcare_table(&records,&step);
return 0;
}
In this case, the load_healthcare_table() function will be:
void load_healthcare_table(healthcare_table** t, int* step ) {
*t = (healthcare_table*)malloc(sizeof(healthcare_table));
// Fill table here
}
EDIT
As suggested by Ajay Brahmakshatriya: Sticking to standard C notion of local variables rather than going into stack terminologies.
Although there is no use in passing an uninitialized pointer to a function, then it could be useful to pass an unallocated pointer as long as:
realloc()
to reallocate memory for the pointer or check if it is a null-pointer before using malloc()
.example code snippet (assuming the struct a_struct
is defined):
a_struct *a_funct(a_struct *p);
int main(void)
{
struct a_struct *p_a_struct = NULL;
p_a_struct = a_funct(p_a_struct);
return 0;
}
a_struct *a_funct(a_struct *p)
{
// Does some stuff to find how many records p should point to.
p = realloc(p, sizeof a_struct * number_of_records);
// Check p for error.
// Do more stuff?
return p;
}
Or you could pass a pointer to a pointer to the function as has been mentioned in other answers.
User contributions licensed under CC BY-SA 3.0