strcpy()不会写新字符串

时间:2015-05-18 16:04:08

标签: c

在我的代码中,我想通过一个函数更新全局数组data的内容。但是,当我调用该函数时,data的内容不会改变,尽管函数调用strcpy()来实现该更改 - 之后,data仍然包含“prg”。 如何使用strcpy()或类似内容为数据写入新值?

char data[255] = "prg";

void process_tuple(Tuple *t)
{
    //Get key
    int key = t->key;

    //Get integer value, if present
    int value = t->value->int32;

    //Get string value, if present
    char string_value[32];
    strcpy(string_value, t->value->cstring);

    strcpy(data, "prg1212");

    //Decide what to do
    switch(key) {
        case key_0:

        break;
    };
}

static  WeatherAppDataPoint s_data_points[] = 
{
  {
    .city = data,
    .description = "surfboard :)",
    .icon = WEATHER_APP_ICON_GENERIC_WEATHER,
    .current = 110,
    .high = 120,
    .low = 100,
  },   
};

2 个答案:

答案 0 :(得分:0)

您错了,如果执行该功能,写入的程序将data更改为prg1212。您要么不调用它(错误事件连接等),所写的代码不准确(部分代码隐藏真正的问题)或在调试期间(或在错误的时间)评估错误的指针。

答案 1 :(得分:0)

I have figured out the problem with help from @Blidny and @MikeofSST It turns out that my code above contained in a file app_data.c wasn't running code outside if the functions created in app_data.h. I moved my code up into the void where my temperature values were formatted and written, and strcpy() worked like a charm.