简体   繁体   English

错误的输出,不明白为什么 C

[英]wrong output, can't understand why C

I am a beginner in C programming and I built a function that recieves the string "HodHasharon,frozenYogurt,100" .我是 C 编程的初学者,我构建了一个接收字符串"HodHasharon,frozenYogurt,100"的函数。 The function cuts the string into 3 pieces where every piece is a field of my City struct.该函数将字符串切成 3 段,其中每一段都是我的City结构的一个字段。

I want to put "HodHasharon" into City pcity.name (name of the city), "frozenYogurt" into pcity.popularFood (popular food) and number of residents ( 100 ) into pcity.residents .我想将"HodHasharon"放入 City pcity.name (城市名称),将"frozenYogurt"放入pcity.popularFood (流行食品)并将居民人数( 100 )放入pcity.residents

When i am debugging the output in my function the output is correct but when i print from the main.c I got a concatenated string.当我在我的函数中调试输出时,输出是正确的,但是当我从main.c打印时,我得到了一个连接的字符串。

For example, when I print pcity.name I get "HodHashafrozenYod" instead of "HodHasharon" but if I do printf at my function printf->name I get the correct output of "HodHasharon"例如,当我打印pcity.name我得到"HodHashafrozenYod"而不是"HodHasharon"但是如果我在我的函数 printf->name 中执行 printf 我得到"HodHasharon"的正确输出

What am I doing wrong?我究竟做错了什么?

struct of City:城市结构:

typedef struct City
{
    char *name;
    char * popluarFood;
    int numberOfPeople;

} City;

the function:功能:

City * cutCityData (char *singleLine)
{
    City* pcity=(City*)malloc(sizeof(City));
    int firstIndex=1;
    int endIndex=1;
    int checkItarion=0;
    while(endIndex<strlen(singleLine))
    {//while
        while (singleLine[endIndex] != ',')
        {//while2
            endIndex++;
        }//while2
        checkItarion++;
        char cityDetails[endIndex - firstIndex +1];
        memcpy(cityDetails,&singleLine[firstIndex], endIndex);
        cityDetails[endIndex - firstIndex] = '\0';
        if (checkItarion == 1) {
            pcity->name = (char *) malloc(cityDetails);
            strcpy(&(pcity->name), cityDetails);
            endIndex++;
            firstIndex = endIndex;
        }
        if (checkItarion == 2) {
            pcity->popluarFood = (char *) malloc(cityDetails);
            strcpy(&(pcity->popluarFood), cityDetails);
            endIndex++;
            firstIndex=endIndex;
            break;
        }
    }//while
    char cityDetails[strlen(singleLine) - firstIndex + 1];
    memcpy(cityDetails, &singleLine[firstIndex], sizeof(singleLine-1));
    int resdints=atoi(cityDetails);
    pcity->numberOfPeople=resdints;
    return pcity;
    }

from main:从主要:

City* pCity=cutCityData(singLine);
printf("%s\n", &(pCity->name));

&(pcity->name) is the address of the pointer variable. &(pcity->name)是指针变量的地址。 You want to copy the string to the memory that it points to, not copy over the pointer.您想将字符串复制到它指向的内存中,而不是复制指针。 So change:所以改变:

strcpy(&(pcity->name), cityDetails);

to

strcpy(pcity->name, cityDetails);

You're also giving the wrong argument to malloc() .您还为malloc()提供了错误的参数。 cityDetails is an array, but the argument should be the number of bytes you want to allocate. cityDetails是一个数组,但参数应该是您要分配的字节数。 So change所以改变

pcity->name = (char *) malloc(cityDetails);

to:到:

pcity->name = malloc(strlen(cityDetails) + 1);

These changes also need to be made for the code that fills in pcity->popularFood as well.还需要对填充pcity->popularFood的代码进行这些更改。

This is wrong:这是错误的:

memcpy(cityDetails, &singleLine[firstIndex], sizeof(singleLine-1));

singleLine is a pointer, so sizeof(singleLine-1) is the number of bytes in a pointer, not the length of the string. singleLine是一个指针,所以sizeof(singleLine-1)是指针中的字节数,而不是字符串的长度。 This should be:这应该是:

memcpy(cityDetails, &singleLine[firstIndex], endIndex + 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM