Character pointers and character arrays in c

Viewed 24

I have written a programme in c to modify elements of an array. Why am I not able to modify elements of an array declared using a pointer, the way is it possible to do using an array? Isn't it possible to reference values stored in the pointer using a variable in order to modify them? Like in the code below, I tried to modify the first element stored in t but I get a segmentation error.

#include <stdio.h>
#include <ctype.h>

int main(void)
{
    char s[] = "hello";

    s[0] = toupper(s[0]);
    printf("s is now: %s\n", s);

    char *t = "hello";
    t[0] = toupper(t[0]);
    printf(" t is now: %s\n", t);
}

What I get after running the code.

s is now: Hello
Segmentation fault (core dumped)
0 Answers
Related