如何获得函数内的数组大小? [英] How to get array size within function?

查看:433
本文介绍了如何获得函数内的数组大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难找到一种从函数中获取数组大小的方法.这是我的代码:

I am having trouble finding a way to get the array size from within a function. Here is my code:

#include <stdio.h>

void printBuff(char *buf);
int main()
{
    char arr[12] = "csdlnclskjn";
    printf("Array size: %d, element size: %d. ",sizeof(arr), sizeof(arr[0]));
    printBuff(arr);
    return 0;
}

void printBuff(char *buf){
    printf("Array size: %d, element size: %d.",sizeof(buf), sizeof(buf[0]));
}

如上所示,printBuff与main函数中的第二行相同.但是,输出是不同的:

As seen above, printBuff does the same as the second line in the main function. However, the outputs are different:

数组大小:12,元素大小:1.数组大小:4,元素大小:1.

Array size: 12, element size: 1. Array size: 4, element size: 1.

考虑一下,我理解为什么在printBuff()方法中输出为4.实际上,arr是指向数组第一个元素的指针.在32位架构上,sizeof(arr)将返回4,在64位架构上,它将返回8.我不明白的是为什么sizeof(arr)返回数组的大小而不是数组的字节数在main()函数中使用时的指针.毕竟,当在main()内部调用arr时,它仍然是指针,对吧?

Thinking about it, I understand why the output is 4 in the printBuff() method. In fact, arr is a pointer to the first element of the array. On a 32-bit architecture, sizeof(arr) will then return 4, on 64-bit one it will return 8. What I do not understand is why sizeof(arr) returns the size of the array instead of the number of bytes of the pointer when used in the main() function. After all, arr, when invoked inside main(), is still a pointer, right?

所以我的问题是:

  1. sizeoff()如何根据使用它的上下文进行不同的解释?这取决于什么?

  1. How come sizeoff() is interpreted differently depending on the context in which it is used? What does this depend on?

如何从函数中获取实际的数组大小(数组中的元素数),而不将大小作为参数传递,不使用诸如在增加计数器直到'\ 0的同时对数组进行迭代的方法'达到-不管上下文如何,这都是获取数组大小的最简单方法.

How to get the actual array size (number of elements in array) from within a function, without passing the size as an argument, without using methods such as iterating over the array while incrementing a counter until '\0' is reached - just the simplest way to get array size regardless of the context.

顺便说一句,负责记住数组大小的编译器/系统在哪里存储数组的大小?它如何与数组关联以及如何检索?

Incidentally, where does the compiler/system responsible for remembering the size of the array store the size of the array? How is it associated with the array and how is it retrieved?

我想使用sizeof(buf),/sizeof(buf [0])作为数组的大小来迭代数组,但是显然这是不可能的.

I wanted to iterate though an array using sizeof(buf), / sizeof(buf[0]) as the size of the array but apparently that is not possible.

推荐答案

所以这是我为您解答的问题:

so here are my answers for your questions:

  1. 将数组传递给函数(带有char *参数)后,将其转换"为char*类型.
  2. AFAIK没有这样的方法.您可以对字符串使用strlen函数.否则,您必须将长度作为参数传递.
  3. 请参见数组指针如何存储其大小?
  1. The array is "converted" into char* type when passed into the function (with the char* parameter).
  2. AFAIK there is no such way. You could use strlen function for strings. Otherwise, you have to pass the length as parameter.
  3. See How does an array pointer store its size?

在传递数组参数时不要使用sizeof(buf)/sizeof(buf[0])来获取数组的长度.参见.有关更多信息.

Don't use sizeof(buf)/sizeof(buf[0]) to get length of an array when passing array parameters. See this. for more information.

这篇关于如何获得函数内的数组大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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