The difference char*between char*
the pointer and char[]char[]
the array is how you interact with them after you create them.
If you are just printing the two examples, it will perform exactly the same. They both generate data in memory, {h,e,l,l,o,/0}{h, e, l, l, o, /0}
.
The fundamental difference is that in one char*char*
you are assigning it to a pointer, which is a variable. In char[]char[]
you are assigning it to an array which is not a variable.
char[]char[]
is a structure, it is specific section of memory, it allows for things like indexing, but it always will start at the address that currently hold's 'h'holds 'h'
.
char*char*
is a variable. It was initializeinitialized with a number, but we can change this number using mathematical operators such as ++++
, because it is essentially an integer.
So here's one example, where the pointer would be much more efficient than an array. Say, for whatever reason we wanted the string to say "ello""ello"
instead of "hello""hello"
. With a pointer all we need to do is to shift the pointer one position to the "right".
char* p = "hello"; p++;
char* p = "hello";
p++;
This is a very fast operation and runs in Big O of 1 (literally, in this case, i it is onea single very fast operation)
But with char[]char[]
, we can't change where the array starts, therefore we actually therefore need to do something much less efficient, we. We need to loop through the entire word and for every index change the char in memory. It would look something like this. (manually done instead of looping for clarity)
char s[] = "hello"; s[0] = 'e' s[1] = 'l' s[2] = 'l' s[3] = 'o' s[4] = '/0'
char s[] = "hello";
s[0] = 'e';
s[1] = 'l';
s[2] = 'l';
s[3] = 'o';
s[4] = '/0';
This is a far slower operation running in "big O of n".