-5

I know similar questions have been asked, but this one is specific to arrays.

I can do this:

char *names[] = { "John", "Paul", "George", "Ringo" };

and then:

printf("%s\n", names[0]);

So why does this not work?:

int *numbers[] = { 11, 12, 13, 14 };

printf("%d\n", numbers[0]);

Thanks

4
  • It would if you'd drop that asterisk. It's not there for show, it changes the type of the array elements. Commented Dec 17, 2017 at 14:16
  • 1
    You don't have a char array, you have an array of pointers to char. Commented Dec 17, 2017 at 14:17
  • 1
    Also please describe "does this not work"! Commented Dec 17, 2017 at 14:17
  • @StoryTeller yeah I'm not completely dumb, I get that the asterix isn't for show. I wanted to understand pointers better and was confused why I can create an array of pointers to chars and not an array of pointers to ints Commented Dec 17, 2017 at 14:51

3 Answers 3

2

String literals have types of character arrays. For example the string literal "John" has type char[5] (string literals include the terminating zero).

Used in expressions arrays with rare exceptions are converted to pointers to their first elements.

You can imagine this declaration

char *names[] = { "John", "Paul", "George", "Ringo" };

like

char *names[] = { &"John"[0], &"Paul"[0], &"George"[0], &"Ringo"[0] };

Thus the array names is initialized by valid addresses of first characters of the string literals.

As for this declaration

int *numbers[] = { 11, 12, 13, 14 };

then the array numbers is initialized by invalid addresses values like 11, 12 and so on that do not point to actual objects.

You could write for example

int i = 11, j = 12, k = 13, l = 14;

and then

int *numbers[] = { &i, &j, &k, &l };

In this case the array would be initialized by valid addresses.

As for the printf calls then the function is designed such a way that when the conversion specifier s is encountered the function considers the corresponding argument as an address of a zero-terminating string and tries to output this string.

While the conversion specifier d serves to output objects of the type int. So for example if you want to output the integer pointed to by numbers[0] then you should write

printf("%d\n", *numbers[0] );
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I think that clears it up for me. So basically, the reason it works with chars is because the strings are actually just arrays, and the C program sees the beginning of those arrays as valid addresses. Whereas when we point at ints, because they are not arrays, the C program sees this as an invalid address. Not sure if my own deduction is spot on but definitely becoming clearer. Thanks!
@RyanMc Yes, you are right.
0

"John", … are of type char *.

11, … are of type int (without indirection).

So it is:

int numbers[] = { 11, 12, 13, 14 };
printf("%d\n", numbers[0]);

1 Comment

""John", … are of type char *" - Not quite
-1

This question is explained here.

In the first example each name is an array and you have an array of arrays.

char *names[] = { "John", "Paul", "George", "Ringo" };

In the second example with int you have a simple array and using *var[] = **var but you don't have such ting in memory.

int *numbers[] = { 11, 12, 13, 14 };

1 Comment

If it's a duplicate, why re-iterate it? SO doesn't work like this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.