简体   繁体   中英

double pointer typedef struct array

I am new to c programming and I am stuck with this one its a typedef struct and what I would like to do is that I want to create an array from the double pointer from this structure

typedef struct
{
 char* firstname;
 float   price;
}Name,*pName,**ppName;

typedef struct
{
 ppName Names;
 unsigned int numPerson;
}Book;

And my main which always give me segmentation fault dont mind the loop it is looping until the use says to quit.

 int main(void)
{

 Book D;
 setUpCollection(&D);

  while(..)
 {


  scanf(...);
  switch(...)
  {
  case 1:
   if(!AddNewPerson(&D))
    return 1;
   break;
  case 2:
  ....
  case 3:
   ....
  default:
   printf("Please enter a valid choice");
  }
 }
 return 0;
}

void setUpCollection(Book* data){
 Name name;
 pName pname;


 pname= malloc(MAX_PERSON* sizeof(pName));

 pname= &name;

 data->Names= &pname;
 data->numPerson= 0;

}

BOOL AddNewPerson(Book* data){
 char *title = malloc(sizeof(char));
 int len;
 Name name;
 pName pname;


 scanf(...);
 len = strlen(firstname);

 name.firstname = malloc(len * sizeof(char*));
 name.firstname  = firstname;

 pname= malloc(1);
 pname= &name;

 data->DVDs[data->numPerson++] = pname;

  printf("%0.2f", data->Names[(data->numPerson)-1]->price); 

 return TRUE;
}

My main problem is that I cant print all the added names and also getting segmentation fault.

There are quite a few errors in your program but let me mention a few:


Doesn't this seem odd to you:

pname= malloc(MAX_PERSON* sizeof(pName));
pname= &name;

you are creating a memory leak by first letting pname point to the array of pName then assigning to &name .


What is this:

char *title = malloc(sizeof(char)); // ?

here you allocate too less space

name.firstname = malloc(len * sizeof(char*));

it should be

name.firstname = malloc(len * sizeof(char) + 1);

or more readable:

name.firstname = malloc(len+1);

this makes no sense again:

pname= malloc(1);
pname= &name;

again you created a memory leak by first letting pname point to a heap block of 1 byte then assigning it to a local variable which you include in data - the local variable is freed up once you leave AddNewPerson() so data will point to garbage.


Instead do something like this (I am no fan of having typedefs for pointers), also try avoiding naming types the same way you name variables for clarity:

typedef struct
{
  char *firstname;
  float price;
} Name;

typedef struct
{
  Name** names;
  unsigned int numPerson;
} Book;

Now allocate the initial size of your array, the whole point of having it on the heap is that the array can grow if more records are added than MAX_PERSONS so you need to keep track of the number of used records in the array as well as the number of records allocated

int allocated = MAX_PERSONS;
Book D;
D.names = malloc( allocated * sizeof(Name*) );
D.numPerson = 0; 

then loop over user input and add records keeping track of how many records have been read. Since names is an array of pointers, you need to allocate a Name struct each time you add an entry

eg

D.names[i] = malloc( sizeof(Name) );
D.names[i]->firstname = strdup(userInputName);
D.names[i]->price = userInputPrice;

then at each iteration check if there is allocated memory left

++i;
if ( i == allocated )
{
  // if yes you need to get more memory, use realloc for that
  // get e.g. 10 more records
  Name* tmp = realloc( D.names, (allocated + 10)*sizeof(Name) ); 
  if ( tmp != NULL )
  {
    D.names = tmp;
    allocated += 10;
  }
  else 
  { .. some error msg .. }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM