How do I create an array of strings in C?

前端 未结 14 2521
野的像风
野的像风 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:06

    In ANSI C:

    char* strings[3];
    strings[0] = "foo";
    strings[1] = "bar";
    strings[2] = "baz";
    
    0 讨论(0)
  • 2020-11-22 15:13

    Or you can declare a struct type, that contains a character arry(1 string), them create an array of the structs and thus a multi-element array

    typedef struct name
    {
       char name[100]; // 100 character array
    }name;
    
    main()
    {
       name yourString[10]; // 10 strings
       printf("Enter something\n:);
       scanf("%s",yourString[0].name);
       scanf("%s",yourString[1].name);
       // maybe put a for loop and a few print ststements to simplify code
       // this is just for example 
     }
    

    One of the advantages of this over any other method is that this allows you to scan directly into the string without having to use strcpy;

    0 讨论(0)
  • 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 <stdio.h>
    #include <string.h>
    
    
    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<strlen(array[i]); j++)
            {
                // if a letter ascii code is bigger we swap values
                if (array[i][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;
    }
    
    0 讨论(0)
  • 2020-11-22 15:13
    char name[10][10]
    int i,j,n;//here "n" is number of enteries
    printf("\nEnter size of array = ");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        for(j=0;j<1;j++)
        {
            printf("\nEnter name = ");
            scanf("%s",&name[i]);
        }
    }
    //printing the data
    for(i=0;i<n;i++)
    {
        for(j=0;j<1;j++)
        {
            printf("%d\t|\t%s\t|\t%s",rollno[i][j],name[i],sex[i]);
        }
        printf("\n");
    }
    

    Here try this!!!

    0 讨论(0)
  • 2020-11-22 15:14

    A good way is to define a string your self.

    #include <stdio.h>
    typedef char string[]
    int main() {
        string test = "string";
        return 0;
    }
    

    It's really that simple.

    0 讨论(0)
  • 2020-11-22 15:16

    There are several ways to create an array of strings in C. If all the strings are going to be the same length (or at least have the same maximum length), you simply declare a 2-d array of char and assign as necessary:

    char strs[NUMBER_OF_STRINGS][STRING_LENGTH+1];
    ...
    strcpy(strs[0], aString); // where aString is either an array or pointer to char
    strcpy(strs[1], "foo");
    

    You can add a list of initializers as well:

    char strs[NUMBER_OF_STRINGS][STRING_LENGTH+1] = {"foo", "bar", "bletch", ...};
    

    This assumes the size and number of strings in the initializer match up with your array dimensions. In this case, the contents of each string literal (which is itself a zero-terminated array of char) are copied to the memory allocated to strs. The problem with this approach is the possibility of internal fragmentation; if you have 99 strings that are 5 characters or less, but 1 string that's 20 characters long, 99 strings are going to have at least 15 unused characters; that's a waste of space.

    Instead of using a 2-d array of char, you can store a 1-d array of pointers to char:

    char *strs[NUMBER_OF_STRINGS];
    

    Note that in this case, you've only allocated memory to hold the pointers to the strings; the memory for the strings themselves must be allocated elsewhere (either as static arrays or by using malloc() or calloc()). You can use the initializer list like the earlier example:

    char *strs[NUMBER_OF_STRINGS] = {"foo", "bar", "bletch", ...};
    

    Instead of copying the contents of the string constants, you're simply storing the pointers to them. Note that string constants may not be writable; you can reassign the pointer, like so:

    strs[i] = "bar";
    strs[i] = "foo"; 
    

    But you may not be able to change the string's contents; i.e.,

    strs[i] = "bar";
    strcpy(strs[i], "foo");
    

    may not be allowed.

    You can use malloc() to dynamically allocate the buffer for each string and copy to that buffer:

    strs[i] = malloc(strlen("foo") + 1);
    strcpy(strs[i], "foo");
    

    BTW,

    char (*a[2])[14];
    

    Declares a as a 2-element array of pointers to 14-element arrays of char.

    0 讨论(0)
提交回复
热议问题