Thread: Just a quick question about character arrays

  1. #1
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168

    Just a quick question about character arrays

    Is there a way to make an array of character arrays? i.e. an array of 10 place names, which are character strings? i know you cant do multiple dimension arrays in C, so does this count as one? cheers!

  2. #2
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    Yes that is a multi-dimensional array.
    Code:
    #include <stdio.h>
    
    int main(){
        char strings[10][9]={     /*10 strings of 9 chars each*/
                            "String1",
                            "String2",
                            "String3",
                            "String4",
                            "String5",
                            "String6",
                            "String7",
                            "String8",
                            "String9",
                            "String10"
        };
        int loop;
        for(loop=0;loop<10;loop++){
                                   printf("%s\n",names[loop]);
        }
        getchar();
    }
    Outputs-
    Code:
    String1
    String2
    String3
    String4
    String5
    String6
    String7
    String8
    String9
    String10
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Code:
    Rectangular:
    multidimensional array of chars: char multi[2][10] = {"first", "second"};
    
    Jagged:
    array of pointers to char(s): char *jagged[5] = {"one", "two", "three", "four", "five"};
    The first one can hold two strings (like you want), but can't surpass 10 characters (that includes null), each. The second one can hold (point to) five strings and each can be of any length.

    - xeddiex

  4. #4
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    thanks guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick question about dynamic arrays.
    By Lawn Gnomusrex in forum C++ Programming
    Replies: 4
    Last Post: 11-13-2008, 03:22 PM
  2. Using a character array in a switch question.
    By bajanElf in forum C Programming
    Replies: 10
    Last Post: 11-08-2008, 08:06 AM
  3. Quick question arrays/pointers
    By liljp617 in forum C Programming
    Replies: 6
    Last Post: 10-02-2008, 12:11 PM
  4. Question about char arrays
    By PJYelton in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2003, 12:44 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM