Home > Software design >  How do I create an one-dimensional array of strings in C?
How do I create an one-dimensional array of strings in C?

Time:07-12

I ask this question here bc I haven't found a good one yet. I've seen some videos where a 2D array is created to store the strings, but I wanted to know if it is possible to make a 1D array of strings, thx.

CodePudding user response:

NOTE: In C, a string is an array of characters.

//string
char *s = "string";

//array of strings
char *s_array[] = {
        "array",
        "of",
        "strings"
};

Example

#include <stdio.h>


int main(void)
{
        const int ARR_LEN = 3;
        int i = 0;
        char *s_array[] = {
        "array",
        "of",
        "strings"
        };

        while (i < ARR_LEN)
        {
                printf("%s ", s_array[i]);
                i  ;
        }

        printf("\n");

        return (0);
}
  • Related