Solution for Exercise 02_05-modify.c
02_05-modify.c
#include <stdio.h>
int main()
{
char string[] = "become";
char *s;
/* initialize the pointer! */
s = string; /* no ampersand needed */
printf("%s to ",string);
s += 3; /* 4th letter */
*s = 'a';
printf("%s\n",string);
return 0;
}
Output
become to became
Notes
* Array string[] contains the string "become".
* Pointer s is the same data type as string[], char.
* When pointer s is assigned to string[], no & operator is required; this is due to the special relationship between arrays and strings.
* The fourth letter of string[] is at offset 3. To modify this position, the value of pointer s is increased by three: s += 3; This expression adds three to the address stored in s, which references the location of the 4th letter, 'o'.
* Then the dereferencing operator (*) is used to alter the character at address s: *s = 'a'; Letter 'a' is assigned to the memory location held in pointer variable s. At this point, variable s no longer references the start of the string.
Copyright © 1997-2025 by QPBC.
All rights reserved
