(C question) (s - out.s) + func(&out) -> ?

Aruna Hewapathirane aruna.hewapathirane-Re5JQEeQqe8AvxtiuMwx3w at public.gmane.org
Thu Oct 31 05:47:04 UTC 2013


I am no C expert but looking at the code I think...

s = (s - out.s) + func (&out);

The yellow highlight is where your parentheses are used to group
sub-expressions to force a different precedence which is correct but...

s = (s - out.s) + func (&out);

The green highlight *IS* a function declaration and according to C operator
precedence will be evaluated by gcc first ?
This may help :
http://www.difranco.net/compsci/C_Operator_Precedence_Table.htm ( See note
1 as well please )

Step it through gdb and you will see what I mean :-)



On Wed, Oct 30, 2013 at 11:50 PM, William Park <opengeometry-FFYn/CNdgSA at public.gmane.org>wrote:

> Question for C expert...  Given the following
>
>     char *s;
>     struct { char *s; } out;
>     char *func();
>
>     s = (s - out.s) + func (&out);
>
> where 'out' is changed inside function 'func'.  What I want is
>     - to calculate (s - out.s) first, then
>     - add that difference to pointer returning from func().
> Here, order is important, because func() changes 'out'.
>
> However, gcc is doing
>     s = func (&out) + (s - out.s);
>
> Is pointer arithmetic always done as
>     (pointer) + (int)
> even if I write
>     (int) + (pointer)
> ?
>
> PS.  Of course, using intermediate variable solves the problem.  But, my
> understanding is that '+' is left-to-right precedence.
> --
> William
> --
> The Toronto Linux Users Group.      Meetings: http://gtalug.org/
> TLUG requests: Linux topics, No HTML, wrap text below 80 columns
> How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists
>



-- 
*Aruna Hewapathirane*
Consultant/Trainer
Phone : 647-709-9269
Website: <http://goog_1768911931>Open Source
Solutions<http://sahanaya.net/aruna/>



<https://sites.google.com/site/arunahewapathirane/home/business-card/buisness-card.png?attredirects=0>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://gtalug.org/pipermail/legacy/attachments/20131031/0ac7c5f7/attachment.html>


More information about the Legacy mailing list