+ 1

I want to know the how is output come....

//am beginner in C language #include <stdio.h> int main() { char s1[7]="1234" ,*p; p=s1+2; *p='0'; printf("%s", s1); } //Output : 1204 https://code.sololearn.com/cwXSkYOEOC2Y/?ref=app

15th Nov 2021, 4:09 PM
born2code
born2code - avatar
3 Answers
+ 3
Remember to calculate offset when assigning memory address to a pointer. Knowing <s1> content and indices 0 1 2 3 <- index 1 2 3 4 < value The following line ... p = s1 + 2; Sets pointer <p> to the address of <s1> plus 2 characters. At this point, <p> points to address of 3rd character in <s1> (index 2) which is '3'. *p = '0'; And the line above modifies the data stored at the memory address of the 3rd character from its original value '3', to '0'.
15th Nov 2021, 4:32 PM
Ipang
+ 2
If you are asking for the explanation of the output: When you create a char pointer and assign it the memory address of s1+2, you are basically assigning the memory address of the third value in the character array which is '3'. So when you change the value with *p='0'; You are changing the value of character array from "1234" to "1204"
15th Nov 2021, 4:20 PM
Ashish Neupane
Ashish Neupane - avatar
+ 2
Ipang Ashish Neupane thank u so much for helping me🙏🙏
15th Nov 2021, 4:34 PM
born2code
born2code - avatar