Thread: Query: Receiving array from functions

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    22

    Query: Receiving array from functions

    Greetings.

    I seek assistance understanding the procedure for passing (receiving) arrays to (from) functions. The following code is indicative of my misunderstanding:


    Code:
    #include <stdio.h>
    
    char retArr(char []);
    
    int main(){
    	char tempArr[5];
    	char rcvArr[5];
    	
    	rcvArr = retArr(tempArr);
    	
    	printf("rcvArr[0]: %c\n", rcvArr[0]);
    	return 1;
    }
    
    char retArr(char arr[]){
    	arr[0] = 'A';
    	arr[1] = 'B';
    	arr[2] = 'C';
    	arr[3] = 'D';
    	arr[4] = 'E';
    
    	return arr[];
    }
    Would greatly appreciate advise & assistance.

    Best regards,
    wirefree101

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You don't receive an array from a function. You pass an array to a function like you pass anything else to a function, but any changes you make to that array are seen in the original main.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    1) return arr[] is not a valid expression.
    2) Your function returns a single char, FYI.
    3) You can't return an array by value in C. You can return a pointer, but just keep in mind that it can't be to a local variable.
    4) You can define a structure that contains an array and pass it around by value, but just keep in mind that it can't hold a pointer to an array of values, just a fixed-size buffer, eg:

    Code:
    #define ARRAY_HOLDER_BUFFER_LENGTH 5
    
    struct array_holder
    {
          char data[ ARRAY_HOLDER_BUFFER_LENGTH ];
    };

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by wirefree101 View Post
    Greetings.

    I seek assistance understanding the procedure for passing (receiving) arrays to (from) functions. The following code is indicative of my misunderstanding:
    Code:
    #include <stdio.h>
    
    char retArr(char []); //this is global, all functions can work with it, no passing needed.
    
    int main(){
                            //char tempArr[5]; not needed
            int i;
    	char rcvArr[5];
    	
    	/*tempArr (the name of the array, is actually pointing to the base of the array with 
              it's name. No return type is needed for an array that you send to a function.
           */
            retArr[rcvArr);  //is all you need tempArr is not needed at all
                         // rcvArr = retArr(tempArr);
    	
    	//let's make it a full-blown peep show, instead: :)
            for(i = 0; i < 5; i++)
               printf("rcvArr[%d]: %c\n", i, rcvArr[i]);
                      // printf("rcvArr[0]: %c\n", rcvArr[0]);
    	return 1;
    }
    
    
    void retArr(char arr[]){  //arr is now another name for rcvArr[] - same array though
    	arr[0] = 'A';
    	arr[1] = 'B';
    	arr[2] = 'C';
    	arr[3] = 'D';
    	arr[4] = 'E';
    
    	//no return needed
    }
    Hope that helps.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    22
    All advise much appreciated.

    Best regards,
    wirefree101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 12:56 AM
  2. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM