why the code does not compile?

Y

yogesh

Why does the following code gives an error.


char c[]="sometext";
char **cp = &c;

This gives the error "can not covert from char (*)[8] to char**;

But when i change the code to
char *c="sometext";
char **cp = &c;

or to
char *c[]={"sometext"};
char **cp = c;

The code runs smoothly.

I have been taught that arrays are conveted to pointers internally by
compiler.
So why the first code is giving the error since saying char c[] is
same as saying char *c ?

regards,
Yogesh Joshi
 
I

infobahn

yogesh said:
Why does the following code gives an error.

char c[]="sometext";
char **cp = &c;

This gives the error "can not covert from char (*)[8] to char**;

&c has the type char (*)[9], not char (*)[8], so the message is
puzzling. But the basic idea is correct - i.e. it's not char **!
But when i change the code to
char *c="sometext";
char **cp = &c;

or to
char *c[]={"sometext"};
char **cp = c;

The code runs smoothly.

I have been taught that arrays are conveted to pointers internally by
compiler.

Only in a value context, and &object is not a value context. You
were mistaught.
 
K

Keith Thompson

Why does the following code gives an error.


char c[]="sometext";
char **cp = &c;

This gives the error "can not covert from char (*)[8] to char**;

Are you sure it was [8], not [9]? (Remember the trailing '\0'.)

c is an array object. The name c is implicitly converted to a pointer
in most contexts, not in all contexts. The exceptions are when it
appears as an operand of the unary "&" or "sizeof" operator.
Similarly, sizeof(c) yields 9, not whatever the size of a pointer
happens to be on your system.
 
N

Nick Austin

Why does the following code gives an error.


char c[]="sometext";
char **cp = &c;

This defines two objects:
1. c is an array-of-char (which is sometimes the same as a
pointer-to-char)
2. cp is a pointer-to-pointer-to-char.

You can't do that initialisation because an intermediate object
of type pointer-to-char is required. &c is still just a
pointer-to-char.
But when i change the code to
char *c="sometext";
char **cp = &c;

This defines three objects:
1. c is pointer-to-char
2. cp is pointer-to-pointer-to-char
3. "sometext" is an array of char (sometimes the same as a
pointer-to-char).

&c yields a pointer-to-pointer-to-char.
char *c[]={"sometext"};
char **cp = c;

Same reason. Three objects. c is an array-of-pointers-to-char.

Nick.
 
L

Lawrence Kirby

Why does the following code gives an error.


char c[]="sometext";
char **cp = &c;

This defines two objects:
1. c is an array-of-char (which is sometimes the same as a
pointer-to-char)

Arrays and pointers are never the same thing, although there are a couple
of features of C that can make it appear that way.

1. When you take the "value" of an array you get a pointer to its first
element. This is just syntactic sugar e.g. char *p = c; is a shorter
way of writing char *p = &c[0];

2. Function parameters specified as array are reinterpreted as pointers. C
doesn't support passing array arguments at all. If you try to pass an
array argument then rule 1. above says you'll pass a pointer to its
first element. This rule changes the parameter type to match that. So
in

void foo(char arg[]) { }

arg isn't an array at all, it IS a pointer to char e.g. sizeof arg ==
sizeof(char *). This is ONLY the case for function parameters.
2. cp is a pointer-to-pointer-to-char.

You can't do that initialisation because an intermediate object
of type pointer-to-char is required. &c is still just a
pointer-to-char.

No, &c has type char (*)[9] i.e. a pointer to an array of 10 chars. You
are correct however that c doesn't contain any object of type char * so
it makes no sense to try to point at it with a pointer of type char **.
The following would work, although it isn't very useful:

char (*cp)[9] = &c;

Lawrence
 
K

Keith Thompson

Lawrence Kirby said:
No, &c has type char (*)[9] i.e. a pointer to an array of 10 chars.

char (*)[9] is a pointer to an array of 9 chars, not 10. (At some
point, you gotta stop incrementing.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top