C Pointer Arithmetic in Python

I am trying to convert a simple C program to Python, but since I know nothing about C and a little about Python, it is just hard for me.

I am stuck in C. pointers

There is a function that takes a longint int unsigned pointer and adds its values ​​to some variables inside the while loop:

uint32_t somename(const uint32_t *z) { while(....) { a += z[0] b += z[1] c += z[2] z += 3 } } 

Can someone please tell me how to do the same thing in python? (The part I didn't understand at all was "z + = 3")

I know there are no pointers in python. (at least not like C). But the problem is that I don’t know what exactly C pointers do, and therefore this cannot happen in python.

+4
source share
3 answers

A similar piece of code in Python could be:

 def somename(z): i = 0 while (....): a += z[i] b += z[i+1] c += z[i+2] i += 3 

In C, z works like an array index, except that it starts at any address at the beginning of the array, and does not start at 0. Python doesn't have a similar concept, so you need to use the list index explicitly.

Everything inside (....) also needs to be modified. I will leave this as an exercise for you, as it is not indicated in the question.

+10
source

Which means z += 3 , it basically moves the pointer 3 elements down. Say you have a pointer to an array in C called lst that contains [1, 2, 3, 4] . The lst pointer points to the first element such that *lst equivalent to lst[0] . In addition, *(lst+1) equivalent to lst[1] .

+1
source

Assuming z is passed as a list (in the corresponding python code). z += 3 can be translated to del z[:3] , which moves element 3 to 0. However, in python you need to copy the array before doing this, because with the del-statement the array is changed.

In C, you can access elements before the specified index using negative indices. This can be done using an "invisible" offset nested in the class. This offset is always added to the index when accessing the list. The following class demonstrates behavior.

 class pointer_like: def __init__(self, lst): self.lst = lst; self.offset = 0 # self[index] def __getitem__(self, index): return self.lst[self.offset + index] # self += offset def __iadd__(self, offset): self.offset += offset # other member functions... # as your example above def somename(z): z = pointer_like(z) while (....): a += z[0] b += z[1] c += z[2] z += 3 >>> # other example >>> z = pointer_like([0, 1, 2, 3, 4, 5]) >>> z[0] 0 >>> z += 3 >>> z[0] 3 >>> >>> # with normal python lists, this would mean third last element >>> z[-3] 0 >>> z += -5 >>> z[2] 0 >>> >>> # this is special, because z.offset is negative (-2), >>> # when a list item is accessed through a negative index, >>> # it is counted from the end of the array in python. >>> # In this case, it is -2, so the second last is accessed >>> # In C this would cause undefined behavor, on most >>> # platforms this causes an access violation >>> z[0] 4 

Note that pyhon also has a += operator for lists, but this allows you to add another list to the end.

0
source

Source: https://habr.com/ru/post/1347005/


All Articles