0

So I tried this code

#include <stdio.h>
int main(void)
 {
        char string[] = "hello world";
        char *my_ptr = string;
        *my_ptr='Y';
        printf("the first char of string is %c", *my_ptr);
 }

OUTPUT_1 :-

the first char of string is Y

Now if I want to print the complete scentence in the string ("Yello world"). For that I changed 7th line to :-

printf("the whole string is %s", *my_ptr);

OUTPUT_2:-

Segmentation fault (core dumped)

But if I try changing it to this :-

printf("the whole string is %s", my_ptr);

OUTPUT_3 :-

the whole string is Yello world
  1. Could someone please explain me why are the second case is failing ? AND

  2. Why the third case prints correct ?

From my understanding *my_ptr (as well as my_ptr both) have the address of the first location, so why does the first one fail in printing a complete string , whereas the second one does well. Im a beginner so it would help if you could detail the reason behind such a behaviour in these cases.

0

5 Answers 5

2

my_ptr is of type char * it's a pointer on the first char of the string.

*my_ptr is of type char it's a character.

printf format string option %s takes a char *, it will loop over each character until it finds a string delimiter (0) :

First, *my_ptr, being Y

Then *(my_ptr + 1), being h

And so on...

When using printf with *my_ptr, The content of *my_ptr will be passed to printf as if it was a string pointer. Its value is 'Y' which is 89 in ascii.

printf will try to access the pointer at address 89 thinking it's a valid string pointer, but this address is most likely not readable and the kernel will kill the program trying to access memory it doesn't have access to.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot pal. You wrote "my_ptr" is of "type char *". Is it because the way we are declaring it "char *my_ptr "
0

This will work:

#include <stdio.h>
int main(void)
 {
        char string[] = "hello world";
        char *my_ptr = string;
        *my_ptr='Y';
        printf("the first char of string is %c", *my_ptr);
        printf("the whole string is %s", my_ptr);
 }

my_ptr is a pointer to the entire string. *my_ptr is the value of the char at the beginning of the string.

printf("%s", char*)
printf("%c", char)

Comments

0

In the statement below:

     printf("the whole string is %s", *my_ptr);

it will read the content from the address of *my_ptr. That produces Segmentation fault (core dumped) While in below:

     printf("the whole string is %s", my_ptr);

The string will be read from the base address of string[ ]. To read the string you have to pass the base address from where character should be started to read untill '\0' character is found.

Comments

0

The reasaon is in C, %s is used for printing the string but u uses that to print the char which results in Core dump.

And in C, it is enough to give the base address to print the whole contents, no need to for using *addr.

If u want to access a particular character u can do so by *(a+0) for printing 1st char and *(a+1) for printin 2nd character and so on.

Comments

0

This:

printf("the whole string is %s", *my_ptr);

dereferences the pointer, so it passes a value of type char to printf(), which will then interpret it (due to the %s formatting specifier) as const char *, i.e. as a pointer to read-only character data. The value of a pointer is an address of a location in memory where some data is stored.

This will make printf() start reading characters from some very low address, where your program is not likely to be allowed to read. Thus the segmentation fault.

1 Comment

Thanks unwind. Im really sorry but what is meant by "const char *" ?

Your Answer

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