By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,463 Members | 771 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,463 IT Pros & Developers. It's quick & easy.

pointer as parameter to function

P: n/a
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 ?

--
http://arnuld.blogspot.com

Aug 9 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
arnuld wrote:
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
>
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 ?
Aug 9 '07 #2

P: n/a
arnuld wrote:
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
Aug 9 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.