c - 为什么使用 & 运算符时 char* 和 char[x] 之间的区别很重要
问题描述
以下代码将出现段错误。
char* input = "12.34"; //< this is only to simplify the example.
char buffer[30] = { 0 };
memcpy(buffer, input, strlen(input));
char* part1 = strsep(&buffer, ".");
下面的代码不会。
char* input = "12.34"; //< this is only to simplify the example.
char buffer[30] = { 0 };
memcpy(buffer, input, strlen(input));
char* ptr = buffer; //< Only diff.
char* part1 = strsep(&ptr , ".");
当通过引用 ( &
) 作为函数参数传递时,为什么 和 之间的区别char**
很char*[30]
重要?
解决方案
如果您查看 man for strsep,它需要双指针,因为它试图分配指针。
"char *strsep(char **stringp, const char *delim);
...
and *stringp is updated to point past the token"
基本上,如果你有一个数组,你不能只告诉数组的头在一个新的地方。但是,您可以创建指向该数组中任何元素的指针,然后更改指针的值(如果您愿意,可以使其指向不同的元素)。
推荐阅读
- react-native - react原生屏幕的布局如何设计?
- linux - 基础文件更改时如何同步环回设备
- linux - 从失败的脚本执行脚本
- python - ImportError: No module named future 尽管已经安装
- excel - VBA 从一个 Word Doc 复制到另一个 Excel
- reactjs - CRA 应用程序在生产构建后不运行?
- python - MySQL/Python 中的“TypeError:格式字符串的参数不足”
- php - 无法移动上传的文件
- database - 工作流数据库结构
- node.js - 如何在没有 dotenv 的情况下运行项目?