2017-10-20 99 views
-3

下面的代碼和示例csv文件。爲什麼2個不同的字符串在C中具有相同的地址?

我編寫的程序是一個csv閱讀器。在while循環中,我將文件的行作爲字符串獲取,然後使用sscanf提取存儲在本地變量中的數據。

我有我存儲在字符*名稱與char *姓2串,但他們碰巧有相同的地址:

Printing description of name: 
(char *) name = 0x000000010004da78 "Bob" 
Printing description of surname: 
(char *) surname = 0x000000010004da78 "Bob 

我不明白爲什麼,因爲他們有不同的變量名。

我希望有這個問題的答案,但它不是我的問題:Temporary C strings have same address

我改名的變量和重新生成.exe(使用Xcode的),但問題仍然存在。任何想法爲什麼發生這個問題?

感謝

代碼

void readFileTest(FILE* *pFile, TTest* *pRootTest) 
//Reads the content of the file, line by line 
{ 
    int count=0; 
    char string[MAX_SIZE]; 
    TTest *pTestCurrent=NULL, *pPrevious=NULL; 

    //Reads first line (wich is the label line) 
    fgets(string, MAX_SIZE, *pFile); 
    printf("Column labelling : %s\n", string); 

    //allocating pointer 
    pTestCurrent=malloc(sizeof(TTest)); 
    pTestCurrent->ID=0; 
    pTestCurrent->name=""; 
    pTestCurrent->surname=""; 
    pTestCurrent->mean=0.0; 
    pTestCurrent->pNext=NULL; 
    pTestCurrent->pPrevious=NULL; 

    (*pRootTest)=pTestCurrent; 
    pPrevious=pTestCurrent; 

    //Extracts data of each line and stores it in a node 
    while(fgets(string, MAX_SIZE, *pFile)) //reads line by line until the EOF 
    { 
     int identification=0; 
     char* name; 
     char* surname; 
     float mean=0.0; 

     //Counts iterations (lines) in the file 
     count+=1; 
     printf("Iteration n°%d\n", count); 

     //Extracts data of the line in variables 
     sscanf(string, "%d,%[^,],%[^,],%f", &identification, name, surname, &mean); 

     //Assign data in variables to node in pTestCurrent 
     pTestCurrent->ID=identification; 
     pTestCurrent->name=name; 
     pTestCurrent->surname=surname; 
     pTestCurrent->mean=mean; 

     //Displays data in node 
     printf("Line content (stored in pTestCurrent) :\nID : %d\nNAME : %s\nSURNAME : %s\nMEAN : %f\n\n", pTestCurrent->ID, pTestCurrent->name, pTestCurrent->surname, pTestCurrent->mean); 

     if(pTestCurrent==NULL) 
     { 
      printf("ERROR : pointer pTestCurrent is NULL, the programm will exit now\n"); 
      EXIT_FAILURE; 
    } 

     //Refresh pointer 
     pTestCurrent->pNext=malloc(sizeof(TTest)); 
     pTestCurrent=pTestCurrent->pNext; 
     pTestCurrent->pPrevious=pPrevious; 
     pTestCurrent->pNext=NULL; 
     pPrevious=pTestCurrent; 
     } 
}; 

示例文件:

ID,NAME,SURNAME,MEAN 
1,Smith,Bob,4.32 
2,Mason,Jack,9.21 
3,Gabe,John,2.67 

回答

1

你讓兩個錯誤:

  1. 你沒有有效的內存,當你sscanf

  2. 你不要複製掃描的值。

第一:

sscanf(string, "%d,%[^,],%[^,],%f", &identification, name, surname, &mean); 

這裏namesurname只對字符指針,但他們並不指向你的程序內存(它們是未初始化的並且可以在任何地方指出這本來是可能你有分段錯誤)。在代替,這樣做:

char name[64], surname[64]; // or any proper size for the names 

現在您爲您的sscanf有效的內存。其次,當您將掃描的數據複製到結構中時,必須在結構中爲數據分配內存。只有pTestCurrent->name=name;,您可以將指針置於name的結構的名稱字段中,但不要複製數據。因此,在您的下一個sscanf上,您將只覆蓋數據。相反,請執行以下操作:

pTestCurrent->name= malloc(strlen(name)+1); 
    strcpy(pTestCurrent->name, name); 
2

這裏的問題是,在while循環中,你使用未初始化的指針。這些指針不能保證指向任何地方有效的,並且你正試圖通過它們訪問存儲器位置指針。這會導致undefined behavior

解決方案:在將它們作爲參數傳遞給sscanf()之前,需要確保它們指向一些可以由進程訪問的有效內存。

+0

由於沒有分配內存但已訪問,情況更糟。這僅僅是C語言基礎知識的不足。 –

+0

如果他繼續以這種方式「複製」字符串,初始化它們將無濟於事。 – Leeor

相關問題