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



AY said:

Hi there,

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 ?

Taking your question literally, a pointer-to-char identifier having
file scope lasts for the lifetime of the program, whereas a
pointer-to-char identifier having function (or block) scope lasts
only from its point of declaration to the end of the function (or
block), unless static-qualified. But I think that to take your
question literally is to misunderstand it.



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, while the value
"
My String" is stored elsewhere in a data segment.

That's one possible way of doing it, yes, but by no means the only
way.

Taking the
sizeof (simpleCh) would be 4 bytes in my machine. Once the
function exits "My String" is no longer accessible.

Well, er, yes because of the way you wrote it. But consider:

#include <stdio.h>

const char *get_literal(void)
{
const char *local_ptr = "Hello world";
return local_ptr;
}

int main(void)
{
const char *p = get_literal();
printf("%s\n", p);
return 0;
}

This is perfectly legal code. Ahem (fixes typo). As I was saying,
this is perfectly legal code, unless there's a typo I haven't seen
in time. The string literal "Hello world" exists for the lifetime
of the program. Once get_literal() returns, local_ptr is destroyed,
so we can't get at "Hello world" /through that identifier/ any
more. But we can certainly get at the string literal by saving the
value of that pointer away (in this case, by using it as the return
value of the function).


Taking the sizeof(globalCh) = 4 bytes

On your system? Fine.

and where is the value ' MY STRING '

Very likely fairly close to where the other string literal is
stored.

and the pointer ' *globalCh ' stored ? since its a global
variable the storage of the pointer ' *globalCh ' should be in a
static area. That means should it be in the code segment?

That's one way of doing it (although it seems quite an odd way). The
C Standard has nothing to say on the subject. It only requires that
the storage is available throughout the lifetime of the program.


[ #2 ]

void foo()
{
char* pch = new char[25];

Syntax error. Did you mean:

char *pch = malloc(25 * sizeof *pch);
if(pch != NULL)
{

by any chance?

strncpy(pch, "ABCD", 4);

Oooooh, bad idea...

pch[4] = '\0';

....but this will make amends.


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

It's a mistake, but it's not an error in the sense that any kind of
diagnostic message is required. It's just a memory leak.

}

We can't de-reference pch = "KLMN";

It isn't a de-reference. It's an assignment.

Is it because 'pch' allocated
in the heap, while "KLMN" is in some variable location?

Mu. It isn't an error in the sense you are suggesting. It's poor
programming practice, however, because you're throwing away the
result of the malloc call.


[ #3 ]

Assuming
char* str = "ABCD"; is same as const char* str =
"ABCD"; [ what it points to is constant ],

For historical reasons, alas, char * works just fine. Would that it
didn't. But yes, what it points to is supposed to be considered
constant.


but we can dereference
as

str = "XYZ"; // OK !

That's assignment, not dereference.

str[1] = 'L'; // ERROR !!!!

That won't give you a diagnostic message (or at least is not
guaranteed to) - it's not that kind of error. But yes, it is
certainly a mistake and the behaviour is undefined.

Can we think of char arrCh[] = " ABCD" as ' char * const arrCh
' ( i.e. we can't modify the address ) ?

You can't modify the address anyway. But when you define arrCh in
this way, you are not creating a pointer - you are creating an
array sufficiently large to hold the string on the right side of
the =, which in this case would be 5 bytes.


We can't dereference

arrCh = "XYZ"; // ERROR !

That's assignment, not dereference (and yes, it's an error, and in
this case a diagnostic message is required).

arrCh[ 2 ] = 'K' ; // Fine !

Right. It's an array. One expects to be able to update arrays
(unless one const-qualifies them).


[ #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?

Is it better to drive on the left or the right? Answers will depend
on which country you're in, whether you are overtaking, whether
there are contraflow roadworks in operation - i.e. it depends on
the situation. Same with pointers and arrays. Use the right tool
for the job.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Forged article? See
http://www.cpax.org.uk/prg/usenet/comp.lang.c/msgauth.php
"Usenet is a strange place" - dmr 29 July 1999
.



Relevant Pages

  • 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)
  • Re: char **argv & char *argv[]
    ... "pointer to pointer to char". ... >> pointer)) pointing to the first element of an array. ... so we have to start adding more context. ... type "pointer to char", rather than "array MISSING_SIZE of char". ...
    (comp.lang.c)
  • Re: storage for temporaries
    ... In an expression the array rvalue ... > is converted to a pointer to its first member. ... > storage for the structure value. ... access it after the next sequence point, ...
    (comp.lang.c)
  • Re: Returning pointer to array problem II
    ... Iam trying to make program were I enter string and serach char. ... and funktion prints out witch position char is found this is done if funktion serach_char. ... so far all good what I want do next is: return, from funktion, pointer value to array were positions is stored. ...
    (comp.lang.c)
  • Re: Simple question on Pointers
    ... int main ... It stores 12 char and only 12 ... pointer to the first element of the array with type pointer to element ...
    (microsoft.public.vc.language)

Loading