简体   繁体   English

如何访问作为参数传递给c中的函数的char数组的元素?

[英]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.

相关问题 如何在函数内部操作char数组(字符串)的指针,它在c / c ++中作为参数传递 - how to manipulate a pointer of char array (string) inside a function where it is passed as a parameter in c / c++ 如何修改在 function 中作为参数传递的 char 指针? - How to modify a char pointer passed as parameter in a function? 作为函数参数传递时如何访问`int`动态数组的数据? - How to access data of dynamic array of `int` when passed as function parameter? 如何释放在C中作为函数参数传递的字符数组 - How to free array of characters passed as function parameter in C 从传递给符合MISRA规则17.4的函数的void指针参数访问所有数组元素 - Access to all array elements from a void pointer parameter passed to a function compliant with MISRA Rule 17.4 如何将Swift字符串数组传递给带有char **参数的C函数 - How to pass an array of Swift strings to a C function taking a char ** parameter 如何将char数组作为参数传递给C中的函数? - How can I pass a char array as a parameter to a function in C? 如何清除传递给C中的函数的char *? - How to clear a char* passed to a function in C? a和&a在C中作为函数参数传递的数组不同 - a and &a differs for an array passed as a function parameter in C 将C数组作为char *函数参数传递 - Passing C array as char* function parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM