Array of strings in C

This is a discussion on Array of strings in C within the C Programming forums, part of the General Programming Boards category; What is the proper way to program an array of strings in C (not C++)? I have a list of ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    4

    Array of strings in C

    What is the proper way to program an array of strings in C (not C++)?

    I have a list of first names and I want to store them in an array. A string is an array of chars, so is an array of strings an array of array of chars? ... dizzy!!!

    Please advise.

    szill.

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Yes, you could use an array of arrays, just make sure that you stay consistent with which number represents the individual chars and which number represents the individual strings. There are a lot of confusing aspect to using 2-dimensional arrays, and they've been discussed frequently, so search the board before posting any problems. You may also find the sites tutorial on C-style strings helpful.

    Welcome to the boards.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    4
    Quote Originally Posted by sean_mackrory
    Yes, you could use an array of arrays, just make sure that you stay consistent with which number represents the individual chars and which number represents the individual strings. There are a lot of confusing aspect to using 2-dimensional arrays, and they've been discussed frequently, so search the board before posting any problems. You may also find the sites tutorial on C-style strings helpful.

    Welcome to the boards.
    I know they are confusing, that's why I asked for help.

    I searched the boards, they weren't helpful for what I am doing. Could you please post how to define an array of strings, and how to use? That's what I am asking.

    thanks.

    szill

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Define:
    Code:
    char strings[10][80];
    Use:
    Code:
    char *first_string = &strings[0][0]; // Sets a pointer to the first string
    char *second_string = &strings[1][0]; // Sets a pointer to the second string

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    4
    Thanks, i will give it a go. Is there a way to define a string as a typedef, and delcare variables as type string?

    please advise.

    thanks.

    szill

  6. #6
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Yeah, you could do that. Declare your data for a string in a struct, and then use a typedef just to make life easier.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,167
    You could do something like this easily enough:
    Code:
    itsme@dreams:~/C$ cat strtype.c
    #include <stdio.h>
    
    typedef char * string;
    
    int main(void)
    {
      string strs[5];  // Make 5 strings
      int i;
    
      strs[0] = "first string";
      strs[1] = "second string";
      strs[2] = "third string";
      strs[3] = "this is the fourth string";
      strs[4] = "and finally this is the fifth string";
    
      for(i = 0;i < 5;++i)
        puts(strs[i]);
    
      return 0;
    }
    Code:
    itsme@dreams:~/C$ ./strtype
    first string
    second string
    third string
    this is the fourth string
    and finally this is the fifth string
    Is that what you're looking for?
    If you understand what you're doing, you're not learning anything.

  8. #8
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    A simple design if you're looking at using constants known before hand, but I don't know that that sounds like what the OP is using.

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    4
    [QUOTE=itsme86]You could do something like this easily enough:
    Code:
    itsme@dreams:~/C$ cat strtype.c
    #include <stdio.h>
    
    typedef char * string;
    
    int main(void)
    {
      string strs[5];  // Make 5 strings
      int i;
    
      strs[0] = "first string";
      strs[1] = "second string";
      strs[2] = "third string";
      strs[3] = "this is the fourth string";
      strs[4] = "and finally this is the fifth string";
    
      for(i = 0;i < 5;++i)
        puts(strs[i]);
    
      return 0;
    }
    That's very close. When i string copy (strcpy) into the array of string, the program crashes.

    Code:
    char myOtherString[80];
    
    strcpy(strs[0], "Bob");                 // This will crash it
    strcpy(strs[1], myOtherString);   // So will this method...
    please advise.

    thanks so much all.

    szill

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    In itsme86's example each element in the array is a pointer to constant memory. So when you try to strcpy into that location it crashes because you aren't allowed to modify read only memory.

    @itsme86: Many people feel it is bad form to typecast away a pointer.

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,167
    Quote Originally Posted by Thantos
    @itsme86: Many people feel it is bad form to typecast away a pointer.
    I know, and I agree. I was just answering what I thought was the OP's question. He specifically asked how to typecast it away, so that's what I did.

    To the OP: If you want to use strcpy() with that typedef, you'll need to allocate some memory in which to store the strings. For example, you might start off by doing something like:
    Code:
    for(i = 0;i < 5;++i)
      strs[i] = malloc(300);
    Then you'd be able to strcpy() into the strs, but make sure to free() the strings when you're doing with them.

    But like Thantos said, you're probably better off without the typedef and just learning how to use 2d arrays. It'll make the code a lot easier to understand when you go back to look at it in a few months.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. intializing an array of strings
    By doubty in forum C Programming
    Replies: 4
    Last Post: 06-19-2009, 12:59 PM
  2. Replies: 1
    Last Post: 03-19-2009, 10:56 AM
  3. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 09:35 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21