Thread: Help with array

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    Help with array

    Code:
    #include <stdio.h>
    
    int main()
    {
        char a[3][8] = { {"First"}, {"Second"}, {"Third"} };
        
        return 0;
    }
    How would I display the elements of array a on the screen?
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        int i, j;
        char a[3][8] = { {"First"}, {"Second"}, {"Third"} };
        
        for( i = 0; i < 3; i++ ) {
             for( j = 0; j < strlen(a[i]); j++ )
                  printf("%s", a[i][j]);
        }
        
        return 0;
    }
    This does not work. Thanks.

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    How about just this:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        int i;
        char a[3][8] = { {"First"}, {"Second"}, {"Third"} };
        
        for( i = 0; i < 3; i++ )
        {
                  printf("%s\n", a[i]);
        }
        
        return 0;
    }
    As a sidenote, you shouldn't use strlen in the condition of a loop. Save the return value of strlen to a temp variable.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    As a sidenote, you shouldn't use strlen in the condition of a loop. Save the return value of strlen to a temp variable.
    Why? What's your reasoning here? As long as you aren't going to be passing NULL to strlen, what does it matter?

    Using the return value of strlen is fine if you do it right. That was just wrong, because they would end up passing it a[3], which would be an invalid array index. That's the real problem. Not the use of the return value for control.


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

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    You don't really want to call strlen every iteration of the loop, do you? Just say NO to strlen.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    In a program this size, it's a moot point. As stated, strlen is not the problem. The problem is their array will call strlen with an invalid array index.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM