# include<stdio.h>
int main() {
char g1[7] = “4567”, *q;
q = g1 + 1;
*q = ‘0’;
printf(“%s”, g1);
}
- In this program
g1[] array
is available in the stack.
And hence modifiable (Write allowed)
q
is just a helper pointer.
One example to your doubt:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char const *argv[]) {
char *old1 = "helloworld1";
char old2[] = "helloworld2";
char *dst1 = old1 + 5;
char *dst2 = old2 + 5;
char *src = "windows";
//strcpy(dst1,src); // DOES NOT WORK
strcpy(dst2,src);
printf("old1 = %s\n",old1);
printf("old2 = %s\n",old2);
return 0;
}
O/P:
old1 = helloworld1
old2 = hellowindows
- using
old2[]
makes the string literal
modifiable because it is in the stack.
- But using the character pointer only string literal stored in read-only area of memory, which becomes nonmodifiable.
strcpy
working