passing 3 dimentional array into function

This is a discussion on passing 3 dimentional array into function within the C Programming forums, part of the General Programming Boards category; hi, i'm looking to pass the array fieldPos[9][50][50] into a function which then changes some of the values of fieldPos[x] ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    13

    passing 3 dimentional array into function

    hi, i'm looking to pass the array fieldPos[9][50][50] into a function which then changes some of the values of fieldPos[x] (ie want to pass one of 9 possible 50 by 50 maps into the function) and returns the new array.

    right now, fieldPos is defined as a global variabe, but i need to change it to a local variable. i've never used pointers, so if i need to use pointers, references would be greatly appreciated.

    so, i'm imagining code is something like this...

    Code:
    char fieldPos[9][50][50];
    int x=3;
    
      for (i=0;i<9; i++){
        for (j=0; j<50; j++){
          for (k=0; k<50;k++){
    	fieldPos[i][j][k]='x';
          }//k
        }//j
      }//i
    and then

    Code:
    doSmthng (fieldPos[x]);
    or something like that ...

    but i don't know what the arguments for doSmthng should be or how to pass the array into the function.

    any help would be greatly appreciated. thanks

    -Sean

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >ie want to pass one of 9 possible 50 by 50 maps into the function
    Then you want to pass a two dimensional array:
    Code:
    void foo ( char fieldPos[50][50] )
    {
      // Blah blah
    
      return fieldPos
    }
    
    // ...
    
    foo ( fieldPos[x] );
    >and returns the new array
    There's no "newing" going on here. Arrays are passed by "reference", in that you always access the original data, so you don't need to return anything if all you're doing is modifying the original array.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    13
    Thanks for the reply ... just to clarify, fieldPos[x] is actually a 2 dimentional 50x50 array??

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >just to clarify, fieldPos[x] is actually a 2 dimentional 50x50 array??
    Multidimensional arrays in C are really just arrays of arrays of arrays of...for however many dimensions you have. So a three dimensional array is an array of arrays of arrays. If you index the first dimension (ie. array[x]), you get an array of arrays, or a two dimensional array in common jargon. If you go on to index one of those (say, array[x][y]), you get a single array. Well, that long winded explanation boils down to yes. So, yes.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  3. Replies: 7
    Last Post: 11-21-2008, 03:27 PM
  4. function passing argument..array ?
    By jochen in forum C Programming
    Replies: 2
    Last Post: 09-30-2007, 11:53 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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