Re: Difference between Char* ptr and char arrCh 
- From: gordonb.8qx8o@xxxxxxxxxxx (Gordon Burditt)
- Date: Mon, 27 Jul 2009 16:38:33 -0500
I have a few queries regarding array of characters using array
notation and pointer notation.
[ #1 ]
Is there a difference in storage of global char* and char* inside a
consider the code:
char* globalCh = "MY STRING";
char* simpleCh = "My String";
What I understand is in the case of 'char* simpleCh' - the
pointer .i.e. ' *simpleCh' is stored in the stack,
The pointer is stored in automatic storage. Don't use the s-word,
as there's no guarantee that the machine has one.
while the value "
My String" is stored elsewhere in a data segment.
It's in static storage with a lifetime of the entire program
execution. It may or may not be read-only. The standard doesn't
recognize a d*** segment.
Taking the sizeof
(simpleCh) would be 4 bytes in my machine. Once the function exits "My
String" is no longer accessible.
It could be accessible if the pointer is returned or is otherwise
made reachable (e.g. by putting a pointer to the string into a
global) once the function returns. In the example, it's not.
Taking the sizeof(globalCh) = 4 bytes and where is the value ' MY
STRING ' and the pointer ' *globalCh ' stored ? since its a global
variable the storage of the pointer ' *globalCh ' should be in a
That means should it be in the code segment?
The standard does not recognize a c*** segment, but such segments are
often not writable, and the pointer globalCh must be writable.
[ #2 ]This is comp.lang.c, so the above line is a syntax error.
char* pch = new char;
I'll pretend you used malloc().
strncpy(pch, "ABCD", 4);The variable pch is *NOT* in the heap. It's in automatic storage.
pch = '\0';
// pch = "KLMN"; // ERROR !
We can't de-reference pch = "KLMN"; Is it because 'pch' allocated in
What it points at is, if you define "heap" as "that place from which
malloc() gets its storage".
while "KLMN" is in some variable location?"KLMN" is in static storage. A pointer in any kind of storage may
point to data in any (same or different) kind of storage.
I see no reason why the line marked ERROR! should cause an error
in C, unless that error is a memory leak. (And C permits leaking
memory with wild abandon. If you can malloc()ate it, you can leak
it. Leaking is not good programming practice, but it should not
cause errors other than those caused by running out of memory to
malloc()). You are discarding the allocated memory without freeing
it and without keeping a pointer to it so it could be freed. You
compiled this with a C++ compiler, didn't you?
[ #3 ]
char* str = "ABCD"; is same as const char* str =
"ABCD"; [ what it points to is constant ], but we can dereference as
str = "XYZ"; // OK !
str = 'L'; // ERROR !!!!
Can we think of char arrCh = " ABCD" as ' char * const arrCh
' ( i.e. we can't modify the address ) ?
No. A pointer and an array are different things. The address of
a floating-point variable and the address of main() can't be modified
either, but main() and a floating-point variable are not the same
We can't dereferenceThe above line is not a dereference.
arrCh = "XYZ"; // ERROR !
arrCh[ 2 ] = 'K' ; // Fine !
[ #4 ]
Which one of the ' char array ' notation is the most preferred one, is
it the pointer notation ( char *ptr = " ABCD" ) or the array notation
( char arrCh = "ABCD" ) ? Why is it one preferred over the other?
char *ptr = " ABCD"; allocates one pointer and 6 bytes for characters.
The characters may be (and you should assume they are) in read-only storage.
char arrCh = "ABCD"; allocates no pointer and 5 bytes for characters.
The characters *must NOT* be in read-only storage.
These are not the same thing.
What is better, a soft drink, a condom, or rat poison? The answer
depends on whether you are thirsty, horny, or infested with rats.
- Prev by Date: Re: pass by reference
- Next by Date: Re: setjmp return value as the right-hand side of a assignment expression?
- Previous by thread: Re: Difference between Char* ptr and char arrCh 
- Next by thread: Re: Difference between Char* ptr and char arrCh