2014-01-09 54 views
-2

這是整天編輯我的代碼,但無法找到我的數組的最後一個位置被覆蓋的原因!簡單的C練習。最後的位置被覆蓋

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

typedef struct Person{ 
    char *name; 
    char *surname; 
    char *address; 
    char *number; 
} Person; 

char * getString(void); 

int main(void) { 
    struct Person *rub = NULL, *ttmp = NULL; 
    int idx=0,i,j,k; 
    char c; 
    char *tmp = NULL; 
    do{ 
     printf("*******************************\ni - Insert new\n"); 
     printf("n - Find by name\n"); 
     printf("c - Find by surname\n"); 
     printf("e - Delete\n"); 
     printf("p - Print list\n"); 
     printf("0 - Exit\n*******************************\n"); 
     c = getchar(); 
     printf("%d\n",c); 
     getchar(); 
     switch(c){ 
      case 'i': 
       ttmp = (struct Person *) realloc(rub, (idx+1)*sizeof(Person)); 
       if(ttmp == NULL){ 
        printf("Cannot allocate more memory.\n"); 
        exit(1); 
       } 
       else 
        rub = ttmp; 
       printf("Nome: "); 
       tmp = getString(); 
       rub[idx].name = (char *) malloc(strlen(tmp)); 
       rub[idx].name = tmp; 
       //printf("Surname: "); //commented in order to test faster 
       //tmp = getString(); 
       rub[idx].surname = (char *) malloc(strlen(tmp)); 
       rub[idx].surname = tmp; 
       //printf("Address: "); 
       //tmp = getString(); 
       rub[idx].address = (char *) malloc(strlen(tmp)); 
       rub[idx].address = tmp; 
       //printf("Number: "); 
       //tmp = getString(); 
       rub[idx].number = (char *) malloc(strlen(tmp)); 
       rub[idx].number = tmp; 
       idx++; 
       for(k=0;k<idx;k++){ 
        printf("%d) %s %s\n%s\n%s\n-------------------\n", k+1, rub[k].name,rub[k].surname,rub[k].address,rub[k].number); 
       } 
       break; 
      case 'n': 
       printf("What name are you looking for? "); 
       scanf("%s",tmp); 
       for(k=0;k<idx;k++){ 
        if(strcmp(rub[k].name,tmp) == 0){ 
         printf("%s\n%s\n%s\n%s\n", rub[k].name,rub[k].surname,rub[k].address,rub[k].number); 
        } 
       } 
       break; 
      case 'c': 
       printf("What surname are you looking for? "); 
       scanf("%s",tmp); 
       for(k=0;k<idx;k++){ 
        if(strcmp(rub[k].surname,tmp) == 0){ 
         printf("%s\n%s\n%s\n%s\n", rub[k].name,rub[k].surname,rub[k].address,rub[k].number); 
        } 
       } 
       break; 
      case 'e': 
       printf("Select number to delete record:\n "); 
       for(k=0;k<idx;k++){ 
        printf("%d) %s %s\n", k+1, rub[k].name,rub[k].surname); 
       } 
       scanf("%d",&j); 
       ttmp = NULL; 
       for(k=0,i=0;k<idx;k++){ 
        if(k+1 != j){ 
         ttmp = (struct Person *) realloc(ttmp, (i+1)*sizeof(Person)); 
         ttmp[i].name = (char *) malloc(strlen(rub[k].name)); 
         ttmp[i].surname = (char *) malloc(strlen(rub[k].surname)); 
         ttmp[i].address = (char *) malloc(strlen(rub[k].address)); 
         ttmp[i].number = (char *) malloc(strlen(rub[k].number)); 
         ttmp[i].name = rub[k].name; 
         ttmp[i].surname = rub[k].surname; 
         ttmp[i].address = rub[k].address; 
         ttmp[i].number = rub[k].number; 
         i++; 
        } 
       } 
       --idx; 
       rub = (struct Person *) realloc(ttmp, (idx)*sizeof(Person)); 
       for(k=0;k<idx;k++){ 
        printf("%d/%d) %s %s\n%s\n%s\n-------------------\n", k,idx, rub[k].name,rub[k].surname,rub[k].address,rub[k].number); 
       } 
       break; 
      case 'p': 
       for(k=0;k<idx;k++){ 
        printf("%d) %s %s\n%s\n%s\n-------------------\n", k+1, rub[k].name,rub[k].surname,rub[k].address,rub[k].number); 
       } 
       break; 
      case '0': 
       return 0; 
       break; 
     } 
     fseek(stdin,0,SEEK_END); 
    } 
    while(c != 0); 
    return 0; 
} 

char * getString(void){ 
    char *stringa = NULL, c; 
    int i=0; 
    stringa = malloc(sizeof(char)); 
    while((c=getchar()) != '\n'){ 
     stringa = (char *) realloc(stringa, (i+1)*sizeof(char)); 
     stringa[i++] = c; 
    } 
    stringa[i] = '\0'; 
    return stringa; 

} 

這是我的輸入(請輸入相同的輸入,並告訴我是否得到相同的錯誤)。我會用一些愚蠢的和隨機的話這個例子:

i 
asd 
i 
qwe 
i 
zxc 
p 
n 
asd 
p 

這是我最後的「P」命令的輸出:

1) asd asd 
asd 
asd 
------------------- 
2) qwe qwe 
qwe 
qwe 
------------------- 
3) asd asd 
asd 
asd 
------------------- 

爲什麼數組的最後一個位置成爲相同第一?? 任何其他建議或代碼的建議將受到歡迎!

感謝

+0

引擎收錄有防火牆,所以我不能幫 – Hogan

+0

對於您預計發佈您的代碼直接向這個網站提供最好的服務。 – suspectus

+0

請評論,當你downvote,這樣我可以明白什麼是錯的...否則不downvote! – Yuri

回答

1

爲什麼數組的最後一個位置成爲相同的第二realloc的?

case 'n': 
    printf("What name are you looking for? "); 
    scanf("%s",tmp); 

TMP被重用。 (case 'c':太)

變化到

E.g

case 'n': 
    printf("What name are you looking for? "); 
    tmp=getString(); 
    for(k=0;k<idx;k++){ 
     if(strcmp(rub[k].name,tmp) == 0){ 
      printf("%s\n%s\n%s\n%s\n", rub[k].name,rub[k].surname,rub[k].address,rub[k].number); 
     } 
    } 
    free(tmp); 
    break; 

rub[idx].name = (char *) malloc(strlen(tmp)); 
rub[idx].name = tmp; 
//As well as in other like 

內存泄漏。

只有

rub[idx].name = tmp;//by getString() 
1

我din't閱讀整個代碼,但發現這個代碼,似乎不正確:

stringa = malloc(sizeof(char)); 
while((c=getchar()) != '\n'){ 
    stringa = (char *) realloc(stringa, sizeof(char)); 

您是realloc的-ING是一個字節,該字符串訥韋爾長大!

你應該根據變量i

+0

謝謝你的建議,我糾正了代碼,但仍然得到相同的錯誤。 – Yuri