468,458 Members | 1,820 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,458 developers. It's quick & easy.

pointer and variable scope concept

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
2 1245
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
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.

Similar topics

4 posts views Thread by Markus Dehmann | last post: by
16 posts views Thread by jacob navia | last post: by
18 posts views Thread by steve | last post: by
51 posts views Thread by Kuku | last post: by
14 posts views Thread by sheroork | last post: by
9 posts views Thread by NevilleDNZ | last post: by
14 posts views Thread by Ian | last post: by
112 posts views Thread by istillshine | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.