Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Function arguments, pass by pointer or reference? (http://www.go4expert.com/forums/function-arguments-pass-pointer-t25036/)

hseldon 20Feb2011 13:28

Function arguments, pass by pointer or reference?
 
Which do you think is best? or cons/pros of each of passing by pointer vs passing by reference.

lionaneesh 21Feb2011 13:00

Re: Function arguments, pass by pointer or reference?
 
Quote:

Originally Posted by hseldon (Post 79607)
Which do you think is best? or cons/pros of each of passing by pointer vs passing by reference.

Passing arguments by pointers allows the function to change our data in the pointed address..

Passing by reference allows us to safeguard our data...

Now lets test it :-

function.c

Code: c

#include<stdio.h>

void passByPointer(int * input)
{
    *input = 100;
}
void passByReference(int input)
{
    input = 1337;
}

int main()
{
    int NumberToTest = 1337;
    printf("Input = %d\n",NumberToTest);
    passByPointer(&NumberToTest);      // change to 100
    printf("Input = %d\n",NumberToTest); // should turn to 100
    passByReference(NumberToTest);   
    printf("Input = %d\n",NumberToTest); // Should be unchanged still will be 100
    return(0);
}


Compiling


Code:

gcc function.c -o function
Running :-

Code:

aneesh@aneesh-laptop:~/Programming/C$ ./function
Input = 1337
Input = 100
Input = 100

Hope this helps...

shabbir 21Feb2011 13:46

Re: Function arguments, pass by pointer or reference?
 
Quote:

Originally Posted by lionaneesh (Post 79646)
Passing arguments by pointers allows the function to change our data in the pointed address..

Passing by reference allows us to safeguard our data...

Now lets test it :-

function.c

Code: c

#include<stdio.h>

void passByPointer(int * input)
{
    *input = 100;
}
void passByReference(int input)
{
    input = 1337;
}

int main()
{
    int NumberToTest = 1337;
    printf("Input = %d\n",NumberToTest);
    passByPointer(&NumberToTest);      // change to 100
    printf("Input = %d\n",NumberToTest); // should turn to 100
    passByReference(NumberToTest);   
    printf("Input = %d\n",NumberToTest); // Should be unchanged still will be 100
    return(0);
}


Compiling


Code:

gcc function.c -o function
Running :-

Code:

aneesh@aneesh-laptop:~/Programming/C$ ./function
Input = 1337
Input = 100
Input = 100

Hope this helps...

You are getting confused with Pass By reference with Pass By Value.

xpi0t0s 21Feb2011 15:22

Re: Function arguments, pass by pointer or reference?
 
Neither is better; they are different tools for different jobs. You may as well ask which is better out of a hammer and a screwdriver.

lionaneesh 21Feb2011 15:59

Re: Function arguments, pass by pointer or reference?
 
Quote:

Originally Posted by shabbir (Post 79651)
You are getting confused with Pass By reference with Pass By Value.

Yes...
Sorry about that!!!

UATLevan 3Mar2011 11:04

Re: Function arguments, pass by pointer or reference?
 
There really is no best, you have to know why you're passing by that choice when you create the function. One advantage of pass by pointer is you can set them to NULL to represent different things in your function while references have to point to an object even though that object might not be valid.


Allen LeVan
UAT Student


All times are GMT +5.5. The time now is 19:19.