Thread: Printing an array of string

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    115

    Printing an array of string

    Hello,

    why does the function below print the second element "Jim" rather than the first "Hanna"?

    Code:
    #define PERSONS_NUM 4
    
    char Names[PERSONS_NUM][40] = {
    	"Hanna",
    	"Jim",
    	"Gregory",
    	"Will"
    };
    
    void PrintString(char* c)
    {
    	while (*c) {
    		putchar(*c);
    		c++;
    	}
    }
    
    int main(void)
    {
    	PrintString(&Names[0][40]);
    }
    I am correct by using a 2d array for a static list of names?

  2. #2
    Registered User
    Join Date
    Mar 2020
    Posts
    6
    Hi Serge,

    The following Stack Overflow article should help explains things to you.
    Initializing 2D char array in C - Stack Overflow

    also check out this site article.
    2D character array-Declaration and initialization-C tutorials | Codingeek

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    115
    Quote Originally Posted by complexnumber View Post
    Hi Serge,

    The following Stack Overflow article should help explains things to you.
    Initializing 2D char array in C - Stack Overflow

    also check out this site article.
    2D character array-Declaration and initialization-C tutorials | Codingeek
    OK, thank you for the links!

    I still don't understand why array of chars are not zeroed, but changing the index to -1 prints the first element.

    PrintString(&Names[-1][40]);
    My question is it a safe way of doing it if I need to store labels for the rows (first index) of an integer array? I am unsure about the negative index.

  4. #4
    Registered User
    Join Date
    Mar 2020
    Posts
    6
    Hi Serge,

    There are a few issues with your code... I'll go through some examples here

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    #define PERSONS_NUM 4
    
    char Names[PERSONS_NUM][40] = {
        "Hanna",
        "Jim",
        "Gregory",
        "Will"
    };
    
    /* when passing in a 2d array you need to use the following 
        syntax. It's all memory....*/
    void PrintString(char c[][40])
    {
    /* your loop is a mess. Use the following */
        
        int counter;
        counter = 0;
        while(counter < PERSONS_NUM)
        {
                    /* putchar is the wrong function to use..I suggest 
                                   puts() */
            puts(c[counter]);
            counter++;
        }
    
    
    }
    
    int main(void)
    {
            /* you only need to pass in the name of the array.
               The name of the array is a pointer to the first
               element of the array */
        PrintString(Names); 
            return EXIT_SUCCESS;
    }
    I've compiled and run the above code using clang on MacOS. It should also work with other compilers

    Remember, that it's all memory. An array name is a pointer to the first element of the array!

    I hope this helps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me String not printing :(
    By fredsilvester93 in forum C++ Programming
    Replies: 5
    Last Post: 02-09-2012, 03:32 PM
  2. printing a string
    By weili in forum C Programming
    Replies: 1
    Last Post: 07-25-2011, 03:08 PM
  3. Issues printing out a string array
    By lodey in forum C Programming
    Replies: 6
    Last Post: 02-22-2011, 11:30 AM
  4. Replies: 8
    Last Post: 03-31-2006, 08:15 AM
  5. printing a string
    By ktntech in forum C Programming
    Replies: 1
    Last Post: 01-24-2002, 05:09 PM

Tags for this Thread