Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

See the belowcode char * s = \"test\"; *s = \'p\'; The second line where I am tr

ID: 3617148 • Letter: S

Question

See the belowcode
char * s = "test";
       *s = 'p'; The second line where I am trying to derefernce the pointerand assign a new value doesn't work. What is wrong with thisstatement? The program stops working as sson as it reaches thesecond line. But if you write the code like below Char s[] = "test"; *s = p; This one works. What's the difference between the two? See the belowcode
char * s = "test";
       *s = 'p'; The second line where I am trying to derefernce the pointerand assign a new value doesn't work. What is wrong with thisstatement? The program stops working as sson as it reaches thesecond line. But if you write the code like below Char s[] = "test"; *s = p; This one works. What's the difference between the two? The second line where I am trying to derefernce the pointerand assign a new value doesn't work. What is wrong with thisstatement? The program stops working as sson as it reaches thesecond line. But if you write the code like below Char s[] = "test"; *s = p; This one works. What's the difference between the two?

Explanation / Answer

//pointer contains the address of memory locations not thevariables. In this case you as assigning a char to a char pointers, which is not correct.

But if you write the code like below Char s[] = "test"; *s = p;
in the above example s[] array contains the value of "test" and *sis pointing towards the start of the array, in the case of arraythe pointer variable s will contains the address of starting ofarray, so this time at the start of array p is stored.