Store string into array in c

后端 未结 6 1332
孤城傲影
孤城傲影 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:13

    Did not really understand what you need. But here is what I guessed.

    char *a[5];//array of five pointers
    
    for(i=0;i<5;i++)// iterate the number of pointer times in the array
    {
    char input[10];// a local array variable
    a[i]=malloc(10*sizeof(char)); //allocate memory for each pointer in the array here
    scanf("%s",input);//take the input from stdin
    strcpy(a[i],input);//store the value in one of the pointer in the pointer array
    }
    

提交回复
热议问题