Connecting Tech Pros Worldwide Forums | Help | Site Map

(part 34) Han from China answers your C questions

Borked Pseudo Mailed
Guest
 
Posts: n/a
#1: Nov 20 '08
What is this?

Eric said:
Quote:
A more detailed block of code is:
>
bool result;
bool GetByte( char *theAddr, int theCount, char *theDest);
>
char Dest[ SOME_SIZE ];
>
result =
GetByte( ( char * ) &( ( struct aStruct * ) 0 )->Addr,
1, ( char * ) ( &Dest ) );
>
My question is... does the struct aStruct live at address 0, or does
address 0 contain a pointer to wherever aStruct actually lives? Seems
to me like the "&" ahead of ( ( struct aStruct * ) 0 )->Addr would
indicate the latter...
...
Quote:
it's an embedded system, uses an ARM7-based controller.
That ( char * ) ( &Dest ) is like two cups of redundancy. I'd hate to
think that this embedded system is a pacemaker or something.

As for "does address 0 contain a pointer to wherever aStruct actually
lives?", no and yes. No, the code appears to be referring to a structure
that begins at address 0, or at least is temporarily treating the data
there as a structure. Yes, it's possible address 0 is in some sense a
pointer (though not in the C sense), since the ARM MMU supports virtual
memory, and the actual address that winds up on the address bus needn't
refer to a real address of 0.

I believe your confusion is a result of binding the "&" before the "->".
&( ( struct aStruct * ) 0 )->Addr is broken down as follows:

1. 0
2. (struct aStruct *) 0
--3. ( ( struct aStruct * ) 0 )->Addr
4. &( ( struct aStruct * ) 0 )->Addr

I believe you're making the mistake of breaking it down as follows:

1. 0
2. (struct aStruct *) 0
--3. &( ( struct aStruct * ) 0 )
4. &( ( struct aStruct * ) 0 )->Addr

Yours,
Han from China


Nick Keighley
Guest
 
Posts: n/a
#2: Nov 21 '08

re: (part 34) Han from China answers your C questions


On 20 Nov, 22:36, Borked Pseudo Mailed <nob...@pseudo.borked.net>
wrote:
Quote:
What is this?
Eric said:
Quote:
Quote:
A more detailed block of code is:
>
Quote:
bool result;
bool GetByte( char *theAddr, int theCount, char *theDest);
>
Quote:
char Dest[ SOME_SIZE ];
>
Quote:
result =
* GetByte( ( char * ) &( ( struct aStruct * ) 0 )->Addr,
* * * * * * * * * * * * * * * * * 1, *( char * ) ( &Dest ) );
>
Quote:
My question is... does the struct aStruct live at address 0, or does
address 0 contain a pointer to wherever aStruct actually lives? *Seems
to me like the "&" ahead of ( ( struct aStruct * ) 0 )->Addr would
indicate the latter...
..
Quote:
it's an embedded system, uses an ARM7-based controller.
>
That ( char * ) ( &Dest ) is like two cups of redundancy. I'd hate to
think that this embedded system is a pacemaker or something.
that would all be sorted out at compile time on any reasonable
compiler. Both address-of and casting between pointers are free at run-
time.

Quote:
As for "does address 0 contain a pointer to wherever aStruct actually
lives?", no and yes. No, the code appears to be referring to a structure
that begins at address 0, or at least is temporarily treating the data
there as a structure. Yes, [its] possible address 0 is in some sense a
pointer (though not in the C sense),
nonsense

Quote:
since the ARM MMU supports virtual
memory, and the actual address that winds up on the address bus needn't
refer to a real address of 0.
by "real" I assume you mean "physical". In what sense is the address/
pointer
referred to in the C program not-real? You seem to be adding
unnecessary
confusion (surprise!)
Quote:
I believe your confusion is a result of binding the "&" before the "->".
&( ( struct aStruct * ) 0 )->Addr is broken down as follows:
>
* * 1. 0 * *
* * 2. (struct aStruct *) 0 *
--3. ( ( struct aStruct * ) 0 )->Addr
* * 4. &( ( struct aStruct * ) 0 )->Addr
this is correct
Quote:
I believe you're making the mistake of breaking it down as follows:
>
* * 1. 0
* * 2. (struct aStruct *) 0
--3. &( ( struct aStruct * ) 0 ) *
* * 4. &( ( struct aStruct * ) 0 )->Addr

--
Nick Keighley
Closed Thread