473,770 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10622
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*****@blueyo nder.co.uk> wrote in message
news:Xi******** **********@text .news.blueyonde r.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
2468
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 give me some advice? I don't know where to go to look this information up. Do I need to access the stack, and hence do some kind of inline assembly code to get the calling address?
1
2064
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: 0b5d9270. The system returns the wrong value, e.g. expects value f7f7f7f7 and instead gets f7f7e7f7. This RAM is on the motherboard and it's about as expensive to replace it than to buy a new computer. Is there any small program that could be...
35
10794
by: hasho | last post by:
Why is "call by address" faster than "call by value"?
15
2243
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 it as an array starting at (you guessed it) 0x0000. So I would have struct iv_s* ivt = (struct iv_s *) 0x0000;
33
3186
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
3167
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
5674
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 expects type 'void *; but argument 2 has type 'void
54
24539
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
3398
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 address? I keep feeling like I'm missing something obvious. -Jul To keep things in context, this is in reference to describing functions to a beginner.
7
2813
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 naming names here to remove bias - I'm trying to tell if I'm relying on implementation defined behavior or if this is a bug in the new compiler. Consider this stripped down example:
0
9617
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9453
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10254
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9904
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8929
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7451
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.