Re: Simple question on Pointers

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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
.



Relevant Pages

  • Re: Problem with va_ macros and arrays of arrays
    ... > the arrays passed to a ... > specific char, somewhat similar to what the standard function ... that with an array of struct, or possibly a pointer to a dynamic array ... > As I'm still a beginner in C without a copy of the standard I ...
    (comp.lang.c)
  • Re: Memory Structure Pointer Problems
    ... typedef struct sta { ... char* name; ... int num_cmpnds; ... A pointer to a struct cmp is almost ...
    (comp.lang.c)
  • Re: Insufficient guarantees for null pointers?
    ... will the compiler know what the bounds are after converting that char * ... to an int *, if it could point to either of two arrays which happen to ... compares equal to the original pointer. ...
    (comp.std.c)
  • Re: easy parsing problem in C for beginner
    ... error: parse error before "char" ... array boundary checks etc. built into the C ... To convert a character of type '0'-'9', ... int PriorityIndexFromCharacter ...
    (comp.sys.mac.programmer.help)
  • Re: problem with memcpy and pointers/arrays confusion - again
    ... int line, unsigned long *total_mem) ... That's a long pointer address... ... If sizeof > sizeof which is ... if you allocate for char with sizeof < sizeof, ...
    (comp.lang.c)