How do I create an array of strings in C?

前端 未结 14 2533
野的像风
野的像风 2020-11-22 14:43

I am trying to create an array of strings in C. If I use this code:

char (*a[2])[14];
a[0]=\"blah\";
a[1]=\"hmm\";

gcc gives me \"warning:

14条回答
  •  逝去的感伤
    2020-11-22 15:13

    hello you can try this bellow :

     char arr[nb_of_string][max_string_length]; 
     strcpy(arr[0], "word");
    

    a nice example of using, array of strings in c if you want it

    #include 
    #include 
    
    
    int main(int argc, char *argv[]){
    
    int i, j, k;
    
    // to set you array
    //const arr[nb_of_string][max_string_length]
    char array[3][100];
    
    char temp[100];
    char word[100];
    
    for (i = 0; i < 3; i++){
        printf("type word %d : ",i+1);
        scanf("%s", word);
        strcpy(array[i], word);
    }
    
    for (k=0; k<3-1; k++){
        for (i=0; i<3-1; i++)
        {
            for (j=0; j array[i+1][j])
                {
                    strcpy(temp, array[i+1]);
                    strcpy(array[i+1], array[i]);
                    strcpy(array[i], temp);
    
                    j = 999;
                }
    
                // if a letter ascii code is smaller we stop
                if (array[i][j] < array[i+1][j])
                {
                        j = 999;
                }
    
            }
        }
    }
    
    for (i=0; i<3; i++)
    {
        printf("%s\n",array[i]);
    }
    
    return 0;
    }
    

提交回复
热议问题