为什么作为参数发送的数组的大小与main中的相同?
#include <stdio.h>
void PrintSize(int p_someArray[10]);
int main () {
int myArray[10];
printf("%d\n", sizeof(myArray)); /* As expected, 40 */
PrintSize(myArray);/* Prints 4, not 40 */
}
void PrintSize(int p_someArray[10]){
printf("%d\n", sizeof(p_someArray));
}
Run Code Online (Sandbox Code Playgroud) 如果将数组传递给另一个函数(未传递大小),是否可以确定数组的大小?该数组初始化为int array [] = {XXX} ..
我知道不可能做sizeof,因为它会返回指针的大小.我问的原因是因为我需要在传递数组的其他函数内运行for循环.我尝试过类似的东西:
for( int i = 0; array[i] != NULL; i++) {
........
}
Run Code Online (Sandbox Code Playgroud)
但我注意到在数组的近端,array [i]有时包含像758433这样的垃圾值,这不是数组初始化中指定的值.
在C中,我有一个结构数组定义如下:
struct D
{
char *a;
char *b;
char *c;
};
static struct D a[] = {
{
"1a",
"1b",
"1c"
},
{
"2a",
"2b",
"2c"
}
};
Run Code Online (Sandbox Code Playgroud)
我想确定数组中元素的数量,但sizeof(a)返回不正确的结果:48,而不是2.我做错了什么,或者sizeof这里根本不可靠?如果重要的话我正在使用GCC 4.4进行编译.
在以下代码中
#include<iostream>
template<typename T,size_t N>
void cal_size(T (&a)[N])
{
std::cout<<"size of array is: "<<N<<std::endl;
}
int main()
{
int a[]={1,2,3,4,5,6};
int b[]={1};
cal_size(a);
cal_size(b);
}
Run Code Online (Sandbox Code Playgroud)
正如预期的那样,两个阵列的大小都被打印出来.但是N如何自动初始化为数组大小的正确值(数组是通过引用传递的)?上面的代码是如何工作的?
在下面的程序中,数组的长度ar在main中是正确的,但是temp它显示了ar我的计算机上指针的长度为2(以单位为单位sizeof(int)).
#include <stdio.h>
void temp(int ar[]) // this could also be declared as `int *ar`
{
printf("%d\n", (int) sizeof(ar)/sizeof(int));
}
int main(void)
{
int ar[]={1,2,3};
printf("%d\n", (int) sizeof(ar)/sizeof(int));
temp(ar);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想知道如何定义函数,以便在函数中正确读取数组的长度.
这只是过去几天困扰我的事情,我认为不可能解决,但我之前看过模板魔术.
开始:
为了获得标准C++数组中的元素数量,我可以使用宏(1)或类型安全的内联函数(2):
(1)
#define sizeof_array(ARRAY) (sizeof(ARRAY)/sizeof(ARRAY[0]))
Run Code Online (Sandbox Code Playgroud)
(2)
template <typename T>
size_t sizeof_array(const T& ARRAY){
return (sizeof(ARRAY)/sizeof(ARRAY[0]));
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,第一个问题是成为一个宏(目前我认为是一个问题),而另一个问题是在编译时无法获得数组的大小; 即我不能写:
enum ENUM{N=sizeof_array(ARRAY)};
Run Code Online (Sandbox Code Playgroud)
要么
BOOST_STATIC_ASSERT(sizeof_array(ARRAY)==10);// Assuming the size 10..
Run Code Online (Sandbox Code Playgroud)
有谁知道这是否可以解决?
更新:
这个问题是在引入constexpr之前创建的.现在你可以简单地使用:
template <typename T>
constexpr auto sizeof_array(const T& iarray) {
return (sizeof(iarray) / sizeof(iarray[0]));
}
Run Code Online (Sandbox Code Playgroud) 我想制作一个计算传递数组大小的FUNCTION.
我将传递一个数组作为输入,它应该返回它的长度.我想要一个功能
int ArraySize(int * Array /* Or int Array[] */)
{
/* Calculate Length of Array and Return it */
}
void main()
{
int MyArray[8]={1,2,3,0,5};
int length;
length=ArraySize(MyArray);
printf("Size of Array: %d",length);
}
Run Code Online (Sandbox Code Playgroud)
长度应该是5,因为它包含5个元素,虽然它的大小是8(即使8会做,但5会很好)
我试过这个:
int ArraySize(int * Array)
{
return (sizeof(Array)/sizeof(int));
}
Run Code Online (Sandbox Code Playgroud)
这将无效,因为" sizeof(Array)"将重新调整Int指针的大小.这个" sizeof"只有你在同一个功能时才有效.
实际上我从C#开始很多天回到C所以我不记得了(和失踪Array.Length())
问候!
我正在尝试编写一个打印出数组中元素的函数.但是,当我使用传递的数组时,我不知道如何迭代数组.
void
print_array(int* b)
{
int sizeof_b = sizeof(b) / sizeof(b[0]);
int i;
for (i = 0; i < sizeof_b; i++)
{
printf("%d", b[i]);
}
}
Run Code Online (Sandbox Code Playgroud)
迭代传递的数组的最佳方法是什么?
当我需要将一个数组传递给一个函数时,似乎所有以下函数声明都可以工作
void f(int arr[])
void f(int arr[4]) // is this one correct?
Run Code Online (Sandbox Code Playgroud)
为了这:
int a[]={1,2,3,4};
f(a);
Run Code Online (Sandbox Code Playgroud)
但是当我将一个数组分配给另一个数组时,它就失败了
int a[]={1,2,3,4};
int b[4] = a; // error: array must be initialized with a brace-enclosed initializer
Run Code Online (Sandbox Code Playgroud)
那么为什么作为函数的参数传递的数组是可以的,但是在简单赋值的rhs上使用是错误的?
在C++中给出一个像这样的数组:
unsigned char bogus1[] = {
0x2e, 0x2e, 0x2e, 0x2e
};
Run Code Online (Sandbox Code Playgroud)
有没有办法反省bogus1找出是四个字符长?
#include <iostream>
using namespace std;
int main(){
int findMax(int *);
const int MAX = 100;
int values[MAX];
char ivals[256];
// Get the space-separated values from user input.
cin.getline(ivals, 256, '0');
char *helper;
// Clean input array and transfer it to values.
for(int i = 0; i < (MAX) && ivals[i] != 0; i++){
helper = ivals[i * 2];
values[i] = atoi(helper);
}
int mval = findMax(values);
cout << values << endl << mval;
return 0;
}
//Function to find the …Run Code Online (Sandbox Code Playgroud) 在测试这样的数组时:
float test [3] = {1, 2, 3};
cout << "Test size: " << sizeof(test) << endl;
Run Code Online (Sandbox Code Playgroud)
print语句显示的大小是12.类似地,当我制作大小时12,数组中的元素数量是47.这不是在C++中指定数组大小的正确方法吗?