Re: Difference between Char* ptr and char arrCh []



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
function ?

consider the code:

char* globalCh = "MY STRING";

void foo()
{
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
static area.

Correct.

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 ]

void foo()
{
char* pch = new char[25];
This is comp.lang.c, so the above line is a syntax error.
I'll pretend you used malloc().

strncpy(pch, "ABCD", 4);
pch[4] = '\0';

// pch = "KLMN"; // ERROR !
}

We can't de-reference pch = "KLMN"; Is it because 'pch' allocated in
the heap,
The variable pch is *NOT* in the heap. It's in automatic storage.
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 ]

Assuming
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[1] = '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
thing either.

We can't dereference

arrCh = "XYZ"; // ERROR !
The above line is not a dereference.
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.

.



Relevant Pages

  • Re: Length of an array of bytes
    ... a non-zero size and shall occupy one or more bytes of storage. ... object can be copied into an array of char or unsigned char. ... can be converted to an rvalue of type "pointer to cv void." ...
    (microsoft.public.vc.language)
  • Re: Difference between Char* ptr and char arrCh []
    ... I have a few queries regarding array of characters using array ... notation and pointer notation. ... Is there a difference in storage of global char* and char* inside ...
    (comp.lang.c)
  • Re: reviving games/freebsd-games
    ... (it's a pointer into some stringtable: easy way to look at it ... with enough storage to write to:) maybe try something like char ... struct scrollname_s { ...
    (freebsd-questions)
  • Re: My strcpy code
    ... >1) The caller supplies the storage for this function. ... >array pointed to by a const char*. ... s2 is a pointer ...
    (comp.lang.c)
  • Re: Problem with va_ macros and arrays of arrays
    ... > the arrays passed to a ... > specific char, somewhat similar to what the standard function ... that with an array of struct, or possibly a pointer to a dynamic array ... > As I'm still a beginner in C without a copy of the standard I ...
    (comp.lang.c)