grid wrote On 12/13/05 13:30,:
>>> *((char **)v)++
>>>
>>> Dereferences v and post-increments what it points to.
>>>
>>>
>>
>> No; this one's a constraint violation. Syntactically,
>>it asks for the value of `v' to be converted to type `char**',
>>then for that value to be post-incremented, meanwhile
>>dereferencing the non-incremented value. The constraint
>>violation is the attempt to post-increment the expression
>>`((char**)v)', which is not an lvalue.
>>
>>
>>
>
> This is code which I dont have any control over.
Does that mean you cannot change it? The code is
incorrect C; if you can't change it, you can't fix it.
> It was initially used as
> simply "*v++".
> But later due to some cleanup and to supress lint warnings changed to
> the above.Does the increment operator work this way :
> *((char **)v) = *((char **)v) + 1
> So that the lvalue cast is a constraint violation.
It's just like `int x; (double)x = 3.14159;' or
like `(2 + 2)++' or like `printf("hello\n") = 42;'.
You cannot assign new values to such things.
> I do get the following errors in the proprietary compiler for which I
> have been to forced to change the code :
> Warning : Cast is not lvalue; ++ requires lvalue.
> Error : Modifiable lvalue required with operator "++".
The proprietary compiler is correct. But "forced to
change?" I thought you said you have no control over
this code ...
> 'v' is as I mentioned earlier a array of pointers to char (an array of
> strings) ,
> char *v[] ;
As it stands and in isolation, this declaration of
`v' is incorrect and requires a diagnostic. Please
provide the rest of the context.
> And the code moves over these strings one by one passing each string to
> a function ( which expects a char *).Since here we have an array of char
> pointers, I am getting the error on non-modifiable lvalue since an array
> address is a constant.But then that was the reason for the cast to char
> **,to make it increment the pointer over the char *'s.
>
>
>>> *((char **)v++)
>>>
>>> Post-increments v itself, and then dereferences it.
>>>
>>>
>>
>> Right. Note that the "it" refers to the converted
>>value of `v' as it was before being incremented.
>>
>>
>>
>
> I tried it like this to circumvert the errors,but seem to have got the
> logic wrong.Since this increments 'v' prior to the dereferencing,I would
> be missing the first string that gets passed to the fucntion because I
> will get the strings from the second string onwards.
You do not understand the post-increment operator (and
probably don't understand post-decrement, either.) Go back
to your C textbook. Snap quiz: assuming appropriate headers
and other context, what is the output of
int x = 1;
printf ("%d\n", x++);
printf ("%d\n", x);
> Ofcourse dereferencing and incrementing 'v' without the cast should work
> fine (*v++), but the first statement seems to work on a particular
> platform.If this is a constraint violation it should be caught on both
> the compilers.
Some popular compilers are not really compilers for C
but for a C-ish language with some extra decorations the
compiler writers thought would look festive. I strongly
suspect you're using gcc, in which case you might find it
instructive to tell it to shed its garish decor and dress
more soberly: "gcc -W -Wall -ansi -pedantic ..." will get
it to behave better.
--