C 二维指针数组到二维数组
我怎样才能转换C 二维指针数组到二维数组,c,arrays,string,C,Arrays,String,我怎样才能转换 char *s[]={ "to err is human", "but to really mess things up ", "one needs to know c!!" }; 到 实际上已经是了,只需删除[]的[3]下标: #include <stdio.h> int main (void) { char s[][50]={ "to err
char *s[]={
"to err is human",
"but to really mess things up ",
"one needs to know c!!"
};
到
实际上已经是了,只需删除[]的[3]下标:
#include <stdio.h>
int main (void) {
char s[][50]={
"to err is human",
"but to really mess things up ",
"one needs to know c!!"
};
int i = 0;
for (i = 0; i < 3; i++)
printf ("s [%d] %s\n", i, s[i]);
return 0;
}
您原来的
char*s[]={
也可以使用。无需记住数组的大小
#include <stdio.h>
#include <string.h>
int main (void) {
char *s[]={
"to err is human",
"but to really mess things up ",
"one needs to know c!!",
'\0'
};
int i = 0;
char SA[100][100];
while (s[i] != '\0') {
strcpy(SA[i],s[i]);
i++;
}
SA[i]='\0'
printf("SA[1][1] : %c\n",SA[1][1]) ;
return 0;
}
#包括
#包括
#包括
内部主(空){
char*s[]={
“犯错是人”,
“但是真的把事情搞砸了”,
“一个人需要知道c!!”
};
int i,尺寸=尺寸/尺寸(*);
char ns[size][50];//或使用malloc,例如下一行
//char(*ns)[50]=calloc(大小、大小(char[50]);
对于(i=0;i您不清楚给定声明的哪一部分?您尝试过什么?另外,还不清楚您所说的转换是什么意思。您是想在运行时将第一个变量中的数据复制到第二个变量中,还是试图重写代码但无法使其工作?我想在您尝试这样做后在运行时复制数据。当您只需要150字节时,无需分配100000字节。当然,除非您喜欢堆栈溢出。@Lundin截至目前为止,我可以看到他没有要求内存限制。如果是这种情况,我只需稍微修改代码。我的期望是,编码器不必为输出数组的大小而烦恼。
s [0] to err is human
s [1] but to really mess things up
s [2] one needs to know c!!
#include <stdio.h>
#include <string.h>
int main (void) {
char *s[]={
"to err is human",
"but to really mess things up ",
"one needs to know c!!",
'\0'
};
int i = 0;
char SA[100][100];
while (s[i] != '\0') {
strcpy(SA[i],s[i]);
i++;
}
SA[i]='\0'
printf("SA[1][1] : %c\n",SA[1][1]) ;
return 0;
}
SA[1][1] : u (as expected)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void){
char *s[]={
"to err is human",
"but to really mess things up ",
"one needs to know c!!"
};
int i, size = sizeof(s)/sizeof(*s);
char ns[size][50];//or use malloc, E.g next line
//char (*ns)[50] = calloc(size, sizeof(char[50]));
for(i=0;i<size;++i){
memset(ns[i], 0, 50);//unnecessary if you use the calloc
strcpy(ns[i], s[i]);
//printf("%s\n", ns[i]);
}
/*
char ns[3][50]= {
"to err is human",
"but to really mess things up ",
"one needs to know c!!"
};
*/
return 0;
}