简体   繁体   English

将带符号的char数组传递给期望const char指针的函数会导致数组值出现乱码

[英]Passing a signed char array to function expecting const char pointer results in garbled array value

I cant seem to wrap my head around what is happening here: 我似乎无法绕过这里发生的事情:

I have a function that calculates the difference between two dates in seconds. 我有一个函数,可以以秒为单位计算两个日期之间的差异。 Then it gets bitshifted around to produce some output: 然后它会移位,以产生一些输出:

void GetUTC(unsigned char buffer[5])
{
    struct tm str_time;
    time_t start;
    time_t current = time(NULL);
    str_time.tm_year = 2010 - 1900;
    str_time.tm_mon = 1;
    str_time.tm_mday = 1;
    str_time.tm_hour = 0;
    str_time.tm_min = 0;
    str_time.tm_sec = 0;
    str_time.tm_isdst = 0;
    start = mktime(&str_time);

    printf("%s\n",ctime(&start));
    printf("%s\n\n",ctime(&current));

    uint32_t someInt = difftime(current,start);
    buffer[0] = 2;
    buffer[1] = (someInt & 0xff);
    someInt = someInt >> 8;
    buffer[2] = (someInt & 0xff);
    someInt = someInt >> 8;
    buffer[3] = someInt;
    someInt = someInt >> 8;
    buffer[4] = (10 & 0xff);
}

I feed it this array: 我将其输入此数组:

    unsigned char toWrite[5];

Then call it 然后叫它

    GetUTC(toWrite);

All goes well. 一切顺利。 Now I have a function I am trying to feed the new array into, which takes these parameters: 现在,我有一个函数正在尝试将新数组输入其中,这些函数需要以下参数:

void gatt_write_attribute(GDBusProxy *proxy, const char *arg)

And I call it with: 我这样称呼:

gatt_write_attribute(someProxy, toWrite);

However the array I passed to the function gatt_write_attribute shows that it's the jumbled garbage: $2\\20023\\n23 instead of my expected value of toWrite. 但是,我传递给函数gatt_write_attribute的数组显示这是混乱的垃圾: $2\\20023\\n23而不是我的toWrite预期值。 (numbers differ since it has to do with time): (数字不同,因为它与时间有关):

[0]: 2
[1]: 23
[2]: 54
[3]: 128
[4]: 10

I tried adding a terminating \\0 to the end of toWrite , but it did not change anything. 我尝试在toWrite的末尾添加一个终止符\\0 ,但是它没有任何改变。 I tried casting it to a const char pointer and that didn't work either. 我尝试将其强制转换为const char指针,但该方法也不起作用。

I feel like I'm missing a very simple detail. 我觉得我缺少一个非常简单的细节。 Could someone explain why I cant pass this char array to the gatt_write_attribute function? 有人可以解释为什么我不能将此char数组传递给gatt_write_attribute函数吗?

As noted by Ian Abbott and Chux in the comments to my original question, I was indeed being silly and was not realizing that my debugger was displaying the thing being pointed to as an array of characters instead of numbers. 正如Ian AbbottChux在对原始问题的评论中指出的那样,我确实很傻,没有意识到我的调试器将指向的内容显示为字符数组而不是数字。 This is what caused the garbled values. 这就是造成乱码的原因。

I should have probably allowed myself a bit more sleep in-between work hours. 在工作时间之间,我应该允许自己多睡一会。

You need to pass the array to the first function as a pointer. 您需要将数组作为指针传递给第一个函数。 Otherwise it just gets copied to the stack and the function writes to the new copy. 否则,它只会被复制到堆栈中,并且该函数将写入新副本。 The garbage is probably there because it has not been initialised. 垃圾可能在那里,因为它尚未初始化。

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

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