[SciPy-User] Help of understanding C code in weave

Santosh Kushwaha santoshvkushwaha at gmail.com
Thu Jan 29 03:22:48 EST 2015


On 01/29/2015 04:04 AM, Zheng Ruan wrote:
> Hi Scipy users,
>
> I am trying to read code that uses scipy.weave and I don't understand
> some of the code in C. To make it easy and clear, I'll just post the
> part that confused me.
>
> I have a numpy array (a) with a shape of (2, 623, 3, 333). And another
> array numpy array (d) with a shape of (1, 623, 623).
>
> In the C code part I have something like this:
>
> code = """
> ...
> c = *(a + b);
> *(d+b) += 1;
> ...
> """
>
> In the above code, b and c are float type in C. I just don't
> understand how the c value are calculated and what does "*(d+b) += 1;" do.
>
> The code is very old and I saw some deprecated warnings when I compile it.
>
> Thank you so much and any hints are welcome!!!
>
> Zheng
>
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
In C language, the name of an array can be considered just as a pointer
to the beginning of a memory block as big as the array. Only difference
being that compiler keeps some extra info for the arrays to keep track
of their storage requirements.
you can check it by:
sizeof(pointer) and sizeof(array);
last one should print the sum of size of all element in the array
 So, back to your question, as 'a' and 'd' are the arrays it could be
written as simply a or d respectively.
i.e.  array = pointer,

 and when you put an asterisk  or indirection to your pointer variable
or array variable as in your case it simply means that you are
dereferencing the value of array at the beginning of the array. i.e.
value at array[0];
there are another forms to write it as well like,
*array = array[index], where index = 0, both are same.

in case you are retrieving the value of an array at a particular
index/offset the it would be like:
*(array+index) = array[index/offset],   both are same.

c = *(a+b);
in above code 'a' is the array, 'b' is the index of the array and *(a+b)
is the value of the array at position 'b', So basically you are
assigning the vale of *(a+b) to variable 'c'.

*(d+b) += 1;
value of *(d+b) is incremented by 1.

And the 'b' and 'c' which are float, would be implicitly cast to int by
numpy 
-- 
Regards,
Santosh Kushwaha
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20150129/bac281a9/attachment.html>


More information about the SciPy-User mailing list