输出C编程中的双数组串互连
我有以下代码:输出C编程中的双数组串互连,c,arrays,string,C,Arrays,String,我有以下代码: #include <stdio.h> int main(void) { char p[5][5]; int i,j; for(i=1;i<=1;i++){ for(j=1;j<=2;j++) { printf("\nInput Product code %d of day %d: ", j,i); scanf("%s", &p[i][j]);
#include <stdio.h>
int main(void) {
char p[5][5];
int i,j;
for(i=1;i<=1;i++){
for(j=1;j<=2;j++) {
printf("\nInput Product code %d of day %d: ", j,i);
scanf("%s", &p[i][j]);
}
}
for(i=1;i<=1;i++){
for(j=1;j<=2;j++) {
printf("\n\tProduct code %s day %d", &p[i][j],i);
}
}
}
有人知道为什么hello和hi相互连接而不是在第一个中打印hello吗
我对弦做了很多研究,但没有一个比这个更有效
char p[5][5];
这是一个字符矩阵,而不是字符串矩阵
使用scanf()
,您希望为矩阵中的每个元素添加一个字符串。这是不可能的
您必须使用以下定义之一:
(一)
而scanf应该是
scanf("%s", p[i][j]);
scanf("%ms", p[i][j]);
(二)
而scanf应该是
scanf("%s", p[i][j]);
scanf("%ms", p[i][j]);
p[i][j]
这里指向由scanf()
动态分配的内存,当p[i][j]
在您的程序中变得无用时,您必须使用free(p[i][j])
释放它,原因是字符串只是字符数组(以\0
或null
终止。现在,您正在索引字符,而scanf
只是获取您传递的字符引用,并用它填充结构的其余部分,直到用字符串填充完为止
你要做的是把p设为
* * * * *
* h e l l
o\0 * * *
* * * * *
* * * * *
然后
* * * * *
* h h i\0
o\0 * * *
* * * * *
* * * * *
如果我是你,我会更靠近这样的东西
char p[5][6];
int i,j;
for(i=0;i<2;i++)
{
printf("\nInput Product code of day %d: ", i);
scanf("%s", &p[i]);
}
for(i=0;i<2;i++)
{
printf("%s\n", &p[i]);
}
charp[5][6];
int i,j;
for(i=0;ip[][]
不是字符串的双数组,而是字符的双数组。它有5行5列,如下所示:
p[row][column] =
Row| Column -> |
v | 0 | 1 | 2 | 3 | 4 |
| 0 | | | | | |
| 1 | | | | | |
| 2 | | | | | |
| 3 | | | | | |
| 4 | | | | | |
第一个scanf()
在i=1,j=1时被调用,并复制字符串“hello”,留下p[][]
如下(“\0”是终止字符串的NUL字符):
下次调用时,i=1,j=2:
| 0 | 1 | 2 | 3 | 4 |
| 0 | | | | | |
| 1 | | H | h | i |\0 |
| 2 | o |\0 | | | |
| 3 | | | | | |
| 4 | | | | | |
你可以这样写:
#include <stdio.h>
#define MAX_CODE_LEN 80 /* or something */
int main(void)
{
char *code[5][5] = {0},
currentCode[MAX_CODE_LEN] = {0};
int day = 0,
codeNum = 0;
/* start loops at 0 */
for ( day = 0; day < 5; day++ ) {
for ( codeNum = 0; codeNum < 5; codeNum++ ) {
int len = 0;
printf("\nInput Product code %d of day %d: ", codeNum, day);
scanf("%s", currentCode);
/* len doesn't include NUL but malloc needs it */
len = strlen(currentCode);
/* yoda style so compiler catches assignment instead of equality */
if ( 0 == len ) {
/* if the user didn't enter a code move onto the next day */
code[day][codeNum] = NULL;
break;
}
len = len >= MAX_CODE_LEN? MAX_CODE_LEN - 1: len;
code[day][codeNum] = malloc(len * sizeof(char) + 1);
strcpy(code[day][codeNum], currentCode);
}
}
for ( day = 0; day < 5; day++ ) {
for ( codeNum = 0; codeNum < 5; codeNum++ ) {
if ( NULL == code[day][codeNum] ) {
/* no more codes for today */
break;
}
printf("\n\tProduct code %s day %d", code[day][codeNum], day);
}
}
return 0;
}
#包括
#定义MAX_CODE_LEN 80/*或其他*/
内部主(空)
{
字符*代码[5][5]={0},
currentCode[MAX_CODE_LEN]={0};
整数天=0,
codeNum=0;
/*从0开始循环*/
对于(天=0;天<5;天++){
对于(codeNum=0;codeNum<5;codeNum++){
int len=0;
printf(“\n输入第%d天的产品代码%d:”,codeNum,day);
scanf(“%s”,当前代码);
/*len不包括NUL,但malloc需要它*/
len=strlen(当前代码);
/*yoda样式,因此编译器捕获赋值而不是相等*/
如果(0==len){
/*如果用户没有输入代码,则转到第二天*/
代码[天][codeNum]=空;
打破
}
len=len>=MAX\u CODE\u len?MAX\u CODE\u len-1:len;
代码[天][codeNum]=malloc(len*sizeof(char)+1);
strcpy(代码[天][codeNum],当前代码);
}
}
对于(天=0;天<5;天++){
对于(codeNum=0;codeNum<5;codeNum++){
如果(NULL==code[day][codeNum]){
/*今天没有更多的代码*/
打破
}
printf(“\n\t产品代码%s天%d”,代码[天][codeNum],天);
}
}
返回0;
}
for(i=1;iyep..确保它第一次工作,我不想浪费任何宝贵的时间。我认为这是偏离目标的。不需要在数组中添加第三维以使代码工作。这并不是那么简单:p[1][0]
没有特定的值;p[1][1]
包含hello
的h
;p[1][2]
包含hi
的h
,等等。hello
的条目超出子数组p[1]
,写入p[2][0]
和p[2][1]
。但是,建议的修复(包括空值的额外空间)大致正确。@JonathanLeffler没有考虑j
变量上1处的索引
| 0 | 1 | 2 | 3 | 4 |
| 0 | | | | | |
| 1 | | H | h | i |\0 |
| 2 | o |\0 | | | |
| 3 | | | | | |
| 4 | | | | | |
#include <stdio.h>
#define MAX_CODE_LEN 80 /* or something */
int main(void)
{
char *code[5][5] = {0},
currentCode[MAX_CODE_LEN] = {0};
int day = 0,
codeNum = 0;
/* start loops at 0 */
for ( day = 0; day < 5; day++ ) {
for ( codeNum = 0; codeNum < 5; codeNum++ ) {
int len = 0;
printf("\nInput Product code %d of day %d: ", codeNum, day);
scanf("%s", currentCode);
/* len doesn't include NUL but malloc needs it */
len = strlen(currentCode);
/* yoda style so compiler catches assignment instead of equality */
if ( 0 == len ) {
/* if the user didn't enter a code move onto the next day */
code[day][codeNum] = NULL;
break;
}
len = len >= MAX_CODE_LEN? MAX_CODE_LEN - 1: len;
code[day][codeNum] = malloc(len * sizeof(char) + 1);
strcpy(code[day][codeNum], currentCode);
}
}
for ( day = 0; day < 5; day++ ) {
for ( codeNum = 0; codeNum < 5; codeNum++ ) {
if ( NULL == code[day][codeNum] ) {
/* no more codes for today */
break;
}
printf("\n\tProduct code %s day %d", code[day][codeNum], day);
}
}
return 0;
}