help with arrays

This is a discussion on help with arrays within the C Programming forums, part of the General Programming Boards category; help me with the following codes, I am doing an example out of a book, but I can't do it. ...

  1. #1
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    Talking help with arrays

    help me with the following codes, I am doing an example out of a book, but I can't do it. I need your help to learn how to do it.

    #include <stdio.h>
    main()/* I am trying to print out all the arrays*/

    {

    char list_ch[][2]={'1','a',
    '2','b',
    '3', 'c',
    '4', 'd',
    '5', 'e',
    '6', 'f'};
    int x, i;
    int j;
    for (i=0; i<6; i++);
    for ( j=0; j<1; j++);
    printf("%6d\n", list_ch[i][j]);// should I leave list_ch empty
    //meaning list_ch[]?

    while (x!='e')// to exit program properly
    x=getchar();
    return 0;
    }

    // I want to print the arrays declared by list_ch, but I don't know how to do it, help...........

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    char multiarray[][2] =
    {
       { 'a', 'b' },
       { 'c', 'd' },
       { 'e','f' },
       { 'g','h' }
    };
    This is how you declare a multidimensional array and assign it values at the time of creation.

    Quzah.

  3. #3
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    printing

    ok but how would I go about printing them?
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How do you _think_ you'd go about printing them? Given that there are N number of rows, each containing 2 characters, you could just simply use a for loop.

    for(x=0;x<N;x++) printf("%c %c", array[x][0],[array[x][1] );

    If you have an array who's first member's value is not given:

    array[][3]

    It means there are an undetermined number of rows, each containing (in this case, 3) a set number of other 'cells'.

    The only way you can use [] is when you define the number of entries (provide all values) at creation:

    char array[][2] = { {'1','2'}, {'2','a'}, {5,3} };

    Quzah.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 07:59 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM

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