Store string into array in c

后端 未结 6 1333
孤城傲影
孤城傲影 2021-02-14 21:28

As i know, i can create an array with item inside such as:

char *test1[3]= {\"arrtest\",\"ao\", \"123\"};

but how can i store my input into arr

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-14 22:07

    Use a 2-dimensional array char input[3][10];
    or
    an array of char pointers (like char *input[3];) which should be allocated memory dynamically before any value is saved at those locations.

    First Case, take input values as scanf("%s", input[0]);, similarly for input[1] and input[2]. Remember you can store a string of max size 10 (including '\0' character) in each input[i].

    In second case, get input the same way as above, but allocate memory to each pointer input[i] using malloc before. Here you have flexibility of size for each string.

提交回复
热议问题