Menu

argh,sorry,coding question about arrays

Kurgusov
2007-08-13
2012-09-26
  • Kurgusov

    Kurgusov - 2007-08-13

    When do you not need to explicitly pass an array to a function?

    I know,nothing to do with dev-cpp,Im desperate.

    any feedback at all would be great,
    even if your telling me to ^*** off.

     
    • Nobody/Anonymous

      But note that generally, the use of 'global variables', is considered poor practice, for various reasons ...

      If you compare the abowe two example programs, the function to print out the arrays can be reused more readily in the first example, where the arrays to be printed are passed by the calling statement each time:

      printArray( B, ROWS, COLS );

      ...

      printArray( A, ROWS, COLS );

       
    • Wayne Keen

      Wayne Keen - 2007-08-13

      Are you asking a question about passing by reference versus value?

      (He asked, a remarkably stupid look on his face)

      Wayne

       
    • Nobody/Anonymous

      the Great Wayne Spoke>>Are you asking a question about passing by reference versus value? (He asked, a remarkably stupid look on his face) <<

      I mean,do I have to pass the array to a function that inspects an element in it,with or without changing the elements value.
      The function might also be in a different source file to the array.

      And also I know how many elements the array has.

      Oh and thanks for NOT telling me to ^&*% off.
      :)

       
    • Wayne Keen

      Wayne Keen - 2007-08-13

      I am not sure if what follows (a) Has any bearing on your question or (b) Has any
      degree of reality in our universe.

      Keep in mind, when you pass an array to a function, what you are really doing is
      passsing a pointer that tells where in memory the array starts.

      Now, I assume that you mean that while the array is defined and maintained in
      a seperate fule - that the files are all part of one project and one executable?

      I know my questions border on the sub-moronic here, but I am trying to ascertain
      for sure in my limited (doing three things at once) brain what yoy are asking.

      Wayne

      p.s. Simple example of array passing:

      http://www.cplusplus.com/doc/tutorial/arrays.html

       
    • Nobody/Anonymous

      My obscure question was created when a few weeks ago I did this bit of cod.

      //a .cpp file

      define MAXELE 100 //defined in its header

      static int array1[MAXELE]; //pretend these have values
      static int array2[MAXELE]; //

      void aFunction()
      {
      for (int i = 0; i < MAXELE; i++)
      {
      array1[i] = array2[i];
      }
      }

      when it ran the pc crashed and burned.
      So of coarse,I was forced to pass in the array like this....

      void aFunction(int ary1[], int ary2[], int theMaxEle)
      {
      for (int i = 0; i < theMaxEle; i++)
      {
      ary1[i] = ary2[i];
      }
      }

      Now the reason Im confused is that this does work...

      char array[100][100];

      void changeArray()
      {
      array[11][39] = 'U';
      }

      but this doesnt...

      char array1[100][100];
      char array2[100][100];

      void swapem()
      {
      array1[44][44] = array2[22][22];
      }

      I dont know why,and mybe Ive just completly confused everyone.

       
      • Osito

        Osito - 2007-08-13

        Your last example works fine for me. Are you doing anything else to the arrays before calling the function?

         
    • Nobody/Anonymous

      w.r.t. your question:

      but this doesn't... 
      
      char array1[100][100]; 
      char array2[100][100]; 
      
      void swapem() 
      { 
      array1[44][44] = array2[22][22]; 
      } 
      
      I don't know why, and maybe I've just completley confused everyone.
      

      Have you assigned a value to array2[22][22] ... like

      array2[22][22]=123;

      before calling 'swapem()'
      (and are 'char array1[100][100]; char array2[100][100];' declared with global scope?)

       
    • Nobody/Anonymous

      or try this ...

      / passingArrays.c /

      include <stdio.h>

      include <stdlib.h>

      void assignReverseBtoA( int a[], int b[], int numElements )
      {
      int i;
      for(i=0; i<numElements; ++i) a[i]=b[numElements-1-i];
      }

      void printArray( int a[], int numElements )
      {
      int i;
      for(i=0; i < numElements; ++i) printf( "%d ", a[i] );
      printf("\n\n");
      fflush(stdout);
      }

      int main()
      {
      int B[] = {0,1,2,3,4,5,6,7,8,9};

          const int numMembers = sizeof(B) / sizeof(B[0]);
      printArray( B, numMembers );
      
      int A[ numMembers ];
      
      assignReverseBtoA( A, B, numMembers  );
      printArray( A, numMembers );
      
      system(&quot;pause&quot;);  
      return 0;
      

      }

       
    • Nobody/Anonymous

      opps ... for C need to ...

      / passingArrays.c /

      include <stdio.h>

      include <stdlib.h>

      void assignReverseBtoA( int a[], int b[], int numElements )
      {
      int i;
      for(i=0; i<numElements; ++i) a[i]=b[numElements-1-i];
      }

      void printArray( int a[], int numElements )
      {
      int i;
      for(i=0; i < numElements; ++i) printf( "%d ", a[i] );
      printf("\n\n");
      fflush(stdout);
      }

      define numMembers 10

      int main()
      {
      int B[] = {0,1,2,3,4,5,6,7,8,9};

      printArray( B, numMembers );
      
      int A[ numMembers ];
      
      assignReverseBtoA( A, B, numMembers  );
      printArray( A, numMembers );
      
      system(&quot;pause&quot;);  
      return 0;
      

      }

       
    • Nobody/Anonymous

      Hey,thanks tons for your time,I really appriciate it,
      its not so much 'how' to code it to get the results,its the question...

      "When do I not have to explicitly pass an array to a function that works with that array"

      or should I always explicitly pass the array.

      I know,Ive just gone full circle and repeated my first post.

       
    • Nobody/Anonymous

      If the arrays are declared with global scope, THEN, they are 'known' inside functions ...

      Otherwise ... how would they be 'known' inside a function, (unless you pass them in)?

      BTW, arrays are always passed by reference in C ( and in C++ too.)

       
    • Nobody/Anonymous

      / passingArrays2d.c /

      / NOTE: Arrays, by default, in C and C++, are passed by reference. /

      include <stdio.h>

      include <stdlib.h>

      define ROWS 2

      define COLS 5

      void assignReverseBtoA( int A[][COLS], int B[][COLS], int r, int c )
      {
      int row, col;
      for(row=0; row<r; ++row)
      for(col=0; col<c; ++col)
      A[row][col] = B[r-1-row][c-1-col];
      }

      void printArray( int A[][COLS], int r, int c )
      {
      int row, col;
      for(row=0; row<r; ++row)
      {
      for(col=0; col<c; ++col)
      printf( "%d ", A[row][col] );

          printf(&quot;\n&quot;);
      }
      
      printf(&quot;\n\n&quot;);
      fflush(stdout);
      

      }

      int main()
      {
      int B[][COLS] =
      {
      {0,1,2,3,4},
      {5,6,7,8,9}
      };

      printArray( B, ROWS, COLS  );
      
      int A[ ROWS ][ COLS ];
      
      assignReverseBtoA( A, B, ROWS, COLS  );
      printArray( A, ROWS, COLS  );
      
      system(&quot;pause&quot;);  
      return 0;
      

      }

       
    • Nobody/Anonymous

      / passingArrays2d2.c /

      / this example utilizes arrays with GLOBAL scope /

      include <stdio.h>

      include <stdlib.h>

      define ROWS 2

      define COLS 5

      int B[][COLS] = / GLOBALs /
      {
      {0,1,2,3,4},
      {5,6,7,8,9}
      };

      int A[ ROWS ][ COLS ]; / GLOBALs /

      void assignReverseBtoA()
      {
      int row, col;
      for(row=0; row<ROWS; ++row)
      for(col=0; col<COLS; ++col)
      A[row][col] = B[ROWS-1-row][COLS-1-col];
      }

      void printArrayA()
      {
      int row, col;
      for(row=0; row<ROWS; ++row)
      {
      for(col=0; col<COLS; ++col)
      printf( "%d ", A[row][col] );

          printf(&quot;\n&quot;);
      }
      
      printf(&quot;\n\n&quot;);
      fflush(stdout);
      

      }

      int main()
      {
      assignReverseBtoA();
      printArrayA();

      system(&quot;pause&quot;);  
      return 0;
      

      }

       
    • Kurgusov

      Kurgusov - 2007-08-15

      Thanks Again Last Poster,

      You fully helped me out.

      Much Respects.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.