Difference between Char* ptr and char arrCh []

A

AY

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 ?

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. Taking the sizeof
(simpleCh) would be 4 bytes in my machine. Once the function exits "My
String" is no longer accessible.

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. That means should it be in the code segment?

[ #2 ]

void foo()
{
char* pch = new char[25];
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, while "KLMN" is in some variable location?

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

We can't 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?

Appreciate your suggestions,

Regards,
- AY.
 
N

Nick Keighley

X-posted to comp.lang.c++

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, while the value "
My String" is stored elsewhere in a data segment.  Taking the sizeof
(simpleCh) would be 4 bytes in my machine. Once the function exits "My
String" is no longer accessible.

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. That means should it be in the code segment?

[ #2 ]

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

this isn't valid C (C has no new operator). If you are coding
in C++ why not use std::vector or even std::string. This avoids
the problems you are having with assigning strings etc.

I usually find using the array form of new is a mistake
(at least it's an easy way to grep a source base for potential
mistakes!)
         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, while "KLMN" is in some variable location?

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

We can't 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?

Appreciate your suggestions,

pick a language
 
D

DDD

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 ?

 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.  Taking the sizeof
(simpleCh) would be 4 bytes in my machine. Once the function exits "My
String" is no longer accessible.
Yes, sizeof simpleCh is 4. 4 is the sizeof pointer variable itself. If
you wanna to know the sizeof string you should use strlen function.
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. That means should it be in the code segment?

[ #2 ]

     void foo()
     {
         char* pch = new char[25];
         strncpy(pch, "ABCD", 4);
         pch[4] = '\0';

         // pch = "KLMN"; // ERROR !
     }
Yes, that's right. pch here is pch[0], so you cann't do assignment
like this. But pch = 'K'.
We can't de-reference pch = "KLMN"; Is it because 'pch' allocated in
the heap, while "KLMN" is in some variable location?

[ #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 !!!!
Here, str is just a pointer but not allocate memory. str="XYZ" just
refer the memory address of "XYZ" to str.
And because you do not allocate memory fo str, str[1]='L' is wrong, str
[1] is readonly now.
Can we think of char arrCh[] = " ABCD" as ' char * const arrCh
' ( i.e. we can't modify the address ) ?

We can't 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?

Appreciate your suggestions,

Regards,
- AY.

Good luck.
 
J

James Kuyper

DDD said:
void foo()
{
char* pch = new char[25];
strncpy(pch, "ABCD", 4);
pch[4] = '\0';

// pch = "KLMN"; // ERROR !
}
Yes, that's right. pch here is pch[0], ...

No, pch is not pch[0]. One is a pointer, the other is a char. The
pointer points at the char.
... so you cann't do assignment
like this. ...

Sure you can. It's perfectly legal in C++, where 'new' is a valid
operator. In C (the subject of this newsgroup) it would be perfectly
legal if "new char[2]" were replaced with "malloc(25)", and checked
against the possibility that the allocation failed. Either way, it's a
memory leak, because the assignment discards the old value of pch,
thereby prevent you from deallocating the allocated memory. But it's
legal to leak memory - it's just extremely sloppy programming.
...But pch = 'K'.


The expression pch = 'K' is constraint violation (6.5.16.1p1): you're
trying to assign an integer value ('K') to an lvalue of pointer type. An
integer value can be converted to a pointer type, but the conversion
doesn't occur implicitly, and if it did occur in this case it would be
pointless, meaningless, and dangerous. What you can say is that pch[0]
== 'K'. You cannot meaningfully say that pch == (char*)'K', for almost
the same reasons that it's meaningless to write pch = (char*)'K'.
 
D

DDD

DDD said:
     void foo()
     {
         char* pch = new char[25];
         strncpy(pch, "ABCD", 4);
         pch[4] = '\0';
         // pch = "KLMN"; // ERROR !
     }
Yes, that's right. pch here is pch[0], ...

No, pch is not pch[0]. One is a pointer, the other is a char. The
pointer points at the char.
sorry, the pch here should be &pch[0].
... so you cann't do assignment
like this. ...

Sure you can. It's perfectly legal in C++, where 'new' is a valid
operator. In C (the subject of this newsgroup) it would be perfectly
legal if "new char[2]" were replaced with "malloc(25)", and checked
against the possibility that the allocation failed. Either way, it's a
memory leak, because the assignment discards the old value of pch,
thereby prevent you from deallocating the allocated memory. But it's
legal to leak memory - it's just extremely sloppy programming.
...But pch = 'K'.

The expression pch = 'K' is constraint violation (6.5.16.1p1): you're
trying to assign an integer value ('K') to an lvalue of pointer type. An
integer value can be converted to a pointer type, but the conversion
doesn't occur implicitly, and if it did occur in this case it would be
pointless, meaningless, and dangerous. What you can say is that pch[0]
== 'K'. You cannot meaningfully say that pch == (char*)'K', for almost
the same reasons that it's meaningless to write pch = (char*)'K'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top