简体   繁体   English

c编程unsigned int或uint8_t

[英]c program unsigned int or uint8_t

I am writing a function called take_picture() . 我正在编写一个名为take_picture()的函数。 The function will make a camera take a picture and store the data in an array. 该功能将使相机拍照并将数据存储在一个数组中。

I want the function to return the address where the array is located and to return the size of the array. 我希望函数返回数组所在的地址并返回数组的大小。

So this is my function declaration: 所以这是我的函数声明:

uint8_t * take_picture(int *piclength)

My question is what would be the correct/logical data types for the function return type and the argument? 我的问题是函数返回类型和参数的正确/逻辑数据类型是什么? considering that they store an address and size of an array? 考虑到它们存储数组的地址和大小? Does a uint8_t and int make sense? uint8_tint有意义吗?

Thanks 谢谢

My question is what would be the correct/logical data types for the function return type and the argument? 我的问题是函数返回类型和参数的正确/逻辑数据类型是什么? considering that they store an address and size of an array? 考虑到它们存储数组的地址和大小?

The function return type should be a pointer to an array item. 函数返回类型应该是指向数组项的指针。 If it is an array of uint8_t , then the return type should be uint8_t* . 如果是uint8_t的数组,则返回类型应为uint8_t* This is also the most correct/safe type for an array of bytes ("raw data"). 这也是字节数组(“原始数据”)的最正确/安全类型。

The most correct type used to describe the size of an array is size_t , an unsigned integer type that exists specifically for this purpose, and is guaranteed portably to be large enough to hold an array size of the given system. 用于描述数组大小的最正确的类型是size_t ,这是一种专门为此目的而存在的无符号整数类型,并且可以保证足够大以容纳给定系统的数组大小。

uint8_t* take_picture (size_t* piclength);

or alternatively 或者

void take_picture (uint8_t* picture, size_t* piclength);

unsigned int size; unsigned int size; // As the image size is always positive and is large. //因为图像大小总是正的并且很大。

uintptr_t address; uintptr_t地址; // if this is available in stdint.h //如果这在stdint.h中可用

or 要么

unsigned int address; unsigned int address; //it is capable of storing a pointer size. //它能够存储指针大小。

address = take_picture(&size); address = take_picture(&size); //take_picture if returns uintptr_t // take_picture if if返回uintptr_t

If you do not know what take_picture returns, then you cast it like what you have done: 如果你不知道take_picture返回什么,那么你就像你所做的那样:

address = (unsigned *int)take_picture(& size); address =(unsigned * int)take_picture(&size);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM