的C指针运算在Python [英] C Pointer arithmetic in Python

查看:172
本文介绍了的C指针运算在Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个简单的C程序转换到Python但我不知道关于C什么,一点点关于Python它只是对我来说很难。

I'm trying to convert a simple C program into Python but as I don't know anything about C and a little about Python its just difficult for me..

我被困在C指针。

有是需要一个unsigned long int类型的指针和一个while循环中增加了它的价值,以一些变量的函数:

There is a function that takes an unsigned long int pointer and adds its values to some variables within a while-loop:

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

有人能告诉我如何完成在python是一回事吗?
(我根本不明白的部分是Z + = 3)

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

我知道,有没有在Python的指针。 (至少不会像C)但问题是,我不知道C指针究竟做并为此不能做到这一点的蟒蛇。

I'm aware that there aren't pointers in python. (at least not like C) But the problem is that I don't know what C pointers exactly do and therefor can't make this happen in python.

推荐答案

在Python中的类似code片断可能是:

A similar code snippet in Python might be:

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

在C,以Z 作品有点像数组索引,但它开始于任何数组的起始地址为,而不是从0开始。有在Python中没有类似的概念,所以你需要明确地使用一个列表索引。

In C, z works sort of like an array index, except it starts at whatever the address of the start of the array is, rather than starting at 0. There is no analogous concept in Python, so you need to use a list index explicitly.

无论是在(....)将需要修改了。我会离开,作为一个练习你,因为它是在问题未指定的。

Whatever is inside (....) will need modification too. I'll leave that as an exercise for you, since it's unspecified in the question.

这篇关于的C指针运算在Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆