简体   繁体   English

如何将指针数组传递给结构并分配成员

[英]How do I pass an array of Pointers to a structure and allocate the members

ok, heres my code. 好的,这是我的代码。 I'm trying to pass an array of pointers to a structure to a function. 我正在尝试将一个指向数组的指针传递给函数。 I need to dynamically allocate each structure and put a pointer to that structure in the array. 我需要动态分配每个结构并在数组中放置一个指向该结构的指针。 When I malloc the second time thru it gets a heap error. 当我第二次通过malloc时它会出现堆错误。 HELP 救命

#define MAXSTRUCTS 50
#define MAXBUFF 100


typedef struct {  
 char fullName[41];  
 char address[41];  
 char cityState[41];  
 char zipcode[11];  
} Persons;  

int readData(Persons *structPtrs[]);

int main(void) {

     int totalStructs;  
     Persons *structPtrs[MAXSTRUCTS];  
     totalStructs = 0;  
     structPtrs[0] = NULL;  
     totalStructs = readData(structPtrs);  
}

int readData(Persons *strptr[]) {

    int tStructs = 0;  
    int recs;  
    char inRecord[MAXBUFF];  
    Persons *tmpPtr;  
    tStructs = 0;  
    for (recs=0; recs < MAXSTRUCTS; recs++) {  
        if (gets(inRecord) != NULL) {  
           strptr[recs] = (Persons *)malloc( sizeof(Persons));  
           tmpPtr = strptr[recs];  
           strncpy(tmpPtr->fullName,inRecord,MAXBUFF);  
           gets(inRecord);  
           strncpy(tmpPtr->address,inRecord,MAXBUFF);  
           gets(inRecord);  
           strncpy(tmpPtr->cityState,inRecord,MAXBUFF);  
           gets(inRecord);  
           strncpy(tmpPtr->zipcode,inRecord,MAXBUFF);  
           strptr[recs] = tmpPtr;  
           tStructs++;  
        }  
        else {  
             if ( recs = 0 ) {  
                exit (0);  
             }  
             recs=MAXSTRUCTS;  
        }  
    }  
    return(tStructs);  
}  

You are doing everything right in regard of passing an array of pointers and allocating memory. 关于传递指针数组和分配内存,您做的一切正确。 What leading to a heap corruption is incorrect usage of strncpy function. 导致堆损坏的原因是strncpy函数的错误使用。 The arrays where you are trying to copy data to are slightly smaller than MAXBUFF in all cases. 在所有情况下,您要将数据复制到的数组都比MAXBUFF To fix this, you have to specify the size of destination array instead of MAXBUFF . 要解决此问题,您必须指定目标数组的大小,而不是MAXBUFF For example, instead of: 例如,而不是:

strncpy(tmpPtr->fullName,inRecord,MAXBUFF); 

... do (assuming that buffer is already filled with \\0 symbols): ... do(假设缓冲区已经用\\0符号填充):

strncpy(tmpPtr->fullName,inRecord, sizeof(tmpPtr->fullName) - 1); 

Also, using gets function is not recommended as well as it could easily lead to buffer overruns. 另外,不建议使用gets函数,因为它很容易导致缓冲区溢出。 Try using fgets instead. 请尝试使用fgets

Here is your modified example that works: 以下是您修改后的示例:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define MAXSTRUCTS 2
#define MAXBUFF 100

typedef struct {
  char fullName[41];
  char address[41];
  char cityState[41];
  char zipcode[11];
} Persons;

int readData(Persons *structPtrs[]);

int main ()
{
  int totalStructs;
  int recs;
  Persons *structPtrs[MAXSTRUCTS];
  totalStructs = 0;
  structPtrs[0] = NULL;
  totalStructs = readData(structPtrs);
  for(recs = 0; recs < totalStructs; ++recs) {
    printf ("Record #%d - %s\n", recs + 1, structPtrs[recs]->fullName);
  }
  return 0;
}

int readData(Persons *strptr[])
{
  int tStructs = 0;
  int recs;
  char inRecord[MAXBUFF];
  Persons *tmpPtr;
  tStructs = 0;
  for (recs=0; recs < MAXSTRUCTS; ++recs) {
    memset (inRecord, 0, sizeof(inRecord));
    if (fgets(inRecord, sizeof (inRecord) - 1, stdin))
      {
      strptr[recs] = (Persons *)malloc(sizeof(Persons));
      tmpPtr = strptr[recs];
      memset (tmpPtr, 0, sizeof(Persons));
      strncpy(tmpPtr->fullName,inRecord,sizeof(tmpPtr->fullName) - 1);
      fgets(inRecord, sizeof (inRecord) - 1, stdin);
      strncpy(tmpPtr->address,inRecord,sizeof(tmpPtr->address) - 1);
      fgets(inRecord, sizeof (inRecord) - 1, stdin);
      strncpy(tmpPtr->cityState,inRecord, sizeof(tmpPtr->cityState) - 1);
      fgets(inRecord, sizeof (inRecord) - 1, stdin);
      strncpy(tmpPtr->zipcode,inRecord, sizeof (tmpPtr->zipcode) - 1);
      strptr[recs] = tmpPtr;
      tStructs++;
    } else {
      if ( recs = 0 ) {
    exit (0);
      }
      recs=MAXSTRUCTS;
    }
  }
  return(tStructs);
}

int readDataToRecord( Persons *eachEntry[] ) {

int numEntries = 0 ;

Persons *tempPtr ;

for( int i=0 ; i < NUM_OF_RECORDS; ++i ) {

    eachEntry[i] = ( Record * ) malloc( sizeof( Record ) ) ;
    memset( eachEntry[i], 0, sizeof( Record ) ) ;

    tempPtr = eachEntry[i] ;

    fgets( tempPtr->firstName,  sizeof( tempPtr->firstName ), stdin ) ;
    fgets( tempPtr->secondName, sizeof( tempPtr->secondName), stdin ) ;

    eachEntry[i] = tempPtr ;

    ++numEntries ;
}

return numEntries ;

}

This would also efficiently do the job. 这也可以有效地完成这项工作。 Once you have new record, you would any how have the memory allocated for each of its member. 获得新记录后,您将如何为其每个成员分配内存。 So you can directly fgets to that variable. 因此,您可以直接获取该变量。

@Vlad : Please let me know if I am wrong. @Vlad:如果我错了,请告诉我。

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

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