问题描述
说我有一个数组
字符消息[10] [2] [50];
strcpy的正确语法是什么,以便将数据放入字符串之一(字符串的最里面的char数组)大小为50),然后通过%s将其提供给printf的相应约定?
为此,我是否以正确的顺序声明数组下标?它打算是10对,每对(共2个)字符串。每个字符串的宽度为50个字符。
01 {{50 chars},{50 chars}}
02 {{50 chars}, {50个字符}}
...
09 {{50个字符},{50个字符}}
10 {{50个字符},{50个字符}}
各种互联网资源似乎在忽略哪个下标上发生冲突,而我所做的任何尝试似乎都会产生意想不到的结果。
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);
//等等...
int i;
for(i = 0; i< 10; i ++)
printf(%s,%s\n,message ???,message ???);
这样的数组具有并保存的结构:
01 {{消息1第1部分\0&},{ m1 p2\0&}}
02 {{ m2 p1\0&},{ " m2 p2\0"}}
01 {{ m3 p1\0"},{ m3 p2\0"}}
//等等...
这样输出
我刚刚编写了一个快速程序,以显示您所要求的内容...将其加载到声明,strncpy放入其中之一,然后将其打印出来。
希望它会有所帮助
编辑:一种讨厌的魔术数字,所以我几乎完全删除了它们
编辑:我添加了Tommi Kyntola替代品,而我在评论中正在谈论
#include< stdio.h>
#include< string.h>
//安全的字符串复制宏,必要时在结尾处终止字符串
//注意:在所有情况下都可以将最后一个字符设置为\0
//安全如果打算像这样切断字符串的结尾
#define sstrcpy(buf,src,size)strncpy(buf,src,size); if(strlen(src)> = size)buf [size-1] =‘\0’;
#定义MSGLIMIT 10
#定义MSGLENGTH 30
#定义MSGFIELDS 2
#定义MSGNAME 0
#定义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;
//未将最后一个char设置为\0,因为str溢出缓冲区
//运行此文件的未记录结果,但可能只是得到name2字符串
//因为这将是大多数系统中内存中的下一件事
strncpy(messages [2] [MSGNAME],name1,MSGLENGTH); // 2,因为它是下一个空的
strncpy(messages [2] [MSGTEXT],text1,MSGLENGTH);
// Tommi Kyntola建议的替代方案
// printf家族比strncpy
//复杂得多,因此花费了更多的CPU时间,但是在任何有字符串的地方它都是快速简便的。 h并且在大多数情况下都很好
snprintf(messages [3] [MSGNAME],MSGLENGTH,%s,name2);
snprintf(messages [3] [MSGTEXT],MSGLENGTH,%s,text2);
//使用页面顶部的define宏,如果
//将最后一个字符设置为\0 //否则不是由strncpy设置的,虽然增加了一些权重但还是更好选项
//如果这部分代码的性能很重要
sstrcpy(messages [4] [MSGNAME],name3,MSGLENGTH);
sstrcpy(messages [4] [MSGTEXT],text3,MSGLENGTH);
for(i = 0; i< 5; i ++)// 5,因为这是我填充了
printf(%s:%s\ n,消息[i] [MSGNAME],消息[i] [MSGTEXT]);
返回0;
}
Say I have an array
char messages[10][2][50];
What is the correct syntax for strcpy, in order to get the data into one of the strings (inner most char array of size 50) and then the corresponding convention to supply it to printf via %s?
For that matter, am I declaring the array subscripts in the correct order? It is intended to be 10 lots of, pairs (of 2) strings. Each string being 50 chars wide.
01{{50 chars},{50 chars}}
02{{50 chars},{50 chars}}
...
09{{50 chars},{50 chars}}
10{{50 chars},{50 chars}}
Various internet sources seem to conflict on which subscript to omit and, whatever I try seems to produce unintended results.
e.g. Could you fill in the blanks to the following
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???);
Such that the array has a structure of and holds:
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...
And outputs as such
I just wrote a quick program to show the things you've asked about... loading them up at declaration, strncpy into one of them, and then printing them out.
Hope it helps
edit: I kind of hate magic numbers so I almost totally removed them
edit: I've added alternatives Tommi Kyntola and I were talking about in the comments
#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;
}
这篇关于strcpy和printf多维char数组C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!