Thread: Array Pointers

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    1

    Angry Array Pointers as Arguments

    Hi everyone, this is my first post here.

    My question concerns Arrays and how they are sent to functions.

    In my main function I have a simple Array of ints, and I set the first element to 1.

    I then printf this value so I can see the output in the console.

    I then call a function, sending my Array as it's argument, inside the function I change the first element to the number 50. In the main function, I printf the value of the first element of this array before and after the other function is called, and I was surprised to see that the modification I did in my function affected the value in the main function.

    It seems the array is sent as a pointer, why and how can I send it as a temp variable in the scope of the function.

    Had I used the function Something_arr_2(int *cc); Then I would expect the value to be picked up in main() as I'm receiving a pointer.

    I just don't really understand why Something_arr(int cc[]); can modify the same array as in main()

    Code:
    #include <stdio.h>
    
    void Something(int cc);
    void Something_arr(int cc[]);
    void Something_arr_2(int *cc);
    
    int main (int argc, const char * argv[]) {
    	int b;
    	b = 99;
    	printf("INT IN MAIN BEFORE %d\n",b);
    	Something(b);
    	printf("INT IN MAIN AFTER %d\n",b);
    
        
    	int arrB[1];
    	arrB[0] = 99;
    	printf("INDEX 0 OF ARRAY IN MAIN BEFORE %d\n",arrB[0]);
    	Something_arr(arrB);
    	printf("INDEX 0 OF ARRAY IN MAIN AFTER %d\n",arrB[0]);
    
    	return 0;
    }
    
    
    void Something(int cc){
    	printf("INSIDE FUNCTION BEFORE: %d\n", cc);
    	cc++;
    	printf("INSIDE FUNCTION AFTER: %d\n", cc);
    }
    
    
    void Something_arr(int cc[]){
    	printf("INSIDE FUNCTION BEFORE: %d\n", cc[0]);
    	cc[0]++;
    	printf("INSIDE FUNCTION AFTER: %d\n", cc[0]);
    }
    
    void Something_arr_2(int *cc){
    	printf("INSIDE FUNCTION BEFORE: %d\n", cc[0]);
    	cc[0]++;
    	printf("INSIDE FUNCTION AFTER: %d\n", cc[0]);
    }
    Last edited by djp1988; 07-12-2010 at 07:39 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by djp1988
    I then call a function, sending my Array as it's argument, inside the function I change the first element to the number 50. In the main function, I printf the value of the first element of this array before and after the other function is called, and I was surprised to see that the modification I did in my function affected the value in the main function.
    Yes, this is expected for pointers to non-const objects. They effectively provide a means to pass the object by reference, hence it is possible for changes to the object to be reflected in the caller.

    Quote Originally Posted by djp1988
    It seems the array is sent as a pointer, why and how can I send it as a temp variable in the scope of the function.
    The rule is that when an array is passed as an argument (and indeed in many other contexts), it is converted to a pointer to its first element. The array in the caller remains an array, of course, but the pointer parameter thus points to the first element of the array argument. Thus, changing what the pointer points to in the function changes the corresponding array element.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 02:45 PM
  2. two-dimensional dynamic array of pointers to classes
    By Timo002 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 06:18 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. array of pointers to struct array
    By eth0 in forum C++ Programming
    Replies: 1
    Last Post: 01-08-2004, 06:43 PM
  5. array of pointers to structs
    By stumon in forum C Programming
    Replies: 7
    Last Post: 03-24-2003, 07:13 AM

Tags for this Thread