此代码似乎按预期工作,使用单个指针填充数字数组
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
int main(void)
{
int arr[4], count = 0, i;
char *p, s[32] = " \t 10, 15 \n ,20, 25 , ";
p = s;
do {
arr[count++] = (int)strtol(p, &p, 10);
while (isspace(*p) || *p == ',') p++;
} while (*p);
for (i = 0; i < count; i++) {
printf("%d\n", arr[i]);
}
return 0;
}
我的问题是:
在strtol中使用p作为param1(source)和&p作为param 2(第一个无效字符的地址)是否有效?
最佳答案
是的,它是安全的。第一个参数按值传递,所以 strtol
具有不受写入第二个参数的更改影响的本地副本。
关于c - strtol 重用参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13952015/