Question about using C pointer array as a parameter

Asked 1 weeks ago, Updated 1 weeks ago, 0 views


#include <stdio.h>

int strO (char* myStr[]) {

    char str[] = "hello";


   myStr[0] = str; // < Part to be modified?


    return 0;
}

int main() {

    char* string_list[10] = {NULL};

    strO(string_list);

    printf("%s\n", string_list[0]);

    return 0;
}

In the strO function, we want to substitute the string indicated by str in string_list[0] and output it from main. (i.e., I want to print out "hello".)

However, the garbage value continues to be printed in the above code. Is there a way to get the result I want by modifying only the marked part?

c parameter pointer string function

2022-09-20 11:01

1 Answers

char str[] = "hello";

Please correct the code above as below.

static char str[] = "hello";


2022-09-20 11:01

If you have any answers or tips


© 2022 pinfo. All rights reserved.