Vishwanath Dalvi
Vishwanath Dalvi

Reputation: 36621

what is the technical difference between these declarations?

  char amessage[] = "now is the time"; /* an array */
  char *pmessage = "now is the time"; /* a pointer */

Upvotes: 2

Views: 270

Answers (4)

David Victor
David Victor

Reputation: 827

char amessage[] = "now is the time"; /* an array */
char *pmessage = "now is the time"; /* a pointer */

In C the thing to the left of '=' is termed an 'lvalue'. The type is defined solely by the 'lvalue'. So 'amessage' is a char array. 'pmessage' is a pointer to a char.

In C more goes on in relation to what is equivalent between array type & 'char *' accesses, and also what is not.

As declared 'pmessage' is a modifiable 'lvalue' - 'amessage' is not modifiable. Hence the advise to declare 'pmessage' as const as really it should not be modifiable also.

E.g.

const char* const pmessage = "...";

In terms of memory access 'amessage' results in a direct access, whereas 'pmessage' requires a dereference.

Note: the C compiler only permits initialisation of 'char *' at compile time via a string literal.

E.g. If you had

1. {
2.     int j = 1;
3.     int *k = &j;
4.     int *i = 1;
5. }

Line 4 makes no sense & would be illegal.

Highly recommend Peter Van Linden's 'Deep C Secrets' which he has a whole chapter about C arrays & pointers.

Upvotes: 0

Foo Bah
Foo Bah

Reputation: 26271

if you are using GCC, turn on -Wwrite-strings. fixed strings are of type const char[length_of_string], and the conversion to a char * will elicit a warning [needs to be const].

The first assignment is a character array assignment, whereas the second assignment is a pointer-based assignment (and the resulting string is held as a fixed string)

The first assignment is acceptable as-is, whereas the second one requires a const qualifier.

In the first assignment, changing a point is acceptable (e.g. amessage[3] = 'q'). in the second assignment, changing a point is unacceptable (since the string is const ) -- you should get a bus error

Upvotes: 0

Brooks Moses
Brooks Moses

Reputation: 9527

In addition to James McNellis's answer, amessage is one thing -- an array of 16 characters on the stack that happens to contain the string "now is the time" (because they are copied there when amessage is created). You can change what it contains.

On the other hand, pmessage is two things -- a string literal (which is stored in a non-writable portion of memory), and a pointer. You can't change the contents of the string literal, but you can change what the pointer points to, and make it point at a different string. And you can use the string literal as a target of other pointers.

(In some sense, this is not entirely true -- amessage also involves a string literal, as it's where the contents are copied from when it's created. But you can't do anything else with that string literal afterwards.)

Upvotes: 0

James McNellis
James McNellis

Reputation: 355069

amessage is of type char[16]. It is an array. The elements of the array contain the sixteen characters from the string literal.

pmessage is of type char*. It is a pointer. It points to a (non-modifiable) array containing the sixteen characters from the string literal. (You should avoid using a char* to point to a string literal; doing so is evil. You should always use a const char* wherever possible when referring to string literals.)

Upvotes: 4

Related Questions