c - 在 C 中构建二维字符串数组的引用运算符

标签 c string

以下代码输出段错误:11

 #include<stdio.h>
 #include<string.h>
 int main(){
 char **total;
 strcpy(*(total+1),"hello");
 printf("%s",*(total+1));
 }

但是下面的(当没有 **Array 初始化时)

#include<stdio.h>
#include<string.h>
int main(){
char total[3][100];
strcpy(total[1],"hello");
printf("%s",total[1]);
} 

那么第一个代码到底出了什么问题呢?是由于初始化步骤还是 strcpy 函数导致的?

最佳答案

使用chartotal[3][100]自动为数组保留一部分称为total的内存。 使用 char **total 您必须为数组分配内存。

关于c - 在 C 中构建二维字符串数组的引用运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43436691/

相关文章:

java - 如何将十进制转换为十六进制?

c++ - 如何在 C++ 中显示变量

c - 测量一组指令的准确时间(周期数)

c - 如何检测C二维数组的溢出?

c - 为什么程序有时会出现 "skip over"printfs?

java - 无法在原始类型 int 上调用 toString()

JAVA关于String的对象转义

java - 如何解码Java字符串中的unicode char?

返回 Vec<&str> 时字符串的生命周期

java - 数字解析库