How do I print out a two dimensional array of strings?

This is a discussion on How do I print out a two dimensional array of strings? within the C Programming forums, part of the General Programming Boards category; okay I have a two dimensional array of strings. Each row of the string is a variable length word, each ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    20

    How do I print out a two dimensional array of strings?

    okay I have a two dimensional array of strings. Each row of the string is a variable length word, each terminated by a null statement. I am able to print out the first row of the string, but can't get the rest of them to print.

    this is how i'm getting it to print the first row:

    Code:
    	printf("%s", newarray);
    I know why it's only printing the top row (because it's reaching a null statement) so I guess what i'm asking is if theres a way to access the next row of the array while printing it.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    %s tells printf to print a string, not an array, so what you get is undefined behavior.
    So if it is an array, there must be multiple strings, right?
    Then you must logically tell it to print each one separately. That's YOUR job - you must call printf to print every string in the array.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    20
    The way my book defined a string was a series of characters each being held in a space of the array with a null statement at the end of it.

    im going to try it your way now as well, would I use %c as a conversion specifier when printing out elements of a character array?

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    %c is used to print a single character, not a string. Your book is correct; a series of characters (an array) is defined as a string. So in that sense, a 2D-array of characters is an array of strings.
    Printf wasn't designed to print an array of strings, so you have to feed it with individual strings. Strings are printed with %s.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    In C, a string is a sequence of characters ended by a null character. What you are describing isn't a two dimensional array of strings, but a two dimensional array of characters, or said otherwise, a one dimensional array of strings.

    Code:
    #include <stdio.h>
    #define MAX_STRING_LENGTH 20
    
    void printArrayOfStrings(const char (*array)[MAX_STRING_LENGTH + 1], size_t n)
    {
       size_t i = 0;
       for (; i < n; ++i)
       {
          puts(array[i]);
       }
    }
    
    int main()
    {
       char arrayOfStrings[][MAX_STRING_LENGTH + 1] = { "Hello", "World", "Bananas"};
    
       printArrayOfStrings(arrayOfStrings, sizeof(arrayOfStrings) / sizeof(*arrayOfStrings));
    }
    I hate real numbers.

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    20
    ok I got this working, I did it by using two for loops and accessing each element of the array , one by one. count is the number of words in total

    Code:
    	for (counter1 = 0; counter1 != count; counter1++)
    	{
    		printf("\n");
    		for(counter2 = 0; newarray[counter1][counter2] != '\0'; counter2++)
    		{
    			printf("%c", newarray[counter1][counter2]);
    		}
    	}

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    You don't need the inner loop - that is handled by printf if you print a string.
    Code:
    	for (i = 0; i != count; i++)
    	{
    		printf("%s\n", newarray[i]);
    	}
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi dimensional array
    By $l4xklynx in forum C Programming
    Replies: 7
    Last Post: 01-03-2009, 02:56 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. two dimensional string array question
    By Hoser83 in forum C Programming
    Replies: 8
    Last Post: 02-07-2006, 07:15 PM
  4. Passing an Array of Strings from VB to a C DLL
    By mr_nice! in forum Windows Programming
    Replies: 9
    Last Post: 03-08-2005, 05:16 AM
  5. printing an array of strings
    By linucksrox in forum C Programming
    Replies: 3
    Last Post: 05-11-2004, 03:31 PM

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