Thread: passing array to function by reference

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    65

    passing array to function by reference

    I tried to send address of array element to the function

    Code:
    #include <stdio.h>   
    
    void fun(int *pointer) 
    { 
       int i; 
       for ( i = 0; i < 8; i++) 
     
          printf("\n %p %d \n", pointer, i);
    } 
    
    
        
    int main() 
    { 
        int i;
    	
       int array[] = {1, 2, 3, 4, 5, 6, 7, 8}; 
    
    
         for( i = 0; i < 8; i++)
        {
            printf("\n address of array[%d] %p", i, &array[i]);
        }
        
    	for( i = 0; i < 8; i++)
        {
            printf("\n content of array[%d]: %d", i, array[i] );
        }
        
    	fun(array);
    	
        return 0; 
    }
    address of array[0] 0061FF0C
    address of array[1] 0061FF10
    address of array[2] 0061FF14
    address of array[3] 0061FF18
    address of array[4] 0061FF1C
    address of array[5] 0061FF20
    address of array[6] 0061FF24
    address of array[7] 0061FF28
    content of array[0]: 1
    content of array[1]: 2
    content of array[2]: 3
    content of array[3]: 4
    content of array[4]: 5
    content of array[5]: 6
    content of array[6]: 7
    content of array[7]: 8
    0061FF0C 0


    0061FF0C 1
    0061FF0C 2
    0061FF0C 3
    0061FF0C 4
    0061FF0C 5
    0061FF0C 6
    0061FF0C 7



    Pointer hold the address of other variable

    content of pointer and address of array would be same

    so pointer [i] should be same as &array[i] ?

    From the output of code it does't seem to correct ?
    Last edited by gajya; 11-15-2019 at 10:21 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,506
    pointer[i] is an int; &array[i] is a pointer to int. How could they be the same thing?
    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

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    65
    Quote Originally Posted by laserlight View Post
    pointer[i] is an int; &array[i] is a pointer to int. How could they be the same thing?
    Code:
    #include <stdio.h>    
    void fun(int *pointer) 
    { 
       int i; 
       for ( i = 0; i < 8; i++) 
      
          printf("\n %p %d \n", pointer++, i);
    } 
         
    int main() 
    { 
        int i;
         
       int array[] = {1, 2, 3, 4, 5, 6, 7, 8}; 
     
     
         for( i = 0; i < 8; i++)
        {
            printf("\n address of array[%d] %p", i, &array[i]);
        }
    
    
         
        fun(array);
         
        return 0; 
    }
    address of array[0] 0061FF0C
    address of array[1] 0061FF10
    address of array[2] 0061FF14
    address of array[3] 0061FF18
    address of array[4] 0061FF1C
    address of array[5] 0061FF20
    address of array[6] 0061FF24
    address of array[7] 0061FF28

    0061FF0C 0
    0061FF10 1
    0061FF14 2
    0061FF18 3
    0061FF1C 4
    0061FF20 5
    0061FF24 6
    0061FF28 7
    Last edited by gajya; 11-15-2019 at 11:57 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,506
    So... you understand what's going on now?
    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. passing array of structs to function by reference
    By greadey in forum C Programming
    Replies: 5
    Last Post: 08-15-2015, 06:52 AM
  2. Warning while passing array by reference to function
    By Jdo300 in forum C Programming
    Replies: 11
    Last Post: 06-10-2008, 01:40 PM
  3. Passing an array as reference to a function?
    By Kylecito in forum C++ Programming
    Replies: 10
    Last Post: 03-11-2006, 02:25 AM
  4. Passing array by reference
    By TBc in forum C++ Programming
    Replies: 4
    Last Post: 12-11-2005, 08:48 AM
  5. Passing Array by Reference...
    By Cheeze-It in forum C++ Programming
    Replies: 5
    Last Post: 02-22-2002, 01:29 PM

Tags for this Thread