简体   繁体   English

双指针typedef结构数组

[英]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 我是c编程的新手,我坚持使用它的一个typedef结构,我想做的是我想从这个结构的双指针创建一个数组

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 . 您正在创建内存泄漏,首先让pname指向pName数组,然后分配给&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. 再次,您通过首先让pname指向1个字节的堆块然后将其分配给您包含在data的本地变量来创建内存泄漏 - 一旦离开AddNewPerson() ,本地变量就会被释放,因此数据将指向垃圾。


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),也尝试避免命名类型,就像为了清晰起见命名变量一样:

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 现在分配数组的初始大小,将它放在堆上的重点是,如果添加的记录多于MAX_PERSONS,则数组可以增长,因此您需要跟踪数组中已使用记录的数量以及分配的记录数

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 由于names是一个指针数组,因此每次添加条目时都需要分配一个Name结构

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 .. }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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