passing and recieving multi-dementional arrays

This is a discussion on passing and recieving multi-dementional arrays within the C Programming forums, part of the General Programming Boards category; I'm writing a C program and I need to pass an array with 2 dementions to a function, but I ...

  1. #1
    Dogbert B-Con's Avatar
    Join Date
    Mar 2004
    Posts
    25

    passing and recieving multi-dementional arrays

    I'm writing a C program and I need to pass an array with 2 dementions to a function, but I don't know how to declare it as an arguement in the function parameters....

    I'll pass, depending, either element "catagory[0]" or "catatory[1]", both have 5 elements in the 2nd demention, but within the function I need to be able to access either of them with the same receiving arguement, ie:


    Code:
    switch(x) {
     case 1:
      myfunction(catagory[0]);
      break;
     case 2:
      myfunction(catagory[1]);
      break
    }
    
    **end main**
    
    void myfunction(*HOW TO RECEIVE THE ELEMENT?*) {
    for (x=0; x < 5; x++) {
     printf("%c", *HOW TO USE THE ELEMENT AND IT'S 2ND DEMENTION*);
    }
    }
    Is this clear?

    Thanx


    BTW: I'm new here, and somewhat new to C programming, I like this place, hope to be around for a while
    "Cryptographically secure linear feedback shift register based stream ciphers" -- a phrase that'll get any party started.

  2. #2
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790
    Code:
    void myfunction(datatype *x);
    as arrays are passed as pointers by default.

    so all you have to do is

    Code:
    void myfunction(array[2]);

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    when passing arrays there is no need to specify the first dimension btw

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by B-Con
    I'm writing a C program and I need to pass an array with 2 dementions to a function, but I don't know how to declare it as an arguement in the function parameters....

    I'll pass, depending, either element "catagory[0]" or "catatory[1]", both have 5 elements in the 2nd demention, but within the function I need to be able to access either of them with the same receiving arguement, ie:
    Is the following close to what you are describing?
    Code:
    #include <stdio.h>
    
    void foo(const char (*bar)[5])
    {
       size_t i;
       for ( i = 0; i < sizeof *bar / sizeof **bar; ++i )
       {
          printf("%c", bar[0][i]);
       }
       putchar('\n');
       for ( i = 0; i < sizeof *bar / sizeof **bar; ++i )
       {
          printf("%c", bar[1][i]);
       }
       putchar('\n');
    }
    
    int main (void)
    {
       const char ked[][5] =
       {
          {'a','b','c','d','e'},
          {'f','g','h','i','j'},
       };
       foo(ked);
       return 0;
    }
    
    /* my output
    abcde
    fghij
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Dogbert B-Con's Avatar
    Join Date
    Mar 2004
    Posts
    25
    Sort of...... but I can't use pointers, it's a small portion of a HW assignment, and the prof's unreachable till the next class.....

    Here's excately what I have (the portions that matter), but I'm getting an "invalid indirection" error..... (ignore the "go" array, at least for now)

    Code:
    if (cat[round][0] == 'F' || cat[round][0] == 'f')
       vGetCat(go,catagory[0],5);
    else if (cat[round][0] == 'N' || cat[round][0] == 'n')
       vGetCat(go,catagory[1],5);
    
    ***end main***
    
    void vGetCat(char go[], char catagory[], int total)
    {
       int idx;
       go[0] = getchar();
       for (idx=0; idx < total; idx++)
       {
          if (go[0] == catagory[0][idx])      <-- LINE THE COMPILER SAYS THE ERROR'S ON
             return;
       }
    
    }
    "Cryptographically secure linear feedback shift register based stream ciphers" -- a phrase that'll get any party started.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Would changing this
    Code:
    void foo(const char (*bar)[5])
    to this
    Code:
    void foo(const char bar[][5])
    allow you to pretend that there are no pointers?
    Last edited by Dave_Sinkula; 04-08-2004 at 08:28 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Dogbert B-Con's Avatar
    Join Date
    Mar 2004
    Posts
    25
    Ah, it's working now..... thanx guys......
    "Cryptographically secure linear feedback shift register based stream ciphers" -- a phrase that'll get any party started.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,656
    > vGetCat(go,catagory[0],5);
    If catagory is a 2D array, what you're passing here is just one element of that array
    Which would be a 1D array

    > if (go[0] == catagory[0][idx])
    Since its a 1D array, this would be
    if (go[0] == catagory[idx])
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

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