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

pointer and variable scope concept

P: n/a
a
Hi,
func(int* p){
p = p+1;
}
main(){
int* ptr;
//assume ptr is pointing to a valid allocated array
func(ptr);
//Will ptr equal the original address value
//or equal the original address + sizeof(int)??
}
Thanks a lot
Jul 9 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
In article <HX8sg.141146$Mn5.44349@pd7tw3no>, a <a@mail.comwrote:
>Hi,
func(int* p){
p = p+1;
}
main(){
int* ptr;
//assume ptr is pointing to a valid allocated array
func(ptr);
//Will ptr equal the original address value
//or equal the original address + sizeof(int)??
}
Inside func, p is a *copy* of the value passed in. You then increment
that copy. The original is not changed.

If you did wish to permanently increment a pointer from inside a
function, you would have to pass the -address- of the pointer in,
and use that address to increment the value stored: that way what
the function receives is a copy of the address of the pointer, and that
copy of the address can be used to manipulate what is pointed to
(i.e., the pointer itself.)
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
Jul 9 '06 #2

P: n/a
a wrote:
Hi,
func(int* p){
p = p+1;
}
`p` is a local variable. Changing its value doesn't do anything
to anything else.
main(){
int* ptr;
//assume ptr is pointing to a valid allocated array
func(ptr);
//Will ptr equal the original address value
Yes. The code phrase for this is "C does not have call by reference".
//or equal the original address + sizeof(int)??
}
Thanks a lot
--
Chris "C does not have call by name" Dollin
"Never ask that question!" Ambassador Kosh, /Babylon 5/

Jul 10 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.