12

Let's assume I have the following code in my C program:

#include <stdio.h>

void PrintSomeMessage( char *p );

int main(int argc, char *argv[]) {
    char arr[10] = "hello";
    PrintSomeMessage(&arr[0]);
    return 0;   
}

void PrintSomeMessage(char *p)
{
    printf("p: %s",p);
}

Why the output of this would be the whole word "hello" instead of a single character "h"?

I understand, though, that if I put a "%c" in the formatter, it will print just a single letter. But still, the memory address for each letter in this address is different. Please, someone explain it to me?

alk
  • 69,737
  • 10
  • 105
  • 255
  • 1
    [Why don't I need to dereference a character pointer in C before printing it?](http://stackoverflow.com/questions/15841696/why-dont-i-need-to-dereference-a-character-pointer-in-c-before-printing-it/15841740#15841740) – Grijesh Chauhan Jun 06 '13 at 12:32
  • note: `%c` doesn't need address to print where as `%s` need to travel untill `\0` as @Rohan explained in answer. – Grijesh Chauhan Jun 06 '13 at 12:43
  • @SuvP `printf()` with `%c` need **value** variable, its quite obvious/logical that it shouldn't need address to access value for **single** element(can be pass by value). Where are to print string it need address to travel until `\0` encounter same Rohan explained in his answer. Additionally to store in memory scanf needs address even with `%c` – Grijesh Chauhan Jun 06 '13 at 12:50
  • @SuvP To `printf()` a `%c` you at least need a `char` variable, not its address. – alk Jun 06 '13 at 12:51
  • 2
    Note, `&arr[0]` ≡ `&*(arr+0)` ≡ `&*arr` ≡ `arr`, so `PrintSomeMessage(&arr[0])` ≡ `PrintSomeMessage(arr)` – Vovanium Jun 06 '13 at 13:14
  • Basically the same question as http://stackoverflow.com/questions/16909490/pointers-with-character-array-in-c – AnT stands with Russia Jun 06 '13 at 13:59

2 Answers2

27

But still, the memory address for each letter in this address is different.

Memory address is different but as its array of characters they are sequential. When you pass address of first element and use %s, printf will print all characters starting from given address until it finds '\0'.

Rohan
  • 52,392
  • 12
  • 90
  • 87
5

Incase of arrays, the base address (i.e. address of the array) is the address of the 1st element in the array. Also the array name acts as a pointer.

Consider a row of houses (each is an element in the array). To identify the row, you only need the 1st house address.You know each house is followed by the next (sequential).Getting the address of the 1st house, will also give you the address of the row.

Incase of string literals(character arrays defined at declaration), they are automatically appended by \0.

printf prints using the format specifier and the address provided. Since, you use %s it prints from the 1st address (incrementing the pointer using arithmetic) until '\0'

Spike0xff
  • 1,246
  • 1
  • 15
  • 24
Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59
  • seems like I misclicked while scrolling somehow when I was looking through this post yesterday; I just found out now by looking at my profile.Unfortunately it wont let me reverse the mistake without an edit as my vote is locked. – Asura Jul 05 '16 at 00:35