从双向链接列表打印时,为什么我得到相同的字符串值但算术值不同 [英] Why do i get the same string value but different arithmetic values when printing from a Doubly Linked List

查看:60
本文介绍了从双向链接列表打印时,为什么我得到相同的字符串值但算术值不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So when printing from a DLL I get for all my records the same string values for the following fields: Last Name, First Name, Address, Place of Residence. All these fields hold string values. Although for each node I print I get the correct arithmetic values such as Customer ID, Address Number, Postal Code and Expenditure. This is my main:

#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);
}

这也是我的插入 print_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;
}

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

And this is a part of the output i get:

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指针中的 values .这意味着所有结构中的文本字段都将指向 main 中的相同char缓冲区.

In your ClientNode_create function, you copy the char pointers, not the values in the char pointers. This means the text fields in all structs will all point to the same char buffers in main.

一种解决方案是使用 malloc strcpy .

One solution is to use malloc and strcpy.

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

用于所有字符串.或编写执行此操作的函数-许多库还包含恰好执行此操作的函数 strdup .
并且不要忘记稍后释放内存!

for all strings. Or write a function that does this - many libraries also contain a function strdup that does exactly this.
And don't forget to free the memory later!

这篇关于从双向链接列表打印时,为什么我得到相同的字符串值但算术值不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆