0

I'm having some trouble wrapping my head around this simple dereferencing statement.

I've tried printing **names, and then I get what I expect to get from *names -- 'C'. However, *names gives me 'D'.

#include <stdio.h>


int main(void)
{
   char *names[] = {"Carl", "David", "Gibson", "Long", "Paul"};
   printf("%c\n", *names);

   return 0;
}

The console prints out 'D'. I'm not sure why the resulting char from *names is not the first letter of the first item, 'C'.

7
  • 1
    This causes undefined behaviour due to using the wrong format specifier -- modern compilers will warn about this. Examples of correct code include printf("%s\n", *names); or printf("%c\n", **names); Commented May 20, 2019 at 4:41
  • 1
    turning on / reading the warnings is also a good idea Commented May 20, 2019 at 4:54
  • The %c format specifier expects a single character (an integer argument to printf). The %s format specifier expects a string (a char *). You're passing a char * to a %c specifier, which makes no sense. Your compiler should have warned about this (make sure your code is clear of warnings before posting here, unless you truly don't understand the warning, in which case that should be the title of your post). Commented May 20, 2019 at 5:01
  • If you did want to stick with your current method, you could try, for example: printf("%c\n", (*(names[1]+(sizeof(char)*2)))); where the 1 represents the second name in the array, and the 2 represents the letter within the name. This would output v from David. Or alternatively, just use %s ;). Commented May 20, 2019 at 5:20
  • 1
    @AndyJ leave the sizeof out, pointer arithmetic already scales by the element size. The only reason it isn't blowing up in your face is that sizeof(char) is guaranteed to be 1. Commented May 20, 2019 at 5:30

2 Answers 2

3

This is undefined behavior and the output varies with the compiler.
When I run this with gcc, there is no output. Using **names prints 'C'.
The undefined behavior is because of the wrong format specifier. You use %c, but *names point to the first element in the array, ie a char array storing "Carl".
Use the %s format specifier to print strings.

printf("%c\n", *names);
Sign up to request clarification or add additional context in comments.

Comments

3

When you compile this code, GCC will give you the following:

test.c:5:12: warning: format ‘%c’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat=]
   printf("%c\n", *names);
           ~^     ~~~~~~
           %s

So you're basically trying to print the first character of the first name, but instead of passing a char as an argument, your're passing a pointer to char. What you could do is this:

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

in which you specify that you want the first character from the first element.

Also, using **names is the same as using *names[0]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.