2

I'm currently learning C through "Learning C the Hard Way"

I am a bit confused in some sample code as to why some arrays must be initialized with a pointer.

 int ages[] = {23, 43, 12, 89, 2}; 
 char *names[] = {
 "Alan", "Frank",
 "Mary", "John", "Lisa"
 }; 

In the above example, why does the names[] array require a pointer when declared? How do you know when to use a pointer when creating an array?

asked Nov 8, 2011 at 6:46

5 Answers 5

5

A string literal such as "Alan" is of type char[5], and to point to the start of a string you use a char *. "Alan" itself is made up of:

{ 'A', 'L', 'A', 'N', '0円' }

As you can see it's made up of multiple chars. This char * points to the start of the string, the letter 'A'.

Since you want an array of these strings, you then add [] to your declaration, so it becomes: char *names[].

answered Nov 8, 2011 at 6:54
Sign up to request clarification or add additional context in comments.

1 Comment

"string literal such as "Alan" is of type char *" - actually, it's of type char [length + 1].
3

Prefer const pointers when you use string literals.

 const char *names[] = {
 "Alan", "Frank",
 "Mary", "John", "Lisa"
 }; 

In the declaration, name is a array of const char pointers which means it holds 5 char* to cstrings. when you want to use a pointer, you use a pointer, as simple as that.

Example:

const char *c = "Hello world";

So, when you use them in an array, you're creating 5 const char* pointers which point to string literals.

answered Nov 8, 2011 at 7:00

Comments

2

Because the content of the array is a char*. The other example has an int. "Alan" is a string, and in C you declare strings as char pointers.

answered Nov 8, 2011 at 6:48

Comments

2

In the case of char *names[] you are declaring an array of string pointers.

a string in C e.g. "Alan" is a series of characters in memory ended with a 0円 value marking the end of the string

so with that declaration you are doing this

names[0] -> "Alan0円"
names[1] -> "Frank0円"
...

then you can use names[n] as the pointer to the string

printf( "%s:%d", names[0], strlen(names[0]) );

which gives output "Alan:4"

answered Nov 8, 2011 at 6:51

Comments

2

The use of "array of pointer" is not required.

The following will work as well. It's an array of 20 byte character arrays. The compiler only needs to know the size of the thing in the array, not the length of the array. What you end up with is an array with 5 elements of 20 bytes each with a name in each one.

#include <stdio.h>
char names[][20] = {
 "Alan", "Frank",
 "Mary", "John", "Lisa"
};
int main(int argc, char *argv[])
{
 int idx;
 for (idx = 0; idx < 5; idx++) {
 printf("'%s'\n", names[idx]);
 }
}

In your example the size of the thing in the array is "pointer to char". A string constant can be used to initialize either a "pointer to char" or an "array of char".

answered Nov 8, 2011 at 23:59

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.