鉴于以下计划,
#include <iostream>
using namespace std;
void foo( char a[100] )
{
cout << "foo() " << sizeof( a ) << endl;
}
int main()
{
char bar[100] = { 0 };
cout << "main() " << sizeof( bar ) << endl;
foo( bar );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出
main() 100
foo() 4
Run Code Online (Sandbox Code Playgroud)
我有
int list[] = {1, 2, 3};
Run Code Online (Sandbox Code Playgroud)
如何获得大小list?
我知道对于char数组,我们可以strlen(array)用来查找大小,或者'\0'在数组末尾检查.
我尝试sizeof(array) / sizeof(array[0])了一些答案,但它只适用于主?例如:
int size(int arr1[]){
return sizeof(arr1) / sizeof(arr1[0]);
}
int main() {
int list[] = {1, 2, 3};
int size1 = sizeof(list) / sizeof(list[0]); // ok
int size2 = size(list_1); // no
// size1 and size2 are not the same
}
Run Code Online (Sandbox Code Playgroud)
为什么?
可能重复:
如果传递给函数,则确定数组的大小
如何获得传递给函数的C++数组的大小?
在以下代码中,sizeof(p_vertData)不返回正确的数组大小.
float verts[] = {
-1.0,1.0,1.0,
1.0,1.0,1.0,
1.0,-1.0,1.0,
-1.0,-1.0,1.0,
-1.0,1.0,-1.0,
1.0,1.0,-1.0,
1.0,-1.0,-1.0,
-1.0,-1.0,-1.0
};
void makeVectorData(float p_vertData[]) {
int num = (sizeof(p_vertData)/sizeof(int));
cout << "output: " << num << endl;
};
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我有一个类型的变量Blah.
我想把它投射到char[sizeof(blah)],而不是复制.
我需要类型强制转换为足以实例化期望的模板char[N].
我尝试了很多东西,但我不太明白.
我想要这样的东西正常工作:
class Blah {
int a;
};
template <typename T>
void foo (T& a)
{
//Not an array
}
template <int N>
void foo (char(&a)[N])
{
//an array!
}
Blah b;
foo(b); //not an array
foo((char[sizeofBlah])b); //hopefully treated as an array
Run Code Online (Sandbox Code Playgroud) 大小char[]是number of char times sizeof(char),大小char*是sizeof(pointer)- 指向第一个元素的指针.
sizeof(char[])打印number of char times sizeof(char)中main(),在它的声明,但如果我通过这个数组的功能,其功能转换char[]到char*和它的imposibble获得使用数组的大小sizeof(),
"void pr(char chr[])" is changed to "void pr(char chr*)"
Run Code Online (Sandbox Code Playgroud)
代码示例:
using namespace std;
void pr(char chr[])
{
std::cout << "in pr(): " << sizeof(chr)<<"\n";
}
int main()
{
char* c;
char z[] = { 1,2,3,4,5,6,7,8,9};
c = z;
std::cout << "sizeof char* c in main(): " << sizeof(c) << "\n"; …Run Code Online (Sandbox Code Playgroud) 作为更大任务的一部分,我试图在 C 中打印数组的长度,即数组中有多少个元素。该数组是一个双精度数组,我想打印一个整数,它是数组中有多少双精度。我有这个代码:
int array_length( double array[] ) {
double length = 0;
length = sizeof(array) / sizeof(double);
printf("%G", length);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我像这样在 main 中调用它:
int main( void ) {
double test[] = {1.1,2.2,3.3,4.4,5.5};
array_length(test);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是这会返回错误:
错误:数组函数参数“array”上的“sizeof”将返回“double *”的大小[-Werror=sizeof-array-argument]
研究告诉我
sizeof(array) / sizeof(double);
Run Code Online (Sandbox Code Playgroud)
会给我一个数组的长度,我正在尝试使用“%G”来允许我打印此代码返回的双精度值。
我相信“double*”意味着long double,所以我尝试将“%G”更改为“%LG”并将“double”的所有用法更改为“long double”,但这并没有解决问题。这里发生了什么?
所以我的 C++ 老师在课堂上告诉我们,C++ 中没有确定数组大小的函数,我对此并不满意。我在 stackoverflow 上发现了一个问题,给出了这段代码(sizeof(array)/sizeof(*array)),虽然我不太明白它,但我知道它需要分配给数组的内存总量,并将其除以我假设的其默认内存分配数据类型...(???) 我决定练习编写函数(我在 CS 111 - 基础知识 1)并编写一个函数,该函数返回我传递给它的任何数组中的元素数量。这是我写的:
#include <iostream>
using namespace std;
int length_of_array(int some_list[])
{
// This only returns the integer 1 for some reason
return (sizeof(some_list)/sizeof(*some_list));
}
int main()
{
// Declare and initialize an array with 15 elements
int num_list[] = {2,4,6,8,10,12,14,16,18,20,22,24,26,28,30};
//Outputs what I assume is the total size in bytes of the array
cout << sizeof(num_list) << endl;
//Outputs what I assume to be size of memory set …Run Code Online (Sandbox Code Playgroud) 如何从函数中获取数组的位大小
int NumberOfElements(int Array[]);
int main()
{
int Array[] = { 5,5,6,5,5 };
std::cout << NumberOfElements(Array);
}
int NumberOfElements(int Array[]) {
return sizeof(Array);
}
Run Code Online (Sandbox Code Playgroud)
它返回 4。结果应该是 20。
我对 C++ 并不陌生,但今天我发现 main 函数和其他函数中数组的大小不同。这是为什么?我想这与指针有关。
#include<bits/stdc++.h>
using namespace std;
void func(int arr[]){
cout<<"func size: "<<sizeof(arr)<<"\n";
}
int main(){
int arr[5];
cout<<sizeof(arr)<<"\n";
func(arr);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
您可以测试此代码以查看差异。