Returning Array

This is a discussion on Returning Array within the C Programming forums, part of the General Programming Boards category; Hi all, please correct me if i make a stupid statement.... I know/think i know its not possible to explicitly ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    22

    Returning Array

    Hi all,

    please correct me if i make a stupid statement....

    I know/think i know its not possible to explicitly return an array within C. However i am undert the impression that it is possible to do so using pointers. Ive had a read of various tutorials but none of which really have helped me in what im trying to do.

    Im trying to....

    1. pass an array to a function,

    2. manipulate the array data

    3. return the array back to main

    thats the simplest way i can describe it i think. I have provided the required sinpets from my main function and the function that i want to edit my array elements.


    heres what ive tried......fairly certain its rubbish but what the hell... if some one could help me clear this one up id be very greatful

    thanks

    Baffa


    Code:
    void MixColumns(int *ptr[], int size){
    
    int r[4];
    
    
    r[0] = ptr[0]*2 + ptr[12] + ptr[8] + ptr[4]*3;
    
    r[1] = 2*(ptr[4]) + ptr[0] + ptr[12] + 3*(ptr[8]);
    
    r[2] = 2*(ptr[8]) + ptr[4] + ptr[0] + 3*(ptr[12]);
    
    r[3] = 2*(ptr[12]) + ptr[8] +ptr[4] +3*(ptr[0]);
    
    
    ptr[0] = r[0];
    
    ptr[4] = r[1];
    
    ptr[8] = r[2];
    
    ptr[12] = r[3];
    
    
    return ptr;
    
    }
    
    int main (){
    
    
    int current_state[16];
    
    int *ptr = current_state;
    
    current_state = MixColumns(ptr, 16);
    
    return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,312
    When an array is passed as an argument, it decays to a pointer to its first element. Consequently, modifying the elements of the array passed modifies the elements of the array from the caller. In that sense, returning the array would be unnecessary.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Remove the line saying "return ptr" and you should be good to go. Of course, you are also not using the size, so that's not needed - it's a good thing to pass the size, but it makes no sense to pass it if all you do is ignore it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    22
    you what? so i dont need to return it anyway? you saying to just use a void function or somthing because when the array data is modified in the function?


    Oh poo ive realised i put void instead of having


    int MixColumns(int ptr[], int size){......

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, your code, as it stands, updates the info inside the array.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    22
    Code:
     line 57: error: expression must have arithmetic or enum type
    
      r[0] = ptr[0]*2 + ptr[12] + ptr[8] + ptr[4]*3;
    
                                                             ^
    im getting a lot of these errors throughout the code on compliation.

    Is there somthing im doing wrong to manipulate the array elements?

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by baffa View Post
    Code:
     line 57: error: expression must have arithmetic or enum type
    
      r[0] = ptr[0]*2 + ptr[12] + ptr[8] + ptr[4]*3;
    
                                                             ^
    im getting a lot of these errors throughout the code on compliation.

    Is there somthing im doing wrong to manipulate the array elements?
    Unless this is a 2-dimensional array you're passing, change the function to:
    Code:
    void MixColumns(int *ptr, int size){

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,312
    You are passing an array of pointers. You probably want to change "int *ptr[]" to "int ptr[]".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    22
    woow that was awesome.....i only get this now any ideas? i dont what an lvalue is?...

    Code:
    error: expression must be a modifiable lvalue
    
      current_state = MixColumns(ptr);

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,312
    MixColumns() is now declared as returning void, right? As such, you cannot assign its return value to current_state is there is no return value.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Nov 2007
    Posts
    22
    ye but what i wanted to do was then return the data in the mixcolumns function to the current_state column. sorry thats my fault for being a bit vague. I did wonder how having a void function would do that :P

    ye i want to assign the array elements in the mixcolumns function to the current state array in main. How would i do that?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,312
    ye i want to assign the array elements in the mixcolumns function to the current state array in main. How would i do that?
    What is the ptr variable for? It looks like you want to write:
    Code:
    MixColumns(current_state);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Nov 2007
    Posts
    22
    I used ptr as the pointer to the array as I thought that i needed to use that to reference the array and return it.

    can there be a case where i can have this in my main function?
    Code:
    current_state = MixColumns(current_state);
    thats is ideally what i want but i dont know how i can return the amended array to update current_state.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,312
    can there be a case where i can have this in my main function?
    Yes, but as stated you do not need to return from MixColumns().
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by laserlight View Post
    When an array is passed as an argument, it decays to a pointer to its first element. Consequently, modifying the elements of the array passed modifies the elements of the array from the caller. In that sense, returning the array would be unnecessary.
    I agree (of course) and I like the word "decay"! That's exactly what happens.
    It should be pointed out (I'm sure it's in the FAQ) that it only happens to the first dimension of a multi-dimensional array. The other dimensional information is useable in the callee at compile-time. For example, the following works no matter what the size of the extra dimensions.
    Code:
    int addem3d(int a[][2][2], int size) {
        int sum = 0;
        for (int i = 0; i < size; ++i)
            for (int j = 0; j < sizeof(*a)/sizeof(**a); ++j)
                for (int k = 0; k < sizeof(**a)/sizeof(***a); ++k)
                    sum += a[i][j][k];
        return sum;
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parsing and returning array pointers?
    By thealmightyone in forum C Programming
    Replies: 26
    Last Post: 03-26-2009, 04:38 PM
  2. Returning an Array
    By mmarab in forum C Programming
    Replies: 10
    Last Post: 07-19-2007, 08:05 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. Replies: 6
    Last Post: 10-21-2003, 10:57 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM

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