Array vs pointer in C, very short questions?

This is a discussion on Array vs pointer in C, very short questions? within the C Programming forums, part of the General Programming Boards category; Which one (Array or pointer) is located in read only portions of memory ?
In other words
char* x = ...

Re: Array vs. Pointer

Which one (Array or pointer) is located in read only portions of memory ?

In other words

char* x = "hello";x[1] ='m';I get error here

but when I use array likechar x[] ="hello";x[1] ='m';I don't get error

why I get error when I use either one and I don't get error when I use array?

Hi Amir,

When you assign a character string pointer (char* x = "hello"), it's really just a pointer pointing to the memory location of "hello". As such, the string is read only. In your first example, you're treating the pointer as if it were an array, when it's really just a pointer to a character array.

To change the string, you would need to copy it (or initialize it) in an array and change it like you did in your second example.

I don't think that this is an appropriate situation for malloc(). Hope this helps.