1

In the below code, we get a pointer from strdup(source) and we store it in a pointer named target. Now, when we print the string using pointer, we don't add * at the beginning of the pointer: why is it so? As I studied whenever we want to dereference any pointer we use *pointer_name. If we add * in the below code, we get an error. I am very beginner, so pls ans in easy words.

#include<stdio.h>
#include<string.h>
 
int main()
{
    char source[] = "Programming";
 
    char* target = strdup(source);
 
    printf("%s\n",target);
    return 0;
}
  • Where do you want to dereference a pointer in your code? If you dereference it, you only get a single `char`. `printf` would not know where the other characters of that string are. Therefore you don't dereference it but let `printf` dereference it. And if you don't dereference, you don't add a `*` anywhere. – Gerhardh Feb 23 '22 at 07:55

3 Answers3

4

printf expects a char pointer in the place of the %s specifier.

https://en.cppreference.com/w/c/io/fprintf

bolov
  • 72,283
  • 15
  • 145
  • 224
1
char* target = strdup(source);
printf("%s\n",target);

Why we don't use *target in the code above?

The explanation is quite simple, as already stated in previous answers: target has type char pointer, which is exactly what printf() wants in the above call.

Now, printf() is a little complicated because its semantic is not simple - basically it accepts zero or more arguments after the first, of any type (possibly applying promotion). But if we use strdup() again, maybe it is simpler:

char* target2 = strdup(target);

Here, if you wrote strdup(*target), the compiler might warn that you are passing a char instead of a pointer to char.

0

strdup() returns a char*, hence the char* type of target. target holds a pointer to the first character in an array of chars. printf("%s", string) expects string to be a char*, so there’s no reason to do anything to target; just pass it to printf().

If you dereferenced target, you would get a single char (P in this case). printf() would then complain that you had supplied a character instead of a string (pointer to character). Even worse, the program could compile, and then printf() would try to print the string at address P (0x50), which would result in probably unwanted behaviour.

When working with arrays—a string is a type of array—you rarely want to dereference the array.

tjcaul
  • 383
  • 1
  • 9
  • 1
    It's possible (maybe even probable) that `printf` wouldn't notice that you passed a `char` instead of a pointer, and the result would be much worse. – Mark Ransom Feb 23 '22 at 05:34
  • GCC gives me a warning, but not an error. I’ll update my answer to reflect that. `warning: format '%s' expects argument of type 'char *', but argument 2 has type 'int'` – tjcaul Feb 23 '22 at 05:40
  • I think the compiler emits "has type int" because by dereferencing a char* you obtain a char which can't be pushed on the stack, so it is promoted to int. – linuxfan says Reinstate Monica Feb 23 '22 at 08:09
  • @linuxfansaysReinstateMonica there's no problem with a char as a parameter type, but `printf` is special - as a [variadic function](https://en.cppreference.com/w/c/variadic) any char parameter is *always* promoted to int. – Mark Ransom Feb 23 '22 at 14:02