pointer as parameter to function

A

arnuld

this wroding is directly from "C++ Primer" by Lippman et al, 4/e:

"a parameter can be a pointer, in which case the argument pointer is
copied. as with any non-reference type parameter, changes made to the
parameter are made to the local copy. if function assigns a new value to
the parameter, the callingpointer value is unchanged"

/* C++ Primer - 4/e
*
* section 7.2.1
*
* pointers as parameters/arguments to functions *
*/

#include <iostream>

void reset_val(int* px)
{
*px = 0;
}



int main()
{
int i = 3;
int* ip = &i;

std::cout << "i = " << i << ",\t"
<< "*ip = " << *ip << "\n\n";


reset_val(ip);

std::cout << "i = " << i << ",\t"
<< "*ip = " << *ip << '\n';


return 0;
}


======== OUTPUT ==========
$ g++ -ansi -pedantic -Wall -Wextra 7.2.1.cpp $ ./a.out
i = 3, *ip = 3

i = 0, *ip = 0


look the original-value of "i" is changed. what exactly the author is
trying to say ?
 
B

Barry

arnuld said:
this wroding is directly from "C++ Primer" by Lippman et al, 4/e:

"a parameter can be a pointer, in which case the argument pointer is
copied. as with any non-reference type parameter, changes made to the
parameter are made to the local copy. if function assigns a new value to
the parameter, the callingpointer value is unchanged"

/* C++ Primer - 4/e
*
* section 7.2.1
*
* pointers as parameters/arguments to functions *
*/

#include <iostream>

void reset_val(int* px)
{
*px = 0;

it means that px is a pointer to int (non-reference), changing the value
of px(what it points to) won't affect the param passed in by the caller

}



int main()
{
int i = 3;
int* ip = &i;

std::cout << "i = " << i << ",\t"
<< "*ip = " << *ip << "\n\n";


reset_val(ip);

ip still points to i after the calling reset_val
 
S

Stuart Redmann

arnuld said:
this wroding is directly from "C++ Primer" by Lippman et al, 4/e:

"a parameter can be a pointer, in which case the argument pointer is
copied. as with any non-reference type parameter, changes made to the
parameter are made to the local copy. if function assigns a new value to
the parameter, the callingpointer value is unchanged"

/* C++ Primer - 4/e
*
* section 7.2.1
*
* pointers as parameters/arguments to functions *
*/

#include <iostream>

void reset_val(int* px)
{
*px = 0;
}

int main()
{
int i = 3;
int* ip = &i;

std::cout << "i = " << i << ",\t"
<< "*ip = " << *ip << "\n\n";
reset_val(ip);
std::cout << "i = " << i << ",\t"
<< "*ip = " << *ip << '\n';
return 0;
}
======== OUTPUT ==========
i = 3, *ip = 3
i = 0, *ip = 0

look the original-value of "i" is changed. what exactly the author is
trying to say ?

I'd say that the example doesn't illustrate the effect that is described by
above paragraph, as the function reset_val doesn't change the passed pointer but
the pointed to value. Are you sure that you didn't accidently changed the
implementation of reset_val from "p = 0;" to "*p = 0;" when you copied the
example into your posting?

Regards,
Stuart
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top