Dear All,
I have a question about array which let me confused and I think it needs deep understanding of arrays.
I would like to know why it is possible to use array of char as pointer without a fixed size and also without dynamically allocating memory for it and that is not valid for all other types of data (int,...).
For Example:
char *c; c="END"; c="BEGIN"; No errors
while int *d; d={1,2,3,4,5}; Gives an error

in fact to use d, an allocation of memory has to be done and then filling the array must to be through filling element by element while using c is so simple!!

AND ALSO, one an array of integers is initialized can't be modified except by accessing its element. But as in the above example string c is modified with no problem

So both should be considered the same in theory but actually they are not. I hope that you can help with explanation as
1)how the compiler deals with char *
2)and how memory is allocated to "END" then it is changed to "BEGIN", is that can overwrite used memory

Thank you in advance.

For Example:
char *c; c="END"; c="BEGIN"; No errors

"END" and "BEGIN" are by definition allocated sequential memory during compile time. Then c="END"; simply loads the address where "END" is defined into the pointer c. Same with c="BEGIN";. It's simply loading a new address into the pointer.

while int *d; d={1,2,3,4,5}; Gives an error

In this case you are attempting to load 1 thru 5 which are not allocated in sequential memory but are simply values and this syntax does not equate to a memory address.

Further:
"END" is defined as the sequence 'E','N','D','\0' by the compiler. But, where c="END"; is valid, c={'E','N','D','\0'}; which looks like it should be the same but is not, for the same reason above.

in fact to use d, an allocation of memory has to be done and then filling the array must to be through filling element by element while using c is so simple!!

AND ALSO, one an array of integers is initialized can't be modified except by accessing its element. But as in the above example string c is modified with no problem

As I mentioned, modifying the string is done by loading another string address into the pointer. The integers are not pointed to by a pointer.

To replicate the char *c; c="END"; c="BEGIN"; concept with integers would be:

char *int1 = {1,2,3,4,5};  // this would replicate "END"
char *int2 = {9,8,7,6,5};  // this replicates "BEGIN"
char *c = int1;            // this is c="END"; 
char *c = int2;            // this is c="BEGIN";

The difference is the system itself keeps track of string pointers that are automatically created ("END") but there is no automatic way to generate a sequence of numbers so you have to create the pointer yourself.

For the rest of your post you can see why your 'theory' is in error.

commented: Thank you very much for your help and sorry for my late thanks +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.