+ 1

Can anyone explain this error in c

Which of the line gives an error in the below code snippet and why?? #include<stdio.h> main() { const char* foo = "hi"; //line1 foo ="hello"; //line2 foo[0]=1; //line3 }

8th Dec 2019, 2:58 PM
Akshay Jain
Akshay Jain - avatar
3 Réponses
+ 9
Akshay Jain You declared foo as pointing to a const char, so you can't change it. And you are assigning value to const char which is an predefined read only memory so that's why line 3 containing error second error which rises from this is integer to character assignment which may leads to problem so the line 3 is which contains error and sane instance in line 2 where only pointer is pointing to hello so it will print hello so only line 3 which contains error
8th Dec 2019, 3:02 PM
GAWEN STEASY
GAWEN STEASY - avatar
+ 8
Akshay Jain yeah the error is in 3 line that's an typo look answer 2 line is pointer is pointing to other string and line is assigning the constant read only memory location which is cause of error
8th Dec 2019, 3:12 PM
GAWEN STEASY
GAWEN STEASY - avatar
+ 1
GAWEN STEASY Thanks for the answer but on compiling the error occurs only in third line not in second one. This is a bit confusing then!
8th Dec 2019, 3:11 PM
Akshay Jain
Akshay Jain - avatar