从双向链表打印时,为什么我得到相同的字符串值但算术值不同

查理斯·亚历克斯

因此,当从DLL打印时,我得到的所有记录在以下字段中具有相同的字符串值:姓,名,地址,居住地。所有这些字段都包含字符串值。尽管我为每个节点打印了正确的算术值,例如客户ID,地址号码,邮政编码和支出。这是我的主要:

#include <stdio.h>
#include <stdlib.h>

#include "ClientList.h"

#define SIZE_T 5000
#define YES 1
#define NO 0

main(int argc, char *argv[]){
    FILE    *fp=NULL;
    unsigned long customerid;
    char    clname[SIZE_T];
    char    cfname[SIZE_T];
    char    Address[SIZE_T];
    unsigned int AddressNumber;
    char PlaceOfResidence[SIZE_T];
    unsigned int PostalCode;
    float Expenditure;

     ClientList *List = ClientList_create();

    fp=fopen(argv[1],"r");

    while(fscanf(fp,"%lu %s %s %s %d %s %u %f \n", &customerid, clname, cfname, Address, &AddressNumber, PlaceOfResidence, &PostalCode, &Expenditure) != EOF){
        //printf("+++ Just read: %lu %s %s %s %d %s %u %.02f \n",customerid, clname, cfname, Address, AddressNumber, PlaceOfResidence, PostalCode, Expenditure);
        ClientNode *Node = ClientNode_create(customerid, clname, cfname, Address, AddressNumber, PlaceOfResidence, PostalCode, Expenditure);
        ClientList_printNode(Node);
        ClientList_pushfront(List, Node);
}
    int K = size(List);
    unsigned long custid;
    char *name;
    printf("The size of the list is %d records \n",K);
    printf("Enter Customer ID you wish to search:\n");
    scanf("%lu",&custid);
    int M = ClientList_search(List, custid);
    if(M == YES)
        printf("YES\n");
    else
        printf("NO\n");
    Print_List(List);
    ClientList_destroy(List);
    fclose(fp);

    exit(0);
}

这也是我的insertprint_list函数:

void ClientList_pushfront(ClientList *list, ClientNode *node){
    node->next = list->head;
    node->previous = NULL;
    if(list->head != NULL){
        node->next = list->head;
        list->head->previous = node;
    }
    else{
        list->tail = node;
    }
    list->head = node;
    list->size ++;
}

void Print_List(ClientList *list)
{
    ClientNode *current = malloc(sizeof(ClientNode));
    current = list->head;
    while(current)
    {
        printf("Customer ID: %lu | Last Name: %s | First Name: %s | Address: %s | Number: %u | Place of Residence: %s | Postal Code: %d | Expenditure: %.02f |\n", current->customerid, current->LastName, current->FirstName, current->Address, current->AddressNumber, current->PlaceOfResidence, current->PostalCode, current->Expenditure);
        current = current->next;
    }   
}

我的Create_Node函数:

ClientNode *ClientNode_create(unsigned long customerid, char *LastName, char *FirstName, char *Address, unsigned int AddressNumber, char *PlaceOfResidence, unsigned int PostalCode, float Expenditure){
    ClientNode *client = malloc(sizeof(ClientNode));
    client->Expenditure = Expenditure;
    client->customerid = customerid;
    client->FirstName = FirstName;
    client->LastName = LastName;
    client->Address = Address;
    client->AddressNumber = AddressNumber;
    client->PostalCode = PostalCode;
    client->PlaceOfResidence = PlaceOfResidence;
    client->next = NULL;
    client->previous = NULL;
    return client;
}

这是我得到的输出的一部分:

Customer ID: 14260622 | Last Name: Pickett | First Name: Norma | Address: Todd | Number: 333 | Place of Residence: Robinwood | Postal Code: 23209 | Expenditure: 1030.00 |
Customer ID: 18723325 | Last Name: Pickett | First Name: Norma | Address: Todd | Number: 264 | Place of Residence: Robinwood | Postal Code: 42473 | Expenditure: 924.00 |
Customer ID: 16243937 | Last Name: Pickett | First Name: Norma | Address: Todd | Number: 350 | Place of Residence: Robinwood | Postal Code: 34297 | Expenditure: 402.00 |
Customer ID: 16451445 | Last Name: Pickett | First Name: Norma | Address: Todd | Number: 253 | Place of Residence: Robinwood | Postal Code: 14361 | Expenditure: 449.00 |
李斯特先生

ClientNode_create函数中,您复制char指针,而不是char指针中的这意味着所有结构中的文本字段都将指向中的相同char缓冲区main

一种解决方案是使用mallocstrcpy

client->FirstName = malloc(strlen(FirstName)+1);
strcpy(client->FirstName, FirstName));

对于所有字符串。或编写执行此操作的函数-许多库也包含执行此操作的函数strdup
而且不要忘free了以后再记!

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么我从尺寸中得到不同的值?

来自分类Dev

为什么hashrow()为相同的字符串值生成不同的哈希?

来自分类Dev

为什么我用.concat()和+ =用Java字符串得到不同的输出?

来自分类Dev

为什么sha1为相同的输入字符串返回不同的哈希值?

来自分类Dev

为什么System.out.println无法打印出字符串值(在我的特定情况下)?

来自分类Dev

为什么我不能得到重载的字符串?

来自分类Dev

为什么我得到的这个字符串值是nil?

来自分类Dev

为什么在我使用get时得到字符串长度的+1?

来自分类Dev

为什么我的对象的值是函数而不是字符串?

来自分类Dev

为什么我的小型C程序使用cat实用程序打印不同的字符串?

来自分类Dev

为什么我从SQLite数据库中选择的值不放入python变量中,而该变量不等于不同变量中的相同字符串?

来自分类Dev

为什么代码总是打印True。每当我检查是否相等但不打印给定的字符串时总是打印布尔值

来自分类Dev

为什么用curl读取数据我得到字符串值?

来自分类Dev

为什么我得到415尝试发布字符串?

来自分类Dev

当我使用矩进行解析时,Moment.js 2个不同的日期字符串给出相同的值

来自分类Dev

为什么我不能使用cout在C ++中打印字符串值的数组?

来自分类Dev

为什么我用.concat()和+ =用Java字符串得到不同的输出?

来自分类Dev

从$ _GET设置时,为什么我要设置的Cookie(字符串)的值增加一?

来自分类Dev

为什么我得到“字符串不是函数”?

来自分类Dev

我有2个相同值的String Buffer Class对象。字符串equals()方法显示错误结果为什么?

来自分类Dev

当我尝试添加拆分后的字符串时,为什么会得到空字符串?

来自分类Dev

为什么我的字符串不接受空值?

来自分类Dev

当我将浮点值转换为字符串时,为什么会有一个“ E”字符?

来自分类Dev

从双向链接列表打印时,为什么我得到相同的字符串值但算术值不同

来自分类Dev

为什么我的双向链表迭代器打印为空?

来自分类Dev

当通过 Intent 返回字符串的 ArrayList 时,我得到空值

来自分类Dev

为什么我在 Python 中读取 csv 文件时得到的是字符串而不是字典?

来自分类Dev

当我处理字符串时,为什么会得到“预期的字符串或缓冲区”?

来自分类Dev

为什么我的分组在两种不同类型的字符串之间不求和正确的值

Related 相关文章

  1. 1

    为什么我从尺寸中得到不同的值?

  2. 2

    为什么hashrow()为相同的字符串值生成不同的哈希?

  3. 3

    为什么我用.concat()和+ =用Java字符串得到不同的输出?

  4. 4

    为什么sha1为相同的输入字符串返回不同的哈希值?

  5. 5

    为什么System.out.println无法打印出字符串值(在我的特定情况下)?

  6. 6

    为什么我不能得到重载的字符串?

  7. 7

    为什么我得到的这个字符串值是nil?

  8. 8

    为什么在我使用get时得到字符串长度的+1?

  9. 9

    为什么我的对象的值是函数而不是字符串?

  10. 10

    为什么我的小型C程序使用cat实用程序打印不同的字符串?

  11. 11

    为什么我从SQLite数据库中选择的值不放入python变量中,而该变量不等于不同变量中的相同字符串?

  12. 12

    为什么代码总是打印True。每当我检查是否相等但不打印给定的字符串时总是打印布尔值

  13. 13

    为什么用curl读取数据我得到字符串值?

  14. 14

    为什么我得到415尝试发布字符串?

  15. 15

    当我使用矩进行解析时,Moment.js 2个不同的日期字符串给出相同的值

  16. 16

    为什么我不能使用cout在C ++中打印字符串值的数组?

  17. 17

    为什么我用.concat()和+ =用Java字符串得到不同的输出?

  18. 18

    从$ _GET设置时,为什么我要设置的Cookie(字符串)的值增加一?

  19. 19

    为什么我得到“字符串不是函数”?

  20. 20

    我有2个相同值的String Buffer Class对象。字符串equals()方法显示错误结果为什么?

  21. 21

    当我尝试添加拆分后的字符串时,为什么会得到空字符串?

  22. 22

    为什么我的字符串不接受空值?

  23. 23

    当我将浮点值转换为字符串时,为什么会有一个“ E”字符?

  24. 24

    从双向链接列表打印时,为什么我得到相同的字符串值但算术值不同

  25. 25

    为什么我的双向链表迭代器打印为空?

  26. 26

    当通过 Intent 返回字符串的 ArrayList 时,我得到空值

  27. 27

    为什么我在 Python 中读取 csv 文件时得到的是字符串而不是字典?

  28. 28

    当我处理字符串时,为什么会得到“预期的字符串或缓冲区”?

  29. 29

    为什么我的分组在两种不同类型的字符串之间不求和正确的值

热门标签

归档