c - strcpy 和 printf 多维字符数组 C

标签 c printf arrays strcpy

假设我有一个数组

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

strcpy 的正确语法是什么,以便将数据放入字符串之一(最内层的 char 数组,大小为 50),然后相应的约定通过 %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...

这样输出

Message 1 part 1, m2 p2

m2, p2

m3, p3

and so on

最佳答案

我刚刚编写了一个快速程序来显示您所询问的事情...在声明时将它们加载,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;
}

关于c - strcpy 和 printf 多维字符数组 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12051187/

相关文章:

php sprintf 用一个值替换多个占位符

c#计算数组内的整数

创建指向指针数组的指针

arrays - 什么是一致数组?

使用 MATLAB Engine 连接到已在运行的 MATLAB

c - 为什么百分比字符在 C 中没有用反斜杠转义?

c - C 中的分层链接

c - 将从客户端发送到服务器的数据保存到文件中时包含无效字符

c - 格式说明符如何知道要打印多少字节?

matlab - 在 matlab 中打印选择性迭代