1

Let's say I have

char *names[] = { "Tom", "Jerry" };

and I want to print the "e" in "Jerry" using printf. My first instinct was

printf("%c\n", *names[5]);

but when I applied what I've been learning about pointers, I realized this is total junk code because the 5 refers to the nonexistent fifth pointer in names, not the "e" in "Jerry". The pointers contained in names will only ever refer to the memory addresses of the first characters in their respective strings.

So it seems what I really need to do is to add one byte to names[1] to point to, and print the "e" in "Jerry". But I'm not sure how to do this, or whether it's even allowed in C.

What is the best way to accomplish this? Thank you in advance.

alk
  • 69,737
  • 10
  • 105
  • 255
intelli78
  • 137
  • 1
  • 8

6 Answers6

6

I think what you're looking for is printf("%c\n", names[1][1]);.

Fred Larson
  • 60,987
  • 18
  • 112
  • 174
6

The thing is that you don't have a multi-dimensional array, you have a single-dimension array containing pointers to arrays.

In memory your array looks something like this:

+----------+----------+
| names[0] | names[1] |
+----------+----------+
  |          |
  |          V
  |          +---------+
  |          | "Jerry" |
  |          +---------+
  V
  +-------+
  | "Tom" |
  +-------+

The above image should make it clear that when you do *names[5] you first of all must remember that the compiler deciphers that as *(names[5]), which means that you try to dereference the sixth entry in an array of only two entries. That will lead to undefined behavior.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Indeed, it causes a segmentation fault. But, help me understand... if the array is not multidimensional, then why are we using this names[1][1] syntax? Isn't that indicative of a multidimensional array? – intelli78 Oct 06 '14 at 14:04
  • @intelli78 Because you can use array-indexing syntax for both array and pointers. Indeed, doing e.g. `names[0]` is actually equivalent to `*(names + 0)` (i.e. it's using pointer arithmetic), so pointer and arrays are pretty much interchangeable (there are of course exceptions). – Some programmer dude Oct 06 '14 at 14:10
2

Take the second string: names[1], add one to point to the second character: names[1]+1, and dereference to get what you point at: *(names[1]+1), which also equals to names[1][1]

names[1] -----v
names[1]+0 ---v
              v
             "Jerry"
               ^
names[1]+1 ----^

*(names[1]+1) == 'e'
names[1][1] == 'e'
2501
  • 25,460
  • 4
  • 47
  • 87
1

Access the jth character of the ith string as (0-indexed) : names[i][j];

Sinstein
  • 887
  • 11
  • 42
1

names[ nameIndex ][ characterIndex ] So if you want to print the "e" of "Jerry" : printf("%c\n", names[1][1]); Or the "m" of "Tom" : printf("%c\n", names[0][2]);

pascx64
  • 904
  • 16
  • 31
0

the best way with pointers to print 'e'(second char of value) of Jerry(second value in the array) is *(array[1]+1) (1 for second value and char because we start with 0 in c )

rtroulak
  • 459
  • 1
  • 8
  • 16