简单的 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 newn");
        printf("n - Find by namen");
        printf("c - Find by surnamen");
        printf("e - Deleten");
        printf("p - Print listn");
        printf("0 - Exitn*******************************n");
        c = getchar();
        printf("%dn",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 %sn%sn%sn-------------------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("%sn%sn%sn%sn", 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("%sn%sn%sn%sn", 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 %sn", 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 %sn%sn%sn-------------------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 %sn%sn%sn-------------------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] = '';
    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
-------------------

为什么数组的最后一个位置变得与第一个位置相同?欢迎对代码提出任何其他建议或建议!!

谢谢

为什么数组的最后一个位置与第一个位置相同?

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

TMP 被重复使用。(case 'c':也是)

更改为

例如

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("%sn%sn%sn%sn", 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()
我没有

阅读整个代码,但发现这段代码似乎不正确:

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

你总是重新分配一个字节,字符串永远不会长大!

您应该根据变量重新分配i

相关内容

  • 没有找到相关文章

最新更新