gpt4 book ai didi

c - 二维指针数组到二维数组

转载 作者:行者123 更新时间:2023-11-30 19:46:15 25 4
gpt4 key购买 nike

如何转换

char *s[]={
"to err is human",
"but to really mess things up ",
"one needs to know c!!"
};

char s[3][50]={
"to err is human",
"but to really mess things up ",
"one needs to know c!!"
};

最佳答案

#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;
}

关于c - 二维指针数组到二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24278377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com