Like Tree2Likes
  • 2 Post By edw211

Using a function to edit a 2D array

This is a discussion on Using a function to edit a 2D array within the C Programming forums, part of the General Programming Boards category; Hi every one, I want to write a function that can do some work on a 2D array. I read ...

  1. #1
    Registered User astroboy2000ir's Avatar
    Join Date
    Jun 2011
    Location
    Sendai, Japan
    Posts
    16

    Using a function to edit a 2D array

    Hi every one, I want to write a function that can do some work on a 2D array. I read that functions don't return arrays and the best way would be to set the arguments of the function as pointers to the array I want to work on. The actual program is long and requires a lot of prerequists so I made a much shorter program showing basically the same problem;

    Code:
    #include <stdio.h>
    
    void testfunction(int *array[][10]);
    
    int main (void) {
       int testarray[10][10], i, j;
       for(i=0; i<10; i++){
           for(j=0; j<10; j++) testarray[i][j]=0;
       }
       testfunction(&testarray[][]);
       for(i=0; i<10; i++){
           for(j=0; j<10; j++) printf("%d\n", testarray[i][j]);
       }  
       return 0;
    }
    
    void testfunction(int *array[][10]){
       int a, b;
       for(a=0; a<10; a++) {
           for(b=0; b<10; b++) *array[a][b]=a*b;
       }  
    }
    I get this error when I compile this program;
    Code:
    test.c:10:28: error: expected expression before ] token
    Could any one please guide me in this problem or introduce me to a better method to work on a 2D array inside a function? It would be greatly appreciated, thanks...

  2. #2
    Registered User edw211's Avatar
    Join Date
    Jun 2011
    Location
    Wilkes-Barre, PA
    Posts
    22
    Someone correct me if I'm wrong, but you don't need to use pointer syntax here because the name of an array itself functions as a pointer. These modifications to your code worked for me:

    Code:
    #include <stdio.h>
    
    void testfunction(int array[][10]);
    
    int main (void) {
       int testarray[10][10], i, j;
       for(i=0; i<10; i++){
           for(j=0; j<10; j++) testarray[i][j]=0;
       }
       testfunction(testarray);
       for(i=0; i<10; i++){
           for(j=0; j<10; j++) printf("%d\n", testarray[i][j]);
       }  
       return 0;
    }
    
    void testfunction(int array[][10]){
       int a, b;
       for(a=0; a<10; a++) {
           for(b=0; b<10; b++) array[a][b]=a*b;
       }  
    }
    My understanding of pointers is currently sub par for these forums, but I think you may have been inadvertently using a "pointer to a pointer" data type. Your function doesn't need to return the array unless there's something I'm missing because the changes made to your array inside the function will persist after control is returned to main.
    Last edited by edw211; 06-13-2011 at 07:19 PM.
    astroboy2000ir and Salem like this.
    Evan Williams
    Lehigh University Senior
    Computer Science and Engineering

  3. #3
    Registered User astroboy2000ir's Avatar
    Join Date
    Jun 2011
    Location
    Sendai, Japan
    Posts
    16
    Dear Evan,
    Thanks for the fast reply, you are right, I took away all the complications I had added (deleting the '&', '*' and []s) and it worked fine, I did the same changes the the actual lengthy program and it worked perfectly, It is a long time I hadn't written in C and now that I am returning back to it, I am making such mistakes...

    Thanks again,
    Last edited by astroboy2000ir; 06-13-2011 at 07:24 PM.

  4. #4
    Registered User edw211's Avatar
    Join Date
    Jun 2011
    Location
    Wilkes-Barre, PA
    Posts
    22
    No problem. Good luck with the rest of your program.
    Evan Williams
    Lehigh University Senior
    Computer Science and Engineering

  5. #5
    Registered User
    Join Date
    Jun 2011
    Location
    Bolpur, India
    Posts
    42
    Quote Originally Posted by edw211 View Post
    Someone correct me if I'm wrong, but you don't need to use pointer syntax here because the name of an array itself functions as a pointer. These modifications to your code worked for me:

    Code:
    #include <stdio.h>
    
    void testfunction(int array[][10]);
    
    int main (void) {
       int testarray[10][10], i, j;
       for(i=0; i<10; i++){
           for(j=0; j<10; j++) testarray[i][j]=0;
       }
       testfunction(testarray);
       for(i=0; i<10; i++){
           for(j=0; j<10; j++) printf("%d\n", testarray[i][j]);
       }  
       return 0;
    }
    
    void testfunction(int array[][10]){
       int a, b;
       for(a=0; a<10; a++) {
           for(b=0; b<10; b++) array[a][b]=a*b;
       }  
    }
    My understanding of pointers is currently sub par for these forums, but I think you may have been inadvertently using a "pointer to a pointer" data type. Your function doesn't need to return the array unless there's something I'm missing because the changes made to your array inside the function will persist after control is returned to main.
    I was under the impression that we cannot pass an array(specially 2D)to a function without using pointer.So can we return values also from the function being called without using pointer?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by edw211 View Post
    My understanding of pointers is currently sub par for these forums, but I think you may have been inadvertently using a "pointer to a pointer" data type. Your function doesn't need to return the array unless there's something I'm missing because the changes made to your array inside the function will persist after control is returned to main.
    Why you should not try to return arrays from functions in C....
    Code:
    #include <stdio.h>
    int* MyFunction(int a, int b, int c)
      {  static int array[3];
         array[0] = a;
         array[1] = b;
         array[2] = c;
         return array;  } // return a pointer.
    
    
    int main (void)
      { int *a1, *a2;  // int pointers
    
        printf("calling a1 = MyFunction(10,20,30);\t");
        a1 = MyFunction(10,20,30);
        printf("a1 has %d %d %d\n",a1[0],a1[1],a1[2]);
    
        printf("calling a2 = MyFunction(100,200,300);\t");
        a2 = MyFunction(100,200,300);
        printf("a2 has %d %d %d\n",a2[0],a2[1],a2[2]);
    
        printf("\nLooks good, except...\t"); 
        printf("a1 now has %d %d %d\n",a1[0],a1[1],a1[2]);
    
        getchar();
        return 0; }
    Must be magic!

  7. #7
    Registered User
    Join Date
    Jun 2011
    Location
    Bolpur, India
    Posts
    42
    Quote Originally Posted by CommonTater View Post
    Why you should not try to return arrays from functions in C....
    Code:
    #include <stdio.h>
    int* MyFunction(int a, int b, int c)
      {  static int array[3];
         array[0] = a;
         array[1] = b;
         array[2] = c;
         return array;  } // return a pointer.
    
    
    int main (void)
      { int *a1, *a2;  // int pointers
    
        printf("calling a1 = MyFunction(10,20,30);\t");
        a1 = MyFunction(10,20,30);
        printf("a1 has %d %d %d\n",a1[0],a1[1],a1[2]);
    
        printf("calling a2 = MyFunction(100,200,300);\t");
        a2 = MyFunction(100,200,300);
        printf("a2 has %d %d %d\n",a2[0],a2[1],a2[2]);
    
        printf("\nLooks good, except...\t"); 
        printf("a1 now has %d %d %d\n",a1[0],a1[1],a1[2]);
    
        getchar();
        return 0; }
    Must be magic!
    I ran the program but couldn't understand why is this happening......Help!!

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The array is static, and persists through function calls. Therefore, consecutive calls of that function, with different inputs, will change the values in that array. You are simply using two different pointers to point to the same place. It's the same as doing this:
    Code:
    int *foo, *bar, baz;
    foo = &baz;
    baz = 5;
    bar = &baz;
    baz = 6;
    printf( "Oh no, baz is six! I am shocked! %d\n", *foo );

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

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mistu4u View Post
    I ran the program but couldn't understand why is this happening......Help!!
    As my buddy Quzah points out it's because you are returning pointers *not* arrays. You are merely choosing to treat those pointers as arrays. Both pointers end up pointing to the same place, so when the second function call is made it changes the array, effectively changing the values at the first pointer, giving you "magic numbers" as a result.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to edit the values of an array in a struct?
    By bluej322 in forum C Programming
    Replies: 4
    Last Post: 04-15-2011, 04:18 PM
  2. Storing values from Edit Box into an array
    By E_I_S in forum C++ Programming
    Replies: 10
    Last Post: 06-05-2008, 06:24 AM
  3. Getting the contents of edit boxes into an array
    By earth_angel in forum Windows Programming
    Replies: 3
    Last Post: 07-05-2005, 12:17 PM
  4. Need to set EDIT background BLACK and EDIT text WHITE
    By Garfield in forum Windows Programming
    Replies: 3
    Last Post: 08-29-2003, 01:36 PM
  5. Edit/Rich Edit window size restrictions
    By Echidna in forum Windows Programming
    Replies: 2
    Last Post: 01-19-2002, 02:23 AM

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