c - 链表中的字符串相互重叠 C

标签 c string memory linked-list

我正在尝试制作一个结构来记录一群人。出于某种原因,每当我输入此人的房间名称时,房间名称都会取代此人的名字。这显示在 addResi 函数中的测试 5 中。

struct resi {
    char *firstname;
    char *lastname;
    double stnscore;
    char *roomName;
    struct resi *next;
};
struct resi *head = NULL;
struct resi *current = NULL;

void printAll() {
    struct resi *outer = head;
    if (outer == NULL) {
        printf("Empty\n"); fflush(stdout);
    }
    while (outer != NULL) {
        printf("First Name: %s, Last Name: %s, Score: %lf, roomName: %s\n", outer->firstname, outer->lastname, outer->stnscore, outer->roomName); fflush(stdout);
        outer = outer->next;
    }
}
void addResi(char *firstname, char *lastname, double stnscore, char *roomName) {
    printf("test 0\n"); fflush(stdout);

    struct resi *link = (struct resi*) malloc(sizeof(struct resi));
    printf("test 1 %s\n", firstname); fflush(stdout);

    strcpy(link->firstname, firstname);
    printf("test 2 %s\n", link->firstname); fflush(stdout);

    strcpy(link->lastname, lastname);
    printf("test 3\n"); fflush(stdout);

    link->stnscore = stnscore;
    printf("test 4\n"); fflush(stdout);

    strcpy(link->roomName, roomName);
    printf("test 5 %s %s\n", link->firstname, link->roomName); fflush(stdout); //they shouldn't be the same.

    link->next = head;
    head = link;
}

int main (void)
{
    int totalStud, tempX = 0;
    char firTemp[21], lasTemp[21], roomTemp[21];
    double scrTemp;

    printf("How many residences?\n"); fflush(stdout);
    scanf("%d", &totalStud);
    if (totalStud < 5) {
        printf("The number %d is less than 5, please type a different number\n",
            totalStud); fflush(stdout);
    }
    while (totalStud < 5) {
        scanf("%d", &totalStud);
    }

    printf("type the residences with following format\nfirst name last name score room\n"); fflush(stdout);

    for (tempX = 0; tempX < totalStud; tempX++) {
        scanf("%20s %20s %lf %20s", firTemp, lasTemp, &scrTemp, roomTemp);
        printf("test mid %s %s %lf %s\n", firTemp, lasTemp, scrTemp,
            roomTemp); fflush(stdout);
        addResi(firTemp, lasTemp, scrTemp, roomTemp);
        printAll();
    }
}

如果我输入“5”,然后输入“Bob Billy 45.5 Jackson”,最后的输出应该看起来像“名字:Bob,姓氏:Billy,分数:45.500000,roomName:Jackson”,但它显示为“First姓名:Jackson,姓氏:Billy,分数:45.500000,房间名称:Jackson”

最佳答案

A resi实际上并不保留名称的空间 - 它只是包含指向它们的指针。我所知道的最小变化是改变 char *firstnamechar firstname[256] ,同样对于另一个char * resi 中的字段.

resi中的指针保存字符所在的内存位置,而不是字符本身。当你malloc ,这些位置是未指定的——它们可以是任何地方。因此,strcpy调用将字符放在内存中的某处,但我们不确定放在哪里!

因为你没有定义放置这些字符的位置,我怀疑内存中的一些随机位置是重叠的,所以几个 strcpy调用将数据放在内存的同一部分。这可能会导致您看到的行为。

关于c - 链表中的字符串相互重叠 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53453813/

相关文章:

c - 指针声明是做什么用的?

c - 无效读取 - Valgrind 和 C

c - 显示 GTK 调试日志消息

c# - 正则表达式应用 - 匹配百分比

javascript - 如何将数字转换为时间?

python - 从以两个十六进制字符串传输的 C double 转换

iphone - 我在我的应用程序中使用的内存在终止后会变干净吗?甚至泄漏? (iOS)

caching - 为什么在大多数处理器中,L1 缓存的大小都小于 L2 缓存的大小?

c - scanf 多个变量

c - '\0' 的 ASCII 值与 0 的 ASCII 值相同吗?