2

I have been trying to write a function with a pointer point to a struct array. After calling it my array should have a new struct element.

This is my function changed by ur guys suggestion

void addPic(pic *picture_Record, int picNumber){


pic tmp_Pic;
char tmp_fileName[100];
char tmp_description[100];
char tmp_location[100];

if (picture_Record[picNumber].description ==NULL || picture_Record[picNumber].fileName ==NULL||picture_Record[picNumber].location==NULL)
    return;

printf("Enter: Picture Name, Picture Description, Picture Location, Picture People(int)\n");
scanf("%s%s%s%d",tmp_fileName, tmp_description, tmp_location, &picture_Record[picNumber].peopleCount);  

tmp_Pic.fileName = (char*)malloc(strlen(tmp_fileName)+1);
tmp_Pic.description=(char*)malloc(strlen(tmp_description)+1);
tmp_Pic.location = (char*)malloc(strlen(tmp_location)+1);
strcpy(tmp_Pic.fileName, tmp_fileName);
strcpy(tmp_Pic.description, tmp_description);
strcpy(tmp_Pic.location, tmp_location);

picture_Record[picNumber] = tmp_Pic;
free(tmp_Pic.fileName);
free(tmp_Pic.description);
free(tmp_Pic.location);
printf("\nInput Done!\n");

This is how I call it.

 int picNumber = 0
 pic pictureRecord[Maximun_Picture +1]= { "" };

 addPic(&pictureRecord[picNumber], picNumber);
 picNumber++;
 //testing
 printf("%s",pictureRecord[0].location)

This is my struct.

  typedef struct picture_Data
  {
    char* fileName;
    char* description;
    char* location;
    int peopleCount;
  }pic;

It doesn‘t work and printing me Null as th location of the first element. Why? Can somebody help me.

2 Answers 2

2

The problem is that this line

scanf("%s%s%s%d",pic_tmp.fileName, pic_tmp.description, pic_tmp.location, &pic_tmp.peopleCount);

assumes that pic.tmp has sufficient space allocated for fileName, description, and location. None of that is true, however, because all three fields remain uninitialized.

In order to fix this, change the code to read strings into a temporary buffer, and then copy it into dynamically allocated strings.

Here is how you do it for fileName; you need to do the same thing for all three:

char tmp_fileName[100];
char tmp_description[100];
char tmp_location[100];
scanf("%99s%99s%99s%d",tmp_fileName, tmp_description, tmp_location, &pic_tmp.peopleCount);
pic_tmp.fileName = malloc(strlen(tmp_fileName)+1);
strcpy(pic_tmp.fileName, tmp_fileName);
...

You can copy the struct with a single assignment, too:

picture_Record[picNumber] = pic_tmp;

Don't forget to call free on all three members of each struct to avoid memory leaks.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 and while the OP is at it, lose the whole passed-index thing in the first place. The caller should be passing pictureRecord + picNumber as the sole parameter to addPic(). The temp is not needed at all.
Sorry to brother again, but my code doesnt work. after inserting the interger the programe will just crush and break down. can u have a look.
@user3628117 This is because you are freeing the pointers too early. The freeing is supposed to happen last - after you have finished using your record structures. Dereferencing freed pointers is undefined behavior, which often causes crashes.
0

You should first have to allocate memory

 void addPic(pic *picture_Record, int picNumber){
 pic pic_tmp;  
 printf("Enter: Picture Name, Picture Description, Picture Location, Picture
 People(int)\n");
 pic_tmp.fileName = (char *)malloc(5);    
 picture_Record[picNumber].fileName = (char *)malloc(5);  
 scanf("%s%d",pic_tmp.fileName, &pic_tmp.peopleCount);  
 printf("\nInput Done!\n");  
 strcpy(picture_Record[picNumber].fileName, pic_tmp.fileName);  
 //picture_Record[picNumber].fileName = pic_tmp.fileName;  
 picture_Record[picNumber].description= pic_tmp.description;  
 picture_Record[picNumber].location = pic_tmp.location;  
 picture_Record[picNumber].peopleCount = pic_tmp.peopleCount;  
 printf("%s,%s",pic_tmp.fileName,picture_Record[picNumber].fileName );  
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.