Re: is this correct C? (char*)spm += spm->partitionMapLength;
- From: O_TEXT <O_TEXT@xxxxxxxxx>
- Date: Fri, 30 Nov 2007 16:50:46 +0100
Spiros Bousbouras a écrit :
On Nov 30, 1:00 pm, O_TEXT <O_T...@xxxxxxxxx> wrote:is line 11 valid?
if yes, is it equivalent to line 10?
1 typedef unsigned char uint8_t;
2 struct sparablePartitionMap
3 {
4 uint8_t partitionMapLength;
5 } __attribute__ ((packed));
6 void initialise(char *devicename)
7 {
8 struct sparablePartitionMap *spm;
9 spm += spm->partitionMapLength;
10 spm = ( struct sparablePartitionMap *) ((char*)spm +
spm->partitionMapLength);
11 (char*)spm += spm->partitionMapLength;
12 }
No , line 11 is not valid C because the expression
(char *)spm is not an lvalue.
gcc 4.1 gives this error message (not a lvalue).
I do not the requirment for a value to be a lvalue.
This is just a curiosity question.
Nevertheless I think
some compilers allow the construct as an extension
although I don't know its meaning.
Might be older gcc.
I note also that on line 9 you dereference the pointer
spm although it hasn't been initialised. That's undefined
behaviour.
Yes.
The code comes from udftools-1.0.0b3 .
I just removed line which was comiling okay and rewrittent line 11 to 9 and 10, just in order to understand compilation issue.
Furthermore if you post code with non standard constructs
like __attribute__ you should explain what they mean.
These are understood by gcc.
I assume the packed just mean there is no gap betwen struct fields.
Personally I would like to know whether lines 9 and 10 do
the same thing assuming that spm has been properly
initialised.
gcc answers no:
../a.out
spm : 0x2b3e202a8ca0
spm : 0x2b3e202a8ca5
spm : 0x2b3e202a8ccd
int main()
{
long *spm;
printf ("spm : %p \n", spm );
spm = ( long *) ((char*)spm + 5);
printf ("spm : %p \n", spm );
spm += 5;
printf ("spm : %p \n", spm );
return 0;
}
.
- Follow-Ups:
- Re: is this correct C? (char*)spm += spm->partitionMapLength;
- From: Spiros Bousbouras
- Re: is this correct C? (char*)spm += spm->partitionMapLength;
- From: Johannes Bauer
- Re: is this correct C? (char*)spm += spm->partitionMapLength;
- References:
- Re: is this correct C? (char*)spm += spm->partitionMapLength;
- From: Spiros Bousbouras
- Re: is this correct C? (char*)spm += spm->partitionMapLength;
- Prev by Date: Re: is this correct C? (char*)spm += spm->partitionMapLength;
- Next by Date: Re: memcat fn
- Previous by thread: Re: is this correct C? (char*)spm += spm->partitionMapLength;
- Next by thread: Re: is this correct C? (char*)spm += spm->partitionMapLength;
- Index(es):
Relevant Pages
|