Thread: question about arrays

  1. #1
    Unregistered
    Guest

    Question question about arrays

    hello,
    how can i print an array of strings? how do i make this array

    thank you.

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    An array of strings can be done like so
    Code:
    #include <stdio.h> 
    
    int main(void) 
    
    { 
        char array[][7] = { {"First"},
                            {"Second"},
                            {"Third"},
                            {"Fourth"} };
    
    	int x;
    
    	for(x=0; x<4; x++)
    		printf("\n%s", array[x]);
    
    	return 0;   
    }
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  3. #3
    Unregistered
    Guest

    Talking Using a pointer

    the other way of doing what Mr. C_Coder has provided is the following:
    ---------------------------------------------------------------------------

    #include <stdio.h>

    int main(void)

    {
    char *array[] = { "First",
    "Second",
    "Third",
    "Fourth"};

    int x;

    for(x=0; x<4; x++)
    printf("\n%s", array[x]);

    return 0;
    }

  4. #4
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    the latter solution is more space-efficient.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about arrays.
    By Kelvie in forum C++ Programming
    Replies: 3
    Last Post: 09-17-2007, 05:32 AM
  2. A question concerning character arrays
    By ellipses in forum C Programming
    Replies: 3
    Last Post: 03-08-2005, 08:24 PM
  3. Replies: 6
    Last Post: 04-26-2004, 10:02 PM
  4. Question about char arrays
    By PJYelton in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2003, 12:44 AM
  5. Question about arrays?? PLease read..
    By foofoo in forum C Programming
    Replies: 3
    Last Post: 06-24-2002, 02:40 PM