2

说我有一个数组

char messages[10][2][50];

strcpy 的正确语法是什么,以便将数据放入其中一个字符串(大小为 50 的最内层 char 数组),然后通过 %s 将其提供给 printf 的相应约定?

就此而言,我是否以正确的顺序声明数组下标?它打算是 10 对(2 个)字符串。每个字符串为 50 个字符宽。

01{{50 chars},{50 chars}}
02{{50 chars},{50 chars}}
...
09{{50 chars},{50 chars}}
10{{50 chars},{50 chars}}

各种互联网资源似乎在省略哪个下标方面存在冲突,而且无论我尝试什么似乎都会产生意想不到的结果。

例如,你能在下面填空吗

strcpy(message???, "Message 1 Part 1");
strcpy(message???, "m1 p2");
strcpy(message???, "m2 p1");
strcpy(message???, "m2 p2");
strcpy(message???, "m3 p1");
strcpy(message???, "m3 p1");
//So on...

int i;
for(i=0;i<10;i++)
    printf("%s, %s\n", message???, message???);

使得数组具有以下结构并保持:

01{{"Message 1 Part 1\0"},{"m1 p2\0"}}
02{{"m2 p1\0"},{"m2 p2\0"}}
01{{"m3 p1\0"},{"m3 p2\0"}}
//So on...

和这样的输出

消息 1 第 1 部分,m2 p2

平方米,p2

m3, p3

等等

4

5 回答 5

1

我刚刚编写了一个快速程序来显示您所询问的内容...在声明时加载它们,将它们 strncpy 放入其中一个,然后将它们打印出来。

希望能帮助到你

编辑:我有点讨厌魔术数字,所以我几乎完全删除了它们
编辑:我添加了替代品 Tommi Kyntola,我在评论中谈论

#include <stdio.h>
#include <string.h>

// safe string copy macro, terminates string at end if necessary
// note: could probably just set the last char to \0 in all cases
// safely if intending to just cut off the end of the string like this

#define sstrcpy(buf, src, size) strncpy(buf, src, size); if(strlen(src) >= size) buf[size-1] = '\0';

#define MSGLIMIT 10
#define MSGLENGTH 30
#define MSGFIELDS 2
#define MSGNAME 0
#define MSGTEXT 1


int main(void) {
    char messages[MSGLIMIT][MSGFIELDS][MSGLENGTH] = { {"bla", "raa"},
                                                      {"foo", "bar"}
                                                    };
    int i;

    char *name1 = "name16789012345678901234567890";
    char *text1 = "text16789012345678901234567890";

    char *name2 = "name26789012345678901234567890";
    char *text2 = "text26789012345678901234567890";

    char *name3 = "name36789012345678901234567890";
    char *text3 = "text36789012345678901234567890";


    // doesn't set last char to \0 because str overruns buffer
    // undocumented result of running this, but likely to just get the name2 string
    // as that'll be the very next thing in memory on most systems

    strncpy(messages[2][MSGNAME], name1, MSGLENGTH); // 2 because it's the next empty one
    strncpy(messages[2][MSGTEXT], text1, MSGLENGTH);

    // alternative suggested by Tommi Kyntola
    // printf family are more complicated and so cost more cpu time than strncpy
    // but it's quick and easy anywhere you have string.h and fine most of the time

    snprintf(messages[3][MSGNAME], MSGLENGTH, "%s", name2);
    snprintf(messages[3][MSGTEXT], MSGLENGTH, "%s", text2);

    // uses the define macro at the top of the page to set the last char to \0 if
    // otherwise not set by strncpy, adds a little weight but still the better option
    // if performance of this section of code is important

    sstrcpy(messages[4][MSGNAME], name3, MSGLENGTH);
    sstrcpy(messages[4][MSGTEXT], text3, MSGLENGTH);


    for(i = 0; i < 5; i++) // 5 because that's how many I've populated
            printf("%s : %s\n", messages[i][MSGNAME], messages[i][MSGTEXT]);

    return 0;
}
于 2012-08-21T08:58:44.183 回答
0

您可以省略最大订阅(例如,它是 10),因为它可以由编译器根据剩余订阅计算。要传递 50 个元素的层,请使用指针: (*messages)[10][2] - 将是 50 个元素的层上的指针

于 2012-08-21T08:49:41.290 回答
0

我会使用:

假设你想复制到char* new_buff

memcpy(new_buff, messages, 10*2*50);

您可以执行 3 个嵌套循环并使用strncpy.

不要使用 strcpy ......它是不安全的

于 2012-08-21T08:50:08.247 回答
0

正如已经指出的那样,最好使用 strncpy,或者如下面的示例,使用断言,以防止可能的缓冲区溢出。strcpy 与 strncpy 相比,性能略有提高。

    #define FIRST_OF_PAIR 0
#define SECOND_OF_PAIR 1

    int message_num = 7;

    char messages[10][2][50];

    char *string = "hello";

    assert(strlen(string) < 50);
    assert(message_num > 0 &&  message_num < 10);

    strcpy(messages[message_num][SECOND_OF_PAIR], "Hello");

    printf("%s", messages[message_num][SECOND_OF_PAIR]);
于 2012-08-21T09:00:33.673 回答
0

这将是

strcpy(message[0][0], "Message 1 Part 1");
strcpy(message[0][1], "m1 p2");
strcpy(message[2][0], "m2 p1");
strcpy(message[2][1], "m2 p2");
strcpy(message[3][0], "m3 p1");
strcpy(message[3][1], "m3 p2");


for(i=0;i<10;i++)
    printf("%s, %s\n", message[i][0], message[i][1]);

尝试理解这个概念。

于 2012-08-21T09:00:43.877 回答