Q:

Create an element of a struct array using struct pointer function

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.

A:

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.

