簡體   English   中英

如何將指針數組傳遞給結構並分配成員

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

好的,這是我的代碼。 我正在嘗試將一個指向數組的指針傳遞給函數。 我需要動態分配每個結構並在數組中放置一個指向該結構的指針。 當我第二次通過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);  
}  

關於傳遞指針數組和分配內存,您做的一切正確。 導致堆損壞的原因是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);
}

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 ;

}

這也可以有效地完成這項工作。 獲得新記錄后,您將如何為其每個成員分配內存。 因此,您可以直接獲取該變量。

@Vlad:如果我錯了,請告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM