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

address of pointer from a pointer

P: n/a
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
Mar 25 '06 #1
Share this Question
Share on Google+
6 Replies


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


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
Mar 25 '06 #2

P: n/a
Scott wrote:
Hi All,
Howdy-doo.
I have a pointer,

int *x;
Okay.
which I can assign:

x = &y;
Yes, assuming y is of type int.
Now, I can send this variable by reference like so:

some_function( &x );
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.
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;
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.
but now, if I try to send this by reference:

some_function( cptr->&x )

I get an error: expected unqualified-id before '&' token.
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.
How do I
properly send this variable by reference?
See above.
Thanks for any help!
Scott


HTH,
Luke

Mar 25 '06 #3

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


Yes, assuming that some_function takes one argument of type int*.


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
Mar 25 '06 #4

P: n/a

"jimjim" <ne*****@blueyonder.co.uk> wrote in message
news:Xi******************@text.news.blueyonder.co. uk...
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 );


Yes, assuming that some_function takes one argument of type int*.


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


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
Mar 25 '06 #5

P: n/a
Hi Luke,

On Fri, 24 Mar 2006 20:54:18 -0800, Luke Meyers wrote:
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.


Excellent... thanks for the help!

Scott

Mar 25 '06 #6

P: n/a
jimjim wrote:
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 );
Yes, assuming that some_function takes one argument of type int*.


Warning W8069 solution.c 7: Nonportable pointer conversion in function f
Warning W8075 solution.c 14: Suspicious pointer conversion in function main


You have to post the code (all of it); nobody has the time to try and
guess.
The function's signature should be: some_function( int ** ).
Right, yes -- I think I misread it the first time. x is of type int*,
so &x is of type int**.
However,
some_function( int *) seems to produce the same results, even though you are
warned by the compiler.
I'd treat that warning as an error. I'm surprised it isn't one.
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?


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

Mar 27 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.