Re: Simple question on Pointers
- From: Barry Schwarz <schwarzb@xxxxxxxx>
- Date: Tue, 18 Nov 2008 19:50:11 -0800
On Tue, 18 Nov 2008 18:21:01 -0800, Robby
<Robby@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
Hello,
If I do this:
=============
int main()
{
int t, *u, **v;
u = &t;
v = &u;
return 0;
}
=============
In reference to the above sample code: In summary, v will contain the
address of where u is stored and u will contain the address of where t is
stored and doing **v can dereference the contents of t. That being said:
In a similar set up, if I do:
===============
int main()
{
char a[] = "hello world";
If you change this to
char *a = "hello world";
it will do what you expect.
char **ap = &a;
What is the type of a? What is the result of applying the & operator
to that type? Is that type the same as the type of ap?
return 0;
}
===============
Why does the latter example give the following warning?
c:\dts_visual_c++\misc_c_samples\misc_c_samples\utility.c(10) : warning
C4047: 'initializing' : 'char **' differs in levels of indirection from 'char
(*)[12]'
The text "abc" is not the same as the text "def".
The type double* is not the same as the type int*.
The type char** is not the same as the type char(*)[12]. What more
information do you require?
======================[Two part Question]
[Question #1]
So, if I do this (as shown in the 1st example):
u = &t;
What is the type of t? What is the result of applying the & operator
to that type. Is it the same as the type of u?
isn't the same thing happening with (as shown in the 2nd sample):
a[] = "hello world";
I mean u and a are both storing an address to something, right?
No. a does not store an address at all. It stores 12 char and only 12
char.
As a general rule, an expression with type array is converted to a
pointer to the first element of the array with type pointer to element
type. So you could say
char *aa = a;
However, when the array expression is the operand of the & operator,
you have one of the three exceptions to this rule. The array
expression is not converted to anything and retains its array type.
The other two exceptions are when it is the operand of the sizeof
operator and when it is a string literal used to initialize an array
of char.
Basically u contains the address of where the contents of t are stored and a
contains the address of where the first character of "hello world" is
stored... the "h".
No. a does not contain any address. See above.
[Question #2]
And if I do this (as shown in the 1st example):
v = &u;
isn't the same thing happening with (as shown in the 2nd sample):
char **ap = &a;
Obviously not. See above.
I mean if ap is a char pointer to pointer and we want to store the address
of a in ap, isn't this permissible just like we did with v = &u ????
Obviously not. See above.
Just trying to get a better feel of pointers relating, character arrays and
character pointers when applied to pointers to pointers. All feedback
appreciated!
In your first example, the address being stored in u is the address of
an int*. Similarly the only value that can be assigned to a char** is
the address of a char*. Since a is an array of char and not a char*,
your code does not satisfy this criteria.
--
Remove del for email
.
- Follow-Ups:
- Re: Simple question on Pointers
- From: Ben Voigt [C++ MVP]
- Re: Simple question on Pointers
- From: Robby
- Re: Simple question on Pointers
- References:
- Simple question on Pointers
- From: Robby
- Simple question on Pointers
- Prev by Date: CSocketComm
- Next by Date: Base64 string save to binary file
- Previous by thread: Re: Simple question on Pointers
- Next by thread: Re: Simple question on Pointers
- Index(es):
Relevant Pages
|