Question:
Array vs pointer in C, short questions?
BackTrack5
2013-03-13 22:04:27 UTC
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 like
char x[] ="hello";
x[1] ='m';
I don't get error

Also when I use malloc I get error too
char* x =malloc(sizeof(char)*10);

-----------------------
So what is the difference if I use malloc and don't use like char x[] ="hello"; and why I get error when I use either one and I don't get error when I use array?
Four answers:
2013-03-14 08:56:32 UTC
char* x = "hello";



DOES NOT allocate memory, it simply creates a string literal in memory and puts the address of the first character in x.



When you declare a character array you can do it in 4 ways:

1) String Literal

Length of string = 5

char name[] = "Hank";

The null terminator is added automatically



2) char list

Length of string = 5

char name[] = {'H','a','n','k','\0'};

The null terminator must be added manually.



3) pointer

Length of string = 5

char* name = malloc(sizeof(char)*5);

strcpy(name,"Hank");



4) pointer literal

Length of string = 5

char* name = "Hank";

HOWEVER you cannot change the string now:

strcpy(name,"Billy"); will cause a segfault because you didn't allocate memory to write to.



In both instances name is just a pointer to the beginning of the array, as such:

printf("%c",*name) will print the first character. There are no 'Strings' in C, just arrays of characters. The Null terminator is used to end the array.



Without the null terminator (that's the '\0') you will run into MANY MANY segfaults (A segfault is when your program accesses memory outside of it's boundaries).



The difference between using pointers and using arrays lie in the fact that arrays are static, meaning once your program is compiled you can't change the size of an array. You can however change the size of an array if it was creating using pointers and malloc. The downside to this is that you become responsible for "Freeing" your own memory.



Here's some code examples to show you what I mean.

http://pastebin.com/MxXJQAiR
1337
2013-03-13 23:42:26 UTC
Nah, the compiler is too dump to translate "hello" into a binary value for you. What really happens is when you say char* x = "hello"; you are creating a character pointer in the method memory which is located on the stack, however the array of characters "hello" which x is pointing to is located in read only memory that is read directly from your exe file at startup. You can check it by printing x[0], it will print 'h'.



When you say char x[] ="hello"; on the other hand you are creating an array of "hello" characters in the method memory on the stack, which is why you can modify it. This goes on to show that char* x and char x[] are indeed different.



As for char* x =malloc(sizeof(char)*10); this statement shouldn't compile because malloc returns a void* and you are trying to use it as char*. You should cast your void pointer like this:

char* x = (char*)malloc(sizeof(char)*10);

Then x[0]='k'; will not give you an error because you pointer x is now actually pointing to a region in memory of the heap.
Little Princess
2013-03-13 23:27:24 UTC
quick guess... when you say char *x = "hello", you're declaring the variable x to be a pointer to a character and you initialize the value of x to be "hello". So, x is a pointer that points to the binary value of the characters h-e-l-l (only four bytes if it's on a 32 bit system). The compiler thinks you're trying to have x point to a fixed location in memory. The compiler's dumb and just lets you do what you tell it you want it to do. When you say x[1] = 'm' you're then saying that the byte that follows that spot you've defined in memory (at addresses h-e-l-l + 1) should be set to the character 'm'.



When you say char x[] = "hello" you're creating an array in memory and filling it with the letters h-e-l-l-o.
?
2016-10-30 18:49:39 UTC
i think of the question is greater, in case you get carry of a pointer to an array as a controversy, is it valid to reference the advice aspects utilising pArray[3] somewhat of *(pArray+3)? and, could you deliver the pointer to the 1st component once you call the function func(&arr[0]) or is basically func(arr) ok? and what's the version in doing those? i'm no longer fullyyt beneficial myself yet I study someplace that there became right into a distinction and so I regularly call applications utilising func(&arr[0]), and if I get carry of a pointer interior the function I dereference it utilising *(pArray+3).


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...