2D character array initialization in C

前端 未结 4 1486
臣服心动
臣服心动 2021-02-03 10:23

I am trying to build a list of strings that I need to pass to a function expecting char **

How do I build this array? I want to pass in two options, each wi

4条回答
  •  天涯浪人
    2021-02-03 10:45

    I think what you originally meant to do was to make an array only of characters, not of pointers:

    char options[2][100];
    
    options[0][0]='t';
    options[0][1]='e';
    options[0][2]='s';
    options[0][3]='t';
    options[0][4]='1';
    options[0][5]='\0';  /* NUL termination of C string */
    
    /* A standard C library function which copies strings. */
    strcpy(options[1], "test2");
    

    The code above shows two distinct methods of setting the character values in memory you have set aside to contain characters.

提交回复
热议问题