address of pointer from a pointer | | |
Hi All,
I can't seem to wrap my head around this one.
I have a pointer,
int *x;
which I can assign:
x = &y;
Now, I can send this variable by reference like so:
some_function( &x );
But, what do I do if x is contained within a pointer to a struct (or class
for that matter)?
struct c {
int *x;
}
such that a pointer to c (c *cptr) would access it like this:
cptr->x = &y;
but now, if I try to send this by reference:
some_function( cptr->&x )
I get an error: expected unqualified-id before '&' token. How do I
properly send this variable by reference?
Thanks for any help!
Scott | | | | re: address of pointer from a pointer
Scott wrote:[color=blue]
> I can't seem to wrap my head around this one.
>
> I have a pointer,
>
> int *x;
>
> which I can assign:
>
> x = &y;
>
> Now, I can send this variable by reference like so:
>
> some_function( &x );
>
> But, what do I do if x is contained within a pointer to a struct (or
> class for that matter)?
>
> struct c {
> int *x;
> }
>
> such that a pointer to c (c *cptr) would access it like this:
>
> cptr->x = &y;
>
> but now, if I try to send this by reference:
>
> some_function( cptr->&x )
>
> I get an error: expected unqualified-id before '&' token. How do I
> properly send this variable by reference?[/color]
The variable is (cptr->x). The address of it is...? (&(cptr->x)).
Use parentheses to fully contain (limit, denote, delineate) your
expression. Then add to your expression. Only remove parentheses
when you're ready and know the rules of precedence.
V
--
Please remove capital As from my address when replying by mail | | | | re: address of pointer from a pointer
Scott wrote:[color=blue]
> Hi All,[/color]
Howdy-doo.
[color=blue]
> I have a pointer,
>
> int *x;[/color]
Okay.
[color=blue]
> which I can assign:
>
> x = &y;[/color]
Yes, assuming y is of type int.
[color=blue]
> Now, I can send this variable by reference like so:
>
> some_function( &x );[/color]
Yes, assuming that some_function takes one argument of type int*. I
assume when you refer to passing "by reference," you mean "by pointer."
Remember, C++ has a different construct which is actually *named*
"reference," and that's a common way to pass parameters as well --
considering that, it's maybe better not to say "pass by reference" when
you mean you're using a pointer, if there's any chance of confusion.
And since the address-of operator (&) uses the same symbol as that used
to declare a reference type, I'd say confusion can crop up pretty
easily here.
[color=blue]
> But, what do I do if x is contained within a pointer to a struct (or class
> for that matter)?
>
> struct c {
> int *x;
> }
>
> such that a pointer to c (c *cptr) would access it like this:
>
> cptr->x = &y;[/color]
No problem. It's a matter of scoping. In C++, as in many other
languages, we have to deal with scoping a lot. When referring to
something that doesn't reside in your local scope, you have to qualify
it. So, you can't just say "x" (as you know); you have to say cptr->x
(or (*cptr).x, which is the same thing). This is true whenever you
refer to x.
[color=blue]
> but now, if I try to send this by reference:
>
> some_function( cptr->&x )
>
> I get an error: expected unqualified-id before '&' token.[/color]
Right. The address-of operator is looking to the right of itself for a
symbol which names a variable to take the address of. The variable
name has to be qualified because it's not local, so you want:
some_function(&(cptr->x))
The compiler is actually balking slightly before it gets a chance to
consider the address-of operator, though, and that's what the error
message is about. The message is saying that the arrow operator has to
point to an unqualified-id -- that is, a variable name which is not
missing any necessary qualifiers (namespaces, enclosing classes, etc.).
Instead, it sees the symbol '&', which is not valid.
[color=blue]
> How do I
> properly send this variable by reference?[/color]
See above.
[color=blue]
> Thanks for any help!
> Scott[/color]
HTH,
Luke | | | | re: address of pointer from a pointer
>> I have a pointer,[color=blue][color=green]
>>
>> int *x;
>> which I can assign:
>>
>> x = &y;
>> Now, I can send this variable by reference like so:
>>
>> some_function( &x );[/color]
>
> Yes, assuming that some_function takes one argument of type int*.[/color]
Warning W8069 solution.c 7: Nonportable pointer conversion in function f
Warning W8075 solution.c 14: Suspicious pointer conversion in function main
The function's signature should be: some_function( int ** ). However,
some_function( int *) seems to produce the same results, even though you are
warned by the compiler. Is there anything that can go wrong when you pass
the address of a pointer to a function that does not recieve a pointer to a
pointer?
Thx | | | | re: address of pointer from a pointer
"jimjim" <netuser@blueyonder.co.uk> wrote in message
news:Xi9Vf.41750$wl.21461@text.news.blueyonder.co. uk...[color=blue][color=green][color=darkred]
>>> I have a pointer,
>>>
>>> int *x;
>>> which I can assign:
>>>
>>> x = &y;
>>> Now, I can send this variable by reference like so:
>>>
>>> some_function( &x );[/color]
>>
>> Yes, assuming that some_function takes one argument of type int*.[/color]
>
> Warning W8069 solution.c 7: Nonportable pointer conversion in function f
> Warning W8075 solution.c 14: Suspicious pointer conversion in function
> main
>
> The function's signature should be: some_function( int ** ). However,
> some_function( int *) seems to produce the same results, even though you
> are warned by the compiler. Is there anything that can go wrong when you
> pass the address of a pointer to a function that does not recieve a
> pointer to a pointer?
>
> Thx[/color]
Oups, my mistake! The C++ compiler outputs:
Error E2034 solution.cpp 14: Cannot convert 'int * *' to 'int *' in function
main()
Error E2342 solution.cpp 14: Type mismatch in parameter 'ip' (wanted 'int
*', got 'int * *') in function main()
I made the mistake and gave a '.c' extension to my file, and therefore it
was compiled with the C compiler. Apparently, C is more tolerant to such
syntax.
However, could anyone attempt to answer my question, which of course is in
the context of the C lang?
Thx in advance.
jimjim | | | | re: address of pointer from a pointer
Hi Luke,
On Fri, 24 Mar 2006 20:54:18 -0800, Luke Meyers wrote:[color=blue]
> Right. The address-of operator is looking to the right of itself for a
> symbol which names a variable to take the address of. The variable
> name has to be qualified because it's not local, so you want:
> some_function(&(cptr->x))
>
> The compiler is actually balking slightly before it gets a chance to
> consider the address-of operator, though, and that's what the error
> message is about. The message is saying that the arrow operator has to
> point to an unqualified-id -- that is, a variable name which is not
> missing any necessary qualifiers (namespaces, enclosing classes, etc.).
> Instead, it sees the symbol '&', which is not valid.[/color]
Excellent... thanks for the help!
Scott | | | | re: address of pointer from a pointer
jimjim wrote:[color=blue][color=green][color=darkred]
> >> I have a pointer,
> >>
> >> int *x;
> >> which I can assign:
> >>
> >> x = &y;
> >> Now, I can send this variable by reference like so:
> >>
> >> some_function( &x );[/color]
> >
> > Yes, assuming that some_function takes one argument of type int*.[/color]
>
> Warning W8069 solution.c 7: Nonportable pointer conversion in function f
> Warning W8075 solution.c 14: Suspicious pointer conversion in function main[/color]
You have to post the code (all of it); nobody has the time to try and
guess.
[color=blue]
> The function's signature should be: some_function( int ** ).[/color]
Right, yes -- I think I misread it the first time. x is of type int*,
so &x is of type int**.
[color=blue]
> However,
> some_function( int *) seems to produce the same results, even though you are
> warned by the compiler.[/color]
I'd treat that warning as an error. I'm surprised it isn't one.
[color=blue]
> Is there anything that can go wrong when you pass
> the address of a pointer to a function that does not recieve a pointer to a
> pointer?[/color]
You should get an error, or a warning that should be taken as an error.
That's what the type system is for -- to make sure you don't (among
other things) pass parameters of the wrong type.
Anyway -- if you still have questions, post the code, THEN ask.
Luke |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,439 network members.
|