DSPRelated.com
Forums

Pointer vs Array, or char * vs char[]

Started by William C Bonner March 21, 2006
I ran into a strange thing when compiling code for my DSP recently. I
would like to know what is going on, if this is something I don't
understand in a difference of C vs C++, or something really strange in
the Code Composer Studio Compiler / Linker.

My understanding was that a a char array was simply dealt with as a
pointer to the chunk of memory that was declared. The following code
snippet that I was using, I would have expected to print the same line
twice. It did not. The second line was some other location from ram. I
have never run into a problem with something like this using MS Visual
C, so I'm wondering if I'm doing something that is really wrong, or if
this is something that is strange happening in the linker, and if
there's some way to tell the linker to fix what it is doing. I'm not
getting any linker errors, it is just not producing the proper output,
and I'm wondering if this may be related to some other problems I have
been having.

File1:

char ReallyBigString[] = "really big string\r\n";

main()
{
printf(ReallyBigString);
File2Subroutine();
}

File2:

extern char * ReallyBigString;
File2Subroutine()
{
printf(ReallyBigString);
}
William--
This code worked just fine in my CCS setup. Are you missing something here?

--Bhooshan
On 3/21/06, William C Bonner wrote:
>
> I ran into a strange thing when compiling code for my DSP recently. I
> would like to know what is going on, if this is something I don't
> understand in a difference of C vs C++, or something really strange in
> the Code Composer Studio Compiler / Linker.
>
> My understanding was that a a char array was simply dealt with as a
> pointer to the chunk of memory that was declared. The following code
> snippet that I was using, I would have expected to print the same line
> twice. It did not. The second line was some other location from ram. I
> have never run into a problem with something like this using MS Visual
> C, so I'm wondering if I'm doing something that is really wrong, or if
> this is something that is strange happening in the linker, and if
> there's some way to tell the linker to fix what it is doing. I'm not
> getting any linker errors, it is just not producing the proper output,
> and I'm wondering if this may be related to some other problems I have
> been having.
>
> File1:
>
> char ReallyBigString[] = "really big string\r\n";
>
> main()
> {
> printf(ReallyBigString);
> File2Subroutine();
> }
>
> File2:
>
> extern char * ReallyBigString;
> File2Subroutine()
> {
> printf(ReallyBigString);
> }
>
--
-------------------------------
"I've missed more than 9000 shots in my career.
I've lost almost 300 games. 26 times I've been trusted to take the game
winning shot and missed.
I've failed over and over again in my life.
And that is why I succeed."
-- Michael Jordan
--------------------------------
char ReallyBigString[] = "really big string\r\n";

extern char * ReallyBigString

These ARE NOT the same thing. One of them says that ReallyBigString is
the address of a pointer to an array of characters. The other says the
ReallyBigString is address of the beginning of an array of characters.
Arrays and pointers are NOT that same thing in C.

I'll Try to draw a picture.

(1) ReallyBigString[] = "really big string\r\n";

ReallyBigString-------> |r|e|a| .|\r|\n|\0|

(2) extern char * ReallyBigString -------> [an addresss]-------> |r|e|a|
.|\r|\n|\0|

When these are mixed disaster ALWAYS strikes. de-referencing
ReallyBigString in file #2 (*ReallyBigString) takes the first n bytes
(where n = the number of bytes in an address" of the string "really big
string \r\n" and tries to use them as the address of the array
*ReallyBigString points to.

I am in a hurry right now. If this is not clear let me know and I will
try to explain it better.

Note that inside a function call - eg void AccessReallyBigString1(char
*String) and void AccessReallyBigString2(char String[]) - they are
interchangeable. This is only a side effect of C's pass by value
semantics.

-----Original Message-----
From: c... [mailto:c...] On Behalf Of
Bhooshan Iyer
Sent: Tuesday, March 21, 2006 11:22 AM
To: William C Bonner
Cc: c...
Subject: Re: [c6x] Pointer vs Array, or char * vs char[]

William--

This code worked just fine in my CCS setup. Are you missing something
here?

--Bhooshan

On 3/21/06, William C Bonner wrote:

I ran into a strange thing when compiling code for my DSP recently. I
would like to know what is going on, if this is something I don't
understand in a difference of C vs C++, or something really strange in
the Code Composer Studio Compiler / Linker.

My understanding was that a a char array was simply dealt with as a
pointer to the chunk of memory that was declared. The following code
snippet that I was using, I would have expected to print the same line
twice. It did not. The second line was some other location from ram. I
have never run into a problem with something like this using MS Visual
C, so I'm wondering if I'm doing something that is really wrong, or if
this is something that is strange happening in the linker, and if
there's some way to tell the linker to fix what it is doing. I'm not
getting any linker errors, it is just not producing the proper output,
and I'm wondering if this may be related to some other problems I have
been having.

File1:

char ReallyBigString[] = "really big string\r\n";

main()
{
printf(ReallyBigString);
File2Subroutine();
}

File2:

extern char * ReallyBigString;
File2Subroutine()
{
printf(ReallyBigString);
}

--
-------------------------------
"I've missed more than 9000 shots in my career.
I've lost almost 300 games. 26 times I've been trusted to take the game
winning shot and missed.
I've failed over and over again in my life.
And that is why I succeed."
-- Michael Jordan
--------------------------------

*

_____