0

In case of pointers *p means value stored and p means the address (for a declaration int *p).
As per the below declaration, name points to the string "Example". So *name would be "E" and NOT an address, but an actual value. So how does the program below work? I mean, we are incrementing the value itself and not the pointer (confused).

char *name="Example";
while(*name !='\0'){
    printf("%c\n",*name++);
}

Prints

    E
    x
    a
    m
    p
    l
    e 
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
redsoxlost
  • 1,215
  • 5
  • 19
  • 32
  • 4
    operator precedence – Justin Jun 13 '17 at 19:43
  • See http://en.cppreference.com/w/cpp/language/operator_precedence – Jesper Juhl Jun 13 '17 at 19:45
  • "name points to the string "Example"" - No. `name` points to the first `char` of the array. C does not have a string type. Other assumptions of your's are also not correct (or badly formatted - use markdown!) – too honest for this site Jun 13 '17 at 19:45
  • @JesperJuhl: Please don't link C++ sites for C questions! – too honest for this site Jun 13 '17 at 19:46
  • https://stackoverflow.com/a/17077660/315052 – jxh Jun 13 '17 at 19:48
  • 1
    http://en.cppreference.com/w/c/language/operator_precedence – Keith Thompson Jun 13 '17 at 19:49
  • @Olaf the question had a C++ tag at the time I posted that comment. – Jesper Juhl Jun 13 '17 at 19:49
  • 1
    `*name` is 'E' (a `char` value), not `"E"` (a string). But @Olaf, a *pointer to a string* is by definition a pointer to its initial character (C11 7.1.1p1). – Keith Thompson Jun 13 '17 at 19:50
  • `name` points to a single character *at a time*. By incrementing it, successive values of `name` point to successive `char` elements of the string. – Keith Thompson Jun 13 '17 at 19:52
  • @KeithThompson So if I use " instead of ' does C treats them differently? I mean apart from string or char – redsoxlost Jun 13 '17 at 19:52
  • 1
    @redsoxlost: What do you mean "apart from string or char"? That *is* the difference. `'E'` is a character constant. (For historical reasons, it's of type `int` in C; in C++ it's of type `char`.) `"E"` is a string literal. It's of type `char[2]` in C, `const char[2]` in C++ (2 because of the terminating null character `'\0'`). – Keith Thompson Jun 13 '17 at 19:54
  • 1
    @redsoxlost: Yes, enclosing materials in single vs double quotes makes a lot of difference in C. When asking questions, be very careful about which you use — the difference matters! And I didn't enclose the "E" in the question in back-ticks because then your statement would have been definitively wrong rather than just 'mostly wrong'. Casual use of single quotes vs double quotes is OK (up to a point) in written English; it is not OK in text discussing C. – Jonathan Leffler Jun 13 '17 at 19:54
  • @KeithThompson: It does not change what I wrote. And this viewpoint is way more clear and uniform than dealing with the term "string" (actually _string literal_ here). A beginner should first understand C-strings are just `char[]` with a special convention. – too honest for this site Jun 13 '17 at 20:03
  • if you declare char *name="Example"; compiler put \0 at the end of name that indicates end of string. see:https://stackoverflow.com/questions/4955198/what-does-dereferencing-a-pointer-mean this is only reason that this *name !='\0' in while condition is true. In last iteration of while loop (7) after printf prints last 'e' in "Example", *name++ second part (++ increase the pointer index) and now name points to \0, and while condition is False, brakes. – EsmaeelE Jun 13 '17 at 21:28

3 Answers3

0

You misinterpreted the *name++ expression: even though ++ follows *name, it applies to name alone, not *name, because operator ++ has higher precedence than pointer dereference operator *.

This rule of precedence is very commonly used in C programs. For example, K&R's one-line strcpy implementation uses this expression twice:

void strcpy( char* dest, const char *src) {
    while (*dest++ = *src++)
        ;
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

The incrementing name++ has a higher precedence than the de-referencing. *name.

So it's basically: *(name++). As opposed to: (*name)++

Rivasa
  • 6,510
  • 3
  • 35
  • 64
0

As you can see from the table at http://en.cppreference.com/w/c/language/operator_precedence , postfix ++ has higer precedence than dereference *. So what happens is that first name++ is evaluated which increments the pointer but returns the original address that operator* (dereference/indirection) is then invoked on.

So the effect is that you dereference the current value of the pointer but the pointer is subsequently incremented.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70