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

时间:2010-12-14 19:04:33

标签: c

好吧,继承我的代码。我正在尝试将一个指向数组的指针传递给函数。 我需要动态分配每个结构并在数组中放置一个指向该结构的指针。 当我第二次通过malloc时它会出现堆错误。 帮助

#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);  
}  

2 个答案:

答案 0 :(得分:1)

在传递指针数组和分配内存方面,你正在做的一切正确。导致堆损坏的原因是strncpy函数的使用不正确。在所有情况下,尝试复制数据的数组都略小于MAXBUFF。要解决此问题,您必须指定目标数组的大小而不是MAXBUFF。例如,而不是:

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

... do(假设缓冲区已经填充了\0个符号):

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

此外,建议不要使用gets函数,否则很容易导致缓冲区溢出。请尝试使用fgets

以下是您修改后的示例:

#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);
}

答案 1 :(得分:1)

 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 ;

}

这也可以有效地完成这项工作。一旦你有了新记录,你就可以为每个成员分配内存。所以你可以直接fgets到那个变量。

@Vlad:如果我错了,请告诉我。