数组大小问题 [英] array size issue
问题描述
这是一个输出4而不是200的虚拟程序。我怀疑它是
因为它计算32位机器上指针的大小而不是
of阵列。有没有办法让它打印200?我需要通过函数调用传递这个
全局变量。谢谢
#include< iostream>
#include< string>
using namespace std;
char数组[200];
void func1(char * str){
cout<<"" func1:"< ;< sizeof(str)<< endl;
}
int main(){
strcpy(array," one.two");
func1(array);}
Here is a dummy program that outputs 4 instead of 200. I suspect it''s
because it calculates the size of pointer on 32 bit machines instead
of array. Is there way to make it print 200? I need to pass the this
global variable via a function call. Thanks
#include<iostream>
#include<string>
using namespace std;
char array[200];
void func1(char *str){
cout<<"func1:"<<sizeof(str)<<endl;
}
int main(){
strcpy(array, "one.two");
func1(array);}
推荐答案
On 13 8月16:07,puzzlecracker< ironsel2 ... @ gmail.comwrote:
On 13 Aug., 16:07, puzzlecracker <ironsel2...@gmail.comwrote:
这是一个输出4而不是200的虚拟程序。我怀疑它's
因为它计算32位机器上指针的大小而不是
的数组。
Here is a dummy program that outputs 4 instead of 200. I suspect it''s
because it calculates the size of pointer on 32 bit machines instead
of array.
当然它确实 - 这就是你要求的。
Of course it does - that is what you asked for.
有没有办法让它打印200?我需要通过函数调用传递这个
全局变量。谢谢
Is there way to make it print 200? I need to pass the this
global variable via a function call. Thanks
是的。最简单的方法是
cout<<" func1:"<<<<<<<<< endl;
哪些可以优化一点bit ;-)
这样做的方法当然是使用std :: vector,但是
是其他方式 - 例如传递尺寸作为参数。
/ Peter
Yes. The easiest way is
cout<<"func1:"<<200<<endl;
Which can be optimised a little bit ;-)
The way to do it is of course to use std::vector instead, but there
are other ways - such as passing the size as a parameter.
/Peter
>
#include< iostream>
#include< string>
使用命名空间std;
char array [200];
void func1(char * str){
* cout<<" func1:"<< sizeof(str)<< endl;
}
int main(){
* strcpy(array," one.two") ;
* func1(数组);}
>
#include<iostream>
#include<string>
using namespace std;
char array[200];
void func1(char *str){
* cout<<"func1:"<<sizeof(str)<<endl;
}
int main(){
*strcpy(array, "one.two");
*func1(array);}
8月13日上午10:07 * am,puzzlecracker< ironsel2。 .. @ gmail.comwrote:
On Aug 13, 10:07*am, puzzlecracker <ironsel2...@gmail.comwrote:
这是一个输出4而不是200的虚拟程序。我怀疑它是
因为它计算32位机器上指针的大小而不是
的数组。有没有办法让它打印200?我需要通过函数调用传递这个
全局变量。谢谢
#include< iostream>
#include< string>
using namespace std;
char数组[200];
void func1(char * str){
* cout<<" func1:" << sizeof(str)<< endl;
}
int main(){
* strcpy(array," one.two");
* func1(array);}
Here is a dummy program that outputs 4 instead of 200. I suspect it''s
because it calculates the size of pointer on 32 bit machines instead
of array. Is there way to make it print 200? I need to pass the this
global variable via a function call. Thanks
#include<iostream>
#include<string>
using namespace std;
char array[200];
void func1(char *str){
* cout<<"func1:"<<sizeof(str)<<endl;
}
int main(){
*strcpy(array, "one.two");
*func1(array);}
除非必须,否则不要使用阵列;他们是邪恶的(http://www.ref /> = _blank> http://www.parashift.com/c++-faq-lit...html#faq-34.1)。使用
std :: string(你已经包含了标题)或std :: vector< char>
代替。这两个都带有它们的大小,不像
指针(这就是为什么std :: memcpy需要一个大小参数,为
实例)。或者,如果您的老师或老板要求您使用
数组,请使用std :: strlen或将sizeof(数组)传入func1。
干杯! --M
Don''t use arrays unless you have to; they''re evil (http://
http://www.parashift.com/c++-faq-lit...html#faq-34.1). Use
std::string (you''ve already included the header) or std::vector<char>
instead. Both of these carry their size around with them, unlike
pointers (that''s why std::memcpy needs a size parameter, for
instance). Alternately, if your teacher or boss requires that you use
arrays, use std::strlen or pass in sizeof(array) into func1.
Cheers! --M
8月13日,10:23 * am,mlimber< mlim ... @ gmail.comwrote:
On Aug 13, 10:23*am, mlimber <mlim...@gmail.comwrote:
8月13日上午10:07 * am,puzzlecracker< ironsel2 ... @ gmail.comwrote:
On Aug 13, 10:07*am, puzzlecracker <ironsel2...@gmail.comwrote:
这是一个虚拟程序输出4而不是200.我怀疑它是
,因为它计算32位机器上指针的大小而不是数组的
。有没有办法让它打印200?我需要通过函数调用传递这个
全局变量。谢谢
Here is a dummy program that outputs 4 instead of 200. I suspect it''s
because it calculates the size of pointer on 32 bit machines instead
of array. Is there way to make it print 200? I need to pass the this
global variable via a function call. Thanks
#include< iostream>
#include< string>
using namespace std ;
#include<iostream>
#include<string>
using namespace std;
char array [200];
char array[200];
void func1(char * str){
* cout<<" func1:"<< ;的sizeof(STR)<< ENDL;
void func1(char *str){
* cout<<"func1:"<<sizeof(str)<<endl;
}
}
int main(){
int main(){
* strcpy(array," one.two");
* func1(array);}
*strcpy(array, "one.two");
*func1(array);}
除非必须,否则不要使用数组;他们是邪恶的( http:/ /www.parashift.com/c++-faq-lit...html#faq-34.1)。使用
std :: string(你已经包含了标题)或std :: vector< char>
代替。这两个都带有它们的大小,不像
指针(这就是为什么std :: memcpy需要一个大小参数,为
实例)。或者,如果您的老师或老板要求您使用
数组,请使用std :: strlen或将sizeof(数组)传入func1。
干杯! --M
Don''t use arrays unless you have to; they''re evil (http://www.parashift.com/c++-faq-lit...html#faq-34.1). Use
std::string (you''ve already included the header) or std::vector<char>
instead. Both of these carry their size around with them, unlike
pointers (that''s why std::memcpy needs a size parameter, for
instance). Alternately, if your teacher or boss requires that you use
arrays, use std::strlen or pass in sizeof(array) into func1.
Cheers! --M
我必须使用char []进行优化。我有什么选择?
I have to use char [] for optimization. What are my options?
这篇关于数组大小问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!