Sign in

username:

password:



Not a member?

Search c6x



Search tips

Subscribe to c6x



c6x by Keywords

AD535 | BIOS | Booting | Bootloader | C621 | C6211 | C6415 | C671 | C6711 | C6711DSK | C6713 | CCS | Chassaing | COFF | DAT | DM64 | DM642 | DMA | DSK671 | DSK6711 | EDM | EDMA | EMIF | Emulator | EVM | EVM620 | FFT | FIR | GPIO | Halting | HPI | HWI | IDK | JTAG | LDB | LDH | LDW | Linker | LMS | LOG_printf | Matlab | McBSP | MEM_alloc | MIPS | PCI | PCM3003 | Pipeline | Profiling | QDM | Reset | ROM | RTDX | Sampling | SDRAM | Stack | TEB | THS1206 | TMS320C621 | TMS320C6416 | TMS320C6711 | TMS320C6713 | UART | Vector Table | XBUS | XDS560


Discussion Groups

Discussion Groups | TMS320C6x | Pointer vs Array, or char * vs char[]

Technical discussions about the TI C6000 DSPs (including the c62x, c64x and c67x DSPs).

  

Post a new Thread

Pointer vs Array, or char * vs char[] - William C Bonner - Mar 21 8:25:15 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);
}



(You need to be a member of c6x -- send a blank email to c6x-subscribe@yahoogroups.com )

Re: Pointer vs Array, or char * vs char[] - Bhooshan Iyer - Mar 21 14:21:54 2006

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

--Bhooshan
On 3/21/06, William C Bonner <w...@wimsworld.com> 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
--------------------------------------------------------------------



(You need to be a member of c6x -- send a blank email to c6x-subscribe@yahoogroups.com )

RE: Pointer vs Array, or char * vs char[] - Wayne J - Mar 22 8:03:13 2006

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...@yahoogroups.com [mailto:c...@yahoogroups.com] On Behalf Of
Bhooshan Iyer
Sent: Tuesday, March 21, 2006 11:22 AM
To: William C Bonner
Cc: c...@yahoogroups.com
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 <w...@wimsworld.com> 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
--------------------------------------------------------------------

*

  _____  


(You need to be a member of c6x -- send a blank email to c6x-subscribe@yahoogroups.com )