473,395 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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
Mar 25 '06 #1
6 10609
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
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
>> 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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: johny smith | last post by:
I am trying to figure out a way to print the address of what called a certain function once inside the function. I am assuming that this information is on the stack somewhere. But can someone...
1
by: JuanPedro | last post by:
OS: Windows XP Home SP2 CPU/Ram: Mobile AMD Duron 4, 1GHz / 256MB System Manufacturer: Compaq Presario 730us Using windiag.exe to test my RAM I get a consistent error for the following address:...
35
by: hasho | last post by:
Why is "call by address" faster than "call by value"?
15
by: dandelion | last post by:
Hi, Just another question for the standards jockeys... Suppose I have an Interrupt Vector Table located at address 0x0000 (16-bit machine). I want to dump the context of the IVT, by treating...
33
by: baumann.Pan | last post by:
hi all, i want to get the address of buf, which defined as char buf = "abcde"; so can call strsep(address of buf, pointer to token);
16
by: jimjim | last post by:
#include <stdio.h> #include <stdlib.h> //WARNING: The function's signature should be: f( int ** ) void f(int *ip) { static int dummy = 5; *ip = &dummy; } int main(){
57
by: Robert Seacord | last post by:
i am trying to print the address of a function without getting a compiler warning (i am compiling with gcc with alot of flags). if i try this: printf("%p", f); i get: warning: format %p...
54
by: John | last post by:
Is the following program print the address of the function? void hello() { printf("hello\n"); } void main() { printf("hello function=%d\n", hello); }
36
by: Julienne Walker | last post by:
Ignoring implementation details and strictly following the C99 standard in terms of semantics, is there anything fundamentally flawed with describing the use of a (non-inline) function as an...
7
by: John Koleszar | last post by:
Hi all, I'm porting some code that provides compile-time assertions from one compiler to another and ran across what I believe to be compliant code that won't compile using the new compiler. Not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.