Why it fails?
-
char *p= "test string";
p[5] = '_';This code will fail. why? My Assumption: "test string" remaining in memory as a constant pointer, hence it modifying teh same fails. it would be helpful if you let me know how compiler handling such definitions and how it resides in memory
SaRath.
_"Where I am from, there is no plan B. So, take advantage of today becuase tomorrow is not promised. - 50 Cent"
-
char *p= "test string";
p[5] = '_';This code will fail. why? My Assumption: "test string" remaining in memory as a constant pointer, hence it modifying teh same fails. it would be helpful if you let me know how compiler handling such definitions and how it resides in memory
SaRath.
_"Where I am from, there is no plan B. So, take advantage of today becuase tomorrow is not promised. - 50 Cent"
-
char *p= "test string";
p[5] = '_';This code will fail. why? My Assumption: "test string" remaining in memory as a constant pointer, hence it modifying teh same fails. it would be helpful if you let me know how compiler handling such definitions and how it resides in memory
SaRath.
_"Where I am from, there is no plan B. So, take advantage of today becuase tomorrow is not promised. - 50 Cent"
Sarath.This code will fail. why?
It's the difference between arrays and pointers. Had you used the following, it would have worked:
char p[] = "test string";
p[5] = '_'; -
Sarath.This code will fail. why?
It's the difference between arrays and pointers. Had you used the following, it would have worked:
char p[] = "test string";
p[5] = '_';Just an FYI: It is VERY bad to declare an array this way and then modify it. In this case, it won't hurt anything, but lets say you tried something like:
char p[] = "hello"; p[5] = ','; p[6] = ' '; p[7] = 'W'; p[8] = 'o'; p[9] = 'r'; p[10] = 'l'; p[11] = 'd';
Now you have overwritten parts in memory that were not allocated for that string. With few exceptions, you should always specify a size for your arrays:char p[30] = "hello, world!";
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
Just an FYI: It is VERY bad to declare an array this way and then modify it. In this case, it won't hurt anything, but lets say you tried something like:
char p[] = "hello"; p[5] = ','; p[6] = ' '; p[7] = 'W'; p[8] = 'o'; p[9] = 'r'; p[10] = 'l'; p[11] = 'd';
Now you have overwritten parts in memory that were not allocated for that string. With few exceptions, you should always specify a size for your arrays:char p[30] = "hello, world!";
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
I think, Null char to be added to p[12].
-
I think, Null char to be added to p[12].
You can think it, but it'd still be wrong.
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
-
I think, Null char to be added to p[12].
Adding NULL to p[12] would still cause a segmentation fault (aka crash). Since the array was not declared with any bounds, it defaults to the size of the initailized string (which is 6 for "hello"). Any character that is written past that size is overwriting something in memory that may or may not be important. If it overwrites, for example, the next instruction on the stack ... you get the idea. The point is, you should always declare arrays with a size.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac