473,320 Members | 1,876 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,320 software developers and data experts.

why the code does not compile?

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
Nov 14 '05 #1
5 1316
yogesh wrote:

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.
Nov 14 '05 #2
yp*********@indiatimes.com (yogesh) writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #3
On 6 Mar 2005 11:11:19 -0800, yp*********@indiatimes.com (yogesh)
wrote:
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.

Nov 14 '05 #4
On Mon, 07 Mar 2005 05:52:11 +0000, Nick Austin wrote:
On 6 Mar 2005 11:11:19 -0800, yp*********@indiatimes.com (yogesh)
wrote:
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
Nov 14 '05 #5
Lawrence Kirby <lk****@netactive.co.uk> writes:
[...]
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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Hung Jung Lu | last post by:
Hi, I know people have talked about it before, but I am still really confused from reading the old messages. When I talk about code blocks, I am not talking about Lisp/Ruby/Perl, so I am not...
242
by: James Cameron | last post by:
Hi I'm developing a program and the client is worried about future reuse of the code. Say 5, 10, 15 years down the road. This will be a major factor in selecting the development language. Any...
1
by: Novice | last post by:
Hi all, I'm afraid this is the second posting of this information as I didn't get a response on the previous post. I will try to shorten my message (i.e. be more concise) in the hopes that it will...
67
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. ...
8
by: Kai-Uwe Bux | last post by:
Hi folks, in another thread there is some discussion about cloning smart pointers. So I started thinking about it and stumbled upon a rather simple generic implementation. However, it uses...
13
by: AFKAFB | last post by:
Hi Sometimes when i edit previously saved VBA code e.g. To update field names etc the revised code does not work. Even if i copy and paste in a previous version it does not work. The only...
13
by: R Reyes | last post by:
is there a way to avoid the validateRequest error page in my code behind file? i can't seem to find a way to handle the error w/o an error code or exception being thrown... i am NOT looking for...
13
by: Bob Jones | last post by:
Here is my situation: I have an aspx file stored in a resource file. All of the C# code is written inline via <script runat="server"tags. Let's call this page B. I also have page A that contains...
10
by: Tomás Ó hÉilidhe | last post by:
I'd post this on a gcc newsgroup but I'd be more productive talking to the wall. Anyway, let's say someone throws some source code at you for a particular program and says, "Just compile it, it...
15
by: colemanj4 | last post by:
Here is what I have so far, it loops while the PW is incorrect, or until cancel is selected. I want it to lock the tables for adds, deletes, and edits when cancel is selected, and if the PW is...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.