Re: Trying to understand pointers for function paramaters
On Wed, 29 Sep 2004 23:09:10 +0200, "Richard Hengeveld"
<richardhengeve ld@hotmail.com> wrote:
[color=blue][color=green]
>> Probably what is confusing you is the *formal* argument
>>
>> int* p[/color]
>
>Thanks for replying.
>Yes, that is exactly what is confusing me.
> I understand you set a pointer (if you're not passing to functions) by:
>
>int a, *p;
>p = &a;
>
>and not:
>
>p* = &a
>
>Wouldn't it be more logical if it was something like:
>
> void f(int i) {
> int *p
> p = i;[/color]
I assume you meant p=&i here.
[color=blue]
> p* = 0;[/color]
I assume you meant *p=0 here. This will set i to 0 but I is local to
f. When you return, i ceases to exist. Consequently, the argument in
the calling function is never updated and defeats the purpose.
[color=blue]
> }
>
> int main(int argc, char* argv[]) {
> int a;
> f(&a);[/color]
With f as defined above, this obviously causes a diagnostic since f is
expecting an int but you are passing an int*.
Going back to your original post which defined f as expecting an int*,
when you call f with this argument, three significant things happen:
i is created as an automatic variable local to f.
i is initialized with the address of a (which is a local variable
local to main.
Control is transferred to f.
Look back at your code at the top of this post and notice how similar
the first two are to what you wrote when not calling a function.
[color=blue]
> return 0;
> }[/color]
The concepts to understand are
In a definition, *i defines i as a pointer.
After the definition, i refers to the pointer itself and not the
object being pointed to, if any.
Once the pointer has been initialized, coding *i dereferences the
pointer and evaluates to the object being pointed to.
A function call causes all the parameters of the function to be
initialized with the corresponding arguments used in the calling
statement. (So f(&a) has a very similar effect to p=&a.)
<<Remove the del for email>>
On Wed, 29 Sep 2004 23:09:10 +0200, "Richard Hengeveld"
<richardhengeve ld@hotmail.com> wrote:
[color=blue][color=green]
>> Probably what is confusing you is the *formal* argument
>>
>> int* p[/color]
>
>Thanks for replying.
>Yes, that is exactly what is confusing me.
> I understand you set a pointer (if you're not passing to functions) by:
>
>int a, *p;
>p = &a;
>
>and not:
>
>p* = &a
>
>Wouldn't it be more logical if it was something like:
>
> void f(int i) {
> int *p
> p = i;[/color]
I assume you meant p=&i here.
[color=blue]
> p* = 0;[/color]
I assume you meant *p=0 here. This will set i to 0 but I is local to
f. When you return, i ceases to exist. Consequently, the argument in
the calling function is never updated and defeats the purpose.
[color=blue]
> }
>
> int main(int argc, char* argv[]) {
> int a;
> f(&a);[/color]
With f as defined above, this obviously causes a diagnostic since f is
expecting an int but you are passing an int*.
Going back to your original post which defined f as expecting an int*,
when you call f with this argument, three significant things happen:
i is created as an automatic variable local to f.
i is initialized with the address of a (which is a local variable
local to main.
Control is transferred to f.
Look back at your code at the top of this post and notice how similar
the first two are to what you wrote when not calling a function.
[color=blue]
> return 0;
> }[/color]
The concepts to understand are
In a definition, *i defines i as a pointer.
After the definition, i refers to the pointer itself and not the
object being pointed to, if any.
Once the pointer has been initialized, coding *i dereferences the
pointer and evaluates to the object being pointed to.
A function call causes all the parameters of the function to be
initialized with the corresponding arguments used in the calling
statement. (So f(&a) has a very similar effect to p=&a.)
<<Remove the del for email>>
Comment