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

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

问题描述

我无法找到从函数内部获取数组大小的方法.这是我的代码:

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 的作用与主函数中的第二行相同.但是,输出是不同的:

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() 函数中使用时的指针.毕竟,arr 在 main() 中调用时,仍然是一个指针,对吗?

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天全站免登陆