使用其指针查找数组的大小

编程入门 行业动态 更新时间:2024-10-23 06:21:44
本文介绍了使用其指针查找数组的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

考虑以下代码: int main(无效) { char buffer [20]; func(缓冲区); } void func(char * bufpas) { ....... } 现在可以找到缓冲区的大小使用其指针bufpas?在主要中我可以使用sizeof(buffer)来实现。但是在func,sizeof(bufpas)中。将 给出4(指针变量的大小)作为输出(如果在红帽上使用gcc linux)。 我我正在考虑一些方法来做到这一点。例如,修改编译器 ,在每个数组的末尾添加一些区别元素然后 使用指针找到大小。 但是有一个问题。它适用于那些具有1字节存储但对于其他数据类型的数据类型 。类型为int的数组,如果 no。 of bytes是16,sizeof(< integer array>)应该返回4.因此我们 也需要知道数组的类型。 如果有的话你有其他好主意,请跟我分享。 谢谢。

Consider the following code: int main(void) { char buffer[20]; func(buffer); } void func(char *bufpas) { ....... } Now can i find size of "buffer" using its pointer "bufpas"? In "main" i can do it using "sizeof(buffer)" but in "func", "sizeof(bufpas)" would give 4 (size of pointer variable)as output (if using gcc on red hat linux). I am thinking of some method to do it. For example modify the compiler to put some distinguishing element at the end of every array and then find the size using the pointer. But there is a problem. It will work in the case of those data types that have 1-byte storage but for others for eg. array of type "int", if no. of bytes is 16, sizeof(<integer array>) should return 4. Thus we need to know the type of array too. If any of you have some other bright idea, please do share with me. Thanks.

推荐答案

Sontu写道: Sontu wrote: 考虑以下代码: int main(void) {char buffer >> [20] ; func(缓冲区); void func(char * bufpas) { ...... } 现在可以找到缓冲区的大小使用其指针bufpas?在主要中我可以使用sizeof(buffer)来做到这一点。但是在func,sizeof(bufpas)中。 会给出4(指针变量的大小)作为输出(如果在red hat上使用gcc linux)。 Consider the following code: int main(void) { char buffer[20]; func(buffer); } void func(char *bufpas) { ...... } Now can i find size of "buffer" using its pointer "bufpas"? In "main" i can do it using "sizeof(buffer)" but in "func", "sizeof(bufpas)" would give 4 (size of pointer variable)as output (if using gcc on red hat linux).

#define BUFF_SIZE 20 int main(无效) { char buffer [BUFF_SIZE]; func(缓冲区,BUFF_SIZE); } void func(char * bufpas,int size) { ...... }

#define BUFF_SIZE 20 int main(void) { char buffer[BUFF_SIZE]; func(buffer, BUFF_SIZE); } void func(char *bufpas, int size) { ...... }

" Sontu" < AB ****** @ gmail>写道: "Sontu" <ab******@gmail> writes: 考虑以下代码: int main(void) {char buffer [20]; func(buffer ); } void func(char * bufpas) { ...... } 现在我可以找到缓冲区的大小。使用其指针bufpas?在主要中我可以使用sizeof(buffer)来做到这一点。但是在func,sizeof(bufpas)中。将给4(指针变量的大小)作为输出(如果在红帽上使用gcc linux)。 你不能。如果你想让func知道数组的长度,你可以将这些信息传递给它。 我正在考虑一些方法来做到这一点。例如,修改编译器将一些区分元素放在每个数组的末尾,然后使用指针找到大小。 C字符串使用该方法(字符串由''\0'' 字符终止),但代价是无法使用在一个字符串中有一个''\0'' 字符 - ''\ 0''标记字符串的结尾, 不一定是数组对象的结尾。对于int数组,对于 示例,没有值可以用作区别元素 (除非你愿意放弃存储该值在数组中)。 但是有一个问题。它适用于那些具有1字节存储但对于其他数据类型具有1字节存储空间的数据类型。类型为int的数组,如果没有。 of bytes是16,sizeof(< integer array>)应该返回4.因此我们也需要知道数组的类型。 如果你们中有任何人有其他好主意,请与我分享。 Consider the following code: int main(void) { char buffer[20]; func(buffer); } void func(char *bufpas) { ...... } Now can i find size of "buffer" using its pointer "bufpas"? In "main" i can do it using "sizeof(buffer)" but in "func", "sizeof(bufpas)" would give 4 (size of pointer variable)as output (if using gcc on red hat linux). You can''t. If you want func to know the length of the array, you have to pass that information to it. I am thinking of some method to do it. For example modify the compiler to put some distinguishing element at the end of every array and then find the size using the pointer. C strings use that method (strings are terminated by a ''\0'' character), but at the expense of not being able to have a ''\0'' character within a string -- and the ''\0'' marks the end of the string, not necessarily the end of the array object. For an array of int, for example, there is no value you can use as a distinguishing element (unless you''re willing to give up on storing that value in the array). But there is a problem. It will work in the case of those data types that have 1-byte storage but for others for eg. array of type "int", if no. of bytes is 16, sizeof(<integer array>) should return 4. Thus we need to know the type of array too. If any of you have some other bright idea, please do share with me.

void func(size_t buflen,char * bufpas) { 。 .. } int main(无效) { char buffer [20] ; func(sizeof缓冲区,缓冲区); ... } - - Keith Thompson(The_Other_Keith) ks***@mib < http:// www .ghoti / ~kst> 圣地亚哥超级计算机中心< *> < users.sdsc.edu/~kst> 我们必须做点什么。这是事情。因此,我们必须这样做。

void func(size_t buflen, char *bufpas) { ... } int main(void) { char buffer[20]; func(sizeof buffer, buffer); ... } -- Keith Thompson (The_Other_Keith) ks***@mib <www.ghoti/~kst> San Diego Supercomputer Center <*> <users.sdsc.edu/~kst> We must do something. This is something. Therefore, we must do this.

谢谢。大家好,回复,但我想如果我告诉你我的 意图那么我的问题会变得更加清晰。 在C中,参数使用按值调用或按b引用或按引用调用传递函数。 [实际上只是按值调用,通过 参考调用)如果值是地址,则可视为前者。 现在如果需要实现按值 - 结果调用,则精确地在现有的情况下 CODE ,我该怎么办? 我的意思是, int main(无效) { char buffer [20]; func(缓冲区); } void func(char * bufpas) { char _buffer [20]; //临时阵列 memcpy(_buffer,bufpas,20); ....... .... memcpy(bufpas,_buffer,20); } 应该发生。为此,我可以使用source-2-source 转换或修改编译器。但无论如何我需要知道bufpas的大小。分配一个相同大小的临时数组。 如果不清楚,请提及,我会尽量使其全面。 Thanks. So nice of you all, for replying, but I think if i tell you my intent then my problem will become much clearer. In C, parameters to functions are either passed using "call by value" or "call by reference" [infact only "call by value", "call by reference" can be considered as the former if value is address]. Now if need to implement "call by value-result", precisely on EXISTING CODE, what should i do? I mean, int main(void) { char buffer[20]; func(buffer); } void func(char *bufpas) { char _buffer[20]; //temporary array memcpy(_buffer,bufpas,20); ....... .... memcpy(bufpas,_buffer,20); } should happen. For this to happen either i can use source-2-source transformation or modify the compiler. But in any case i need to know the size of "bufpas" to allocate a temporary array of same size. If its not clear, kindly mention, i will try to make it comprehensive.

更多推荐

使用其指针查找数组的大小

本文发布于:2023-07-29 00:48:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1235693.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   指针   大小

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!