7

The first block

#include <stdio.h>

const int MAX = 3;

int main() {

    int  var[] = { 10, 100, 200 };
    int i, *ptr[MAX];

    for (i = 0; i < MAX; i++) {
        ptr[i] = &var[i]; /* assign the address of integer. */
    }

    for (i = 0; i < MAX; i++) {
        printf("Value of var[%d] = %d\n", i, *ptr[i]);
    }

    return 0;
}

Easy to understand, since ptr is an array of int pointers. So when you need to access the i-th element, you need to dereference its value as *ptr[i].

Now the second block, just the same, but now it points to an array of char pointer:

#include <stdio.h>

const int MAX = 4;

int main() {

    char *names[] = {
        "Zara Ali",
        "Hina Ali",
        "Nuha Ali",
        "Sara Ali",
    };

    int i = 0;

    for (i = 0; i < MAX; i++) {
        printf("Value of names[%d] = %s\n", i, names[i]);
    }

    return 0;
}

This time, when we need to access its element, why don't we add a * first?

I tried to form a correct statement to print this value, seems if you dereference, it will be a single char. Why?

printf("%c", *names[1]) // Z

I know there is no strings in C, and it is a char array. And I know pointers, but still don't under the syntax here.

1
  • Because the element that we need to access is an address. The %s tells printf to print all the characters starting at the given address, and until encountering 0. In your example, the given address is the value of names[i]. The %c tells printf to print the given character.In your example, the given character is the value of *names[i], which is equivalent to names[i][0]. Commented Aug 1, 2016 at 14:17

4 Answers 4

4

For printf() with %s format specifier, quoting C11, chapter §7.21.6.1

s
If no l length modifier is present, the argument shall be a pointer to the initial element of an array of character type. [...]

In your case,

 printf("Value of names[%d] = %s\n", i, names[i] );

names[i] is the pointer, as required by %s. That is why, you don't dereference the pointer.

FWIW,

  • %c expects an int type argument (converted to an unsigned char,), so you need to dereference the pointer to get the value.
  • %d also expects an int argument, so you have to go ahead and dereference the pointer, as mentioned in the question.
Sign up to request clarification or add additional context in comments.

Comments

1

The %d conversion specifier expects its corresponding argument to have type int; ptr[i] has type int *, so the dereference is necessary.

The %s conversion specifier expects its corresponding argument to have type char *; that is, a pointer to char. So names[i] is already of the correct type. The %s specifier tells printf to print the sequence of characters starting at the given location until it sees the string terminator.

Comments

0

You would use the same syntax in the second example if you were printing the first character of each array of char:

    printf("Value of names[%d] = %c\n", i, *names[i]);

But since you want to print the C strings, you pass the values of the elements of the names array, which are of the proper type, char *.

Comments

0

printf() has it's own way of dealing with format type specifiers. In case of strings, that is, array of characters, it requires the %s specifier and a pointer of type char * as a corresponding argument. When printf() receives this pointer, it dereference it discreetly to print the characters that are of type char. So, that is why you pass a pointer and don't dereference when you want to print strings.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.