Thread: creating array of strings in C

  1. #1
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87

    creating array of strings in C

    Hello. I study C.

    How to create array of strings in C?

    Let's say that I intend to have three strings:

    Code:
    t[0]='string number one content'
    t[1]='string number two content'
    t[3]='string number three content'
    I know how to make one string:

    Code:
    char u[30];
    which defines a string of length 30 (from 0 to 29). Now should I create an array of arrays?

    I have found this, but I do not understand it:

    How do I create an array of strings in C? - Stack Overflow

    Thank you.
    Last edited by nerio; 12-17-2015 at 02:17 PM.

  2. #2
    Registered User
    Join Date
    Nov 2015
    Posts
    3
    char array[30][30]

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by Filipe Oliveira View Post
    char array[30][30]
    Probably too many, plus it doesn't really help the original poster infer which dimension is the number of strings versus the max length of each string.
    Code:
    char t[3][30] = { 
        "string number one content", 
        "string number two content", 
        "string number three content" 
    };
    Of course you can use char ** or a specific number of pointers, like char *t[3] if 30 is not enough length.

  4. #4
    Registered User
    Join Date
    Dec 2015
    Posts
    34
    Quote Originally Posted by nerio View Post
    Hello. I study C.

    How to create array of strings in C?

    Let's say that I intend to have three strings:

    Code:
    t[0]='string number one content'
    t[1]='string number two content'
    t[3]='string number three content'
    I know how to make one string:

    Code:
    char u[30];
    which defines a string of length 30 (from 0 to 29). Now should I create an array of arrays?

    I have found this, but I do not understand it:

    How do I create an array of strings in C? - Stack Overflow

    Thank you.
    Code:
        char *name[3];
        
        name[0] = "hello world";
        name[1] = "my name is lawson";
        name[2] = "what is your name?";
        
        printf("%s\n", name[0]);
        printf("%s\n", name[1]);
        printf("%s\n", name[2]);
    Depending on what you want to do, if all you want is an array of strings, this is probably the easiest way. create a char * that points to the first char of each sentence/word and then just let printf() deal with traversing through the string of characters for displaying the string.

    Snap! What whiteflag said ^
    Last edited by Lawson; 12-17-2015 at 06:32 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nerio
    Let's say that I intend to have three strings:
    Recall that single quotes are used to delimit character literals and double quotes are used to delimit string literals. As such, 'string number one content' should have been "string number one content".

    Quote Originally Posted by Lawson
    Depending on what you want to do, if all you want is an array of strings, this is probably the easiest way. create a char * that points to the first char of each sentence/word and then just let printf() deal with traversing through the string of characters for displaying the string.
    If you want to use Lawson's suggestion, modify it slightly for const-ness:
    Code:
    const char *name[3];
    
    name[0] = "hello world";
    name[1] = "my name is lawson";
    name[2] = "what is your name?";
    This way, if you attempt modify name[0][0] for example, your compiler will warn you. Without the const, your compiler is unlikely to warn you, and so you would end up with undefined behaviour for attempting to modify a string literal.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-16-2012, 06:08 AM
  2. Replies: 1
    Last Post: 11-15-2011, 01:36 PM
  3. create and populate create bidimensional array
    By darkducke in forum C Programming
    Replies: 0
    Last Post: 12-03-2010, 07:06 AM
  4. Create strings based on input int?
    By omaralqady in forum C Programming
    Replies: 6
    Last Post: 03-02-2008, 11:14 AM
  5. create an array of strings that hold filenames
    By finkus in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2005, 09:19 PM