[英]How to access elements of char array passed as a parameter to a function in c?
I have this function which receives a pointer to char array and initializes it to be len
repetitions of "a": 我有这个函数,它接收到一个指向char数组的指针,并将其初始化为
len
的“ a”重复:
void test(char ** s, int len) {
*s = (char *)malloc(sizeof(char) * len);
int i;
for(i = 0; i < len; i++) {
*(s[i]) = 'a';
}
printf("%s\n", *s);
}
in the main()
I have this code: 在
main()
我有以下代码:
char * s;
test(&s, 3);
but I get EXC_BAD_ACCESS (code=1, address=0x0)
error when I run main()
. 但是当我运行
main()
时出现EXC_BAD_ACCESS (code=1, address=0x0)
错误。 The error occurs on the second iteration of the for loop in this line: *(s[i]) = 'a';
错误发生在该行的for循环的第二次迭代中:
*(s[i]) = 'a';
As far as I understand I'm not accessing the elements correctly, what is the correct way? 据我了解,我没有正确访问元素,正确的方法是什么?
s
is declared as a pointer to a pointer. s
被声明为指向指针的指针。 In reality, it's a pointer to a pointer to the start of an array, but that cannot be inferred from the type system alone. 实际上,它是一个指向数组开头的指针,但是不能仅从类型系统中推断出来。 It could just as well be a pointer to a start of an array of pointers, which is how
s[i]
treats it. 它也可以是指向指针数组开始的指针,这就是
s[i]
对待它的方式。 You need to first derefence s
(to get the pointer to the array's start), and then index on it: 您需要首先取消引用
s
(以获取指向数组起点的指针),然后对其进行索引:
(*s)[i] = 'a';
Also, as @MFisherKDX correctly pointed out in comments, if you're going to pass *s
to printf
or any other standard string-manipulation function, you have to turn into a proper C string by terminating it with a 0 character. 另外,正如@MFisherKDX在注释中正确指出的那样,如果要将
*s
传递给printf
或任何其他标准的字符串操作函数,则必须通过以0字符终止将其转换为正确的C字符串。
This more clearly shows what you should be doing, while staying close to your original code: 这更清楚地显示了您应该做什么,同时又保持原始代码的位置:
void test(char ** s, int len) {
char *p = malloc(len);
int i;
for(i = 0; i < len; i++) {
p[i] = 'a';
}
printf("%s\n", p);
*s = p;
}
Note that sizeof(char)
is always one by definition, and in C there's no need to cast the result of malloc()
. 请注意,按定义,
sizeof(char)
始终为1,在C语言中,无需malloc()
的结果。
That code also has all your original problems in that it doesn't actually create a string that you can send to printf( "%s... )
. This fixes that problem: 该代码还存在您所有的原始问题,因为它实际上并未创建可以发送给
printf( "%s... )
的字符串。这可以解决该问题:
void test(char ** s, int len) {
char *p = malloc(len+1);
int i;
for(i = 0; i < len; i++) {
p[i] = 'a';
}
p[i]='\0';
printf("%s\n", p);
*s = p;
}
And this is even easier, with no need to use a double- *
pointer: 而且这甚至更容易,不需要使用双
*
指针:
char *test(int len) {
char *p = malloc(len+1);
int i;
for(i = 0; i < len; i++) {
p[i] = 'a';
}
p[i]='\0';
printf("%s\n", p);
return(p);
}
Instead of assignment 代替分配
*(s[i]) = 'a';
use this: 用这个:
(*s)[i] = 'a';
But do not forget that C/C++ strings are null terminated 但是请不要忘记C / C ++字符串以null结尾
Or you can use this approach to get more readable code: 或者,您可以使用这种方法来获得更具可读性的代码:
void test(char** s, int len) {
// 1 char extra for zero
char *str = (char*)malloc(sizeof(char) * (len+1));
int i;
for(i = 0; i < len; i++)
str[i] = 'a';
// zero terminated string
str[len] = 0;
printf("%s\n", str);
*s = str;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.