C:发现元素的数量在阵列[] [英] C: finding the number of elements in an array[]

查看:137
本文介绍了C:发现元素的数量在阵列[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C:
如何找到的元素数在结构的数组,把它发送给一个函数之后

In C: How do you find the number of elements in an array of structs, after sending it to a function?

int main(void) {
  myStruct array[] = { struct1, struct2, struct3, struct4, struct5, struct6 };
  printf("%d\n", sizeof(array));
  printf("%d\n", sizeof(array[0]));
  f(array);
}
void f(myStruct* array) {
  printf("%d\n", sizeof(array));
  printf("%d\n", sizeof(array[0]));
}

由于某种原因主要在与printf显示出比F中的printf不同的结果。
我需要的是知道有多少元素数组中

For some reason the printf in main shows different results than the printf in f. My need is to know how many elements are in the array.

推荐答案

您不能。

您必须将大小传递给函数,例如:

You have to pass the size to the function, eg:

void f(myStruct* array, size_t siz);

另请注意,在˚F数组是一个指针,而在它是一个数组。数组和指针是不同的东西。

Also notice that in f array is a pointer, while in main it is an array. Arrays and pointers are different things.

这篇关于C:发现元素的数量在阵列[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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