printing an array of strings

This is a discussion on printing an array of strings within the C Programming forums, part of the General Programming Boards category; I have this code and I just can't figure out why it won't work the way I want it to. ...

  1. #1
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198

    printing an array of strings

    I have this code and I just can't figure out why it won't work the way I want it to. I have a valid array of strings that I have already verified, but when I pass it to the function and it tries to print, all I get is one line of garbage.
    Code:
    void printlist(char star[MAX][LIM], int n)
    {
        int ct = 4;
    
        puts(star[ct]);
        for (ct = 0; ct < n; ct++);
          puts(star[ct]);
    }
    the first puts() prints correctly, but the for loop just prints one line of garbage. MAX is 10 and LIM is 81, n is equal to the number of strings in the array.

    EDIT: DANG I feel so stupid. I just found that stupid semicolon blatantly staring me in the face right after the for declaration. wonder why my compiler didn't give me any errors... haha i'm still learning
    Last edited by linucksrox; 05-11-2004 at 03:56 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    EDIT: DANG I feel so stupid. I just found that stupid semicolon blatantly staring me in the face right after the for declaration. wonder why my compiler didn't give me any errors... haha i'm still learning
    The reason your compiler didn't say anything is because it's valid C. It's common to have a loop that does all of its work in the loop definition itself. For example, here's a common one to clear out the input stream:
    Code:
    while( (c = getchar( )) != '\n' && c != EOF );
    What this does is read one character from the stream, check to see if it's a newline or EOF, and stop if it is. If it isn't, it continues reading. With a for loop, you may get something like:
    Code:
    for( ptr = array, ptr2 = array2; *ptr; *ptr2++ = *ptr++ );
    This would in effect copy the contents of the first array to the second.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    127
    >wonder why my compiler didn't give me any errors...
    Because a loop with a null statement as the body is perfectly legal. I avoid such problems simply by not taking advantage of the C (mis-)feature that allows single statement loops to omit bracing. This way if I forget to place a statement in the loop then I've also forgotten to close the loop, and that will result in a syntax error. The opening brace is also a mnemonic telling you that the line doesn't end with a semicolon because it's the start of a compound statement block. I've found that this particular error simply vanishes when you use braces religiously.

  4. #4
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    ahhh.... I get it. I might start using braces more religiously myself. i have a tendency to just hit that semicolon key... And what it was doing was trying to print a line that didn't have a valid string in it which was the garbage i was seeing. awesome i understand now

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, 01:59 PM
  2. Replies: 1
    Last Post: 03-19-2009, 11:56 AM
  3. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 12:28 PM
  4. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 10:35 PM
  5. 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, 06:16 AM

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