the question of "sizeof"!
-
#include <iostream.h>
I want to print the "helloworld" but it print "hell" ,why??
void show( char str[])
{
for (int i=0; i < sizeof(str)/sizeof(str[0]); ++i)
cout << str[i];
}int main()
{char str[]="helloworld!";
show(str);
return 0;
}wbgxx wrote:
I want to print the "helloworld" but it print "hell" ,why??
Because the size of a pointer is 4. Try
strlen()
instead."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
-
#include <iostream.h>
I want to print the "helloworld" but it print "hell" ,why??
void show( char str[])
{
for (int i=0; i < sizeof(str)/sizeof(str[0]); ++i)
cout << str[i];
}int main()
{char str[]="helloworld!";
show(str);
return 0;
}wbgxx wrote:
void show( char str[]) { for (int i=0; i < sizeof(str)/sizeof(str[0]); ++i) cout << str[i]; }
This code is the same as this:
void show(char *str)
{
for (int i=0; i < sizeof(str)/sizeof(str[0]); ++i)
cout << str[i];
}The array decays to a pointer. The size of a pointer is 4 bytes and the size of a
char
is 1.Steve
-
#include <iostream.h>
I want to print the "helloworld" but it print "hell" ,why??
void show( char str[])
{
for (int i=0; i < sizeof(str)/sizeof(str[0]); ++i)
cout << str[i];
}int main()
{char str[]="helloworld!";
show(str);
return 0;
}'str' is a pointer. sizeof a pointer is 4 (on Win32). so you get four characters: h,e,l,l. if you want to get the length of a character string, use
strlen
. -
#include <iostream.h>
I want to print the "helloworld" but it print "hell" ,why??
void show( char str[])
{
for (int i=0; i < sizeof(str)/sizeof(str[0]); ++i)
cout << str[i];
}int main()
{char str[]="helloworld!";
show(str);
return 0;
}It's worth noting this way it would work:
void show( char str[], int size)
{
for (int i=0; i < size; ++i)
cout << str[i];
}int main()
{
char str[]="helloworld!";
show(str, sizeof(str)/sizeof(str[0]));
return 0;
}:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
It's worth noting this way it would work:
void show( char str[], int size)
{
for (int i=0; i < size; ++i)
cout << str[i];
}int main()
{
char str[]="helloworld!";
show(str, sizeof(str)/sizeof(str[0]));
return 0;
}:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
#include <iostream.h>
I want to print the "helloworld" but it print "hell" ,why??
void show( char str[])
{
for (int i=0; i < sizeof(str)/sizeof(str[0]); ++i)
cout << str[i];
}int main()
{char str[]="helloworld!";
show(str);
return 0;
}Here's another alternative (MSVC6 can't handle it):
void show(const char *pStr, int size)
{
for (int i=0; i<size; ++i)
{
cout << pStr[i];
}
}
template <int N>
inline
void show(const char (&a)[N])
{
show(a, N-1);
}
int _tmain(int argc, _TCHAR* argv[])
{
char str[] = "Hello world!";
show(str);
cout << endl;
return 0;
}Steve
-
Here's another alternative (MSVC6 can't handle it):
void show(const char *pStr, int size)
{
for (int i=0; i<size; ++i)
{
cout << pStr[i];
}
}
template <int N>
inline
void show(const char (&a)[N])
{
show(a, N-1);
}
int _tmain(int argc, _TCHAR* argv[])
{
char str[] = "Hello world!";
show(str);
cout << endl;
return 0;
}Steve
Interesting example, Steve. In your example, does the compiler set N to 12 or 13? I ask because the
for
loop increments i up to but not including the size, but your template function call passes the value of N-1 to the function. So characters 0-11, all 12 of them, would only be output if the value of N was set to 13. 13 being the 'correct' size when you include the NULL terminator as part of the size of the string. Just an interesting little difference from a lot of the std C string functions. On a side note, is it the array reference that MSVC6 doesn't like or something about the template function declaration?Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]
-
#include <iostream.h>
I want to print the "helloworld" but it print "hell" ,why??
void show( char str[])
{
for (int i=0; i < sizeof(str)/sizeof(str[0]); ++i)
cout << str[i];
}int main()
{char str[]="helloworld!";
show(str);
return 0;
}You don't need the loop at all. cout is savvy enough to know where your string ends. Unless there's some other reason for the loop then your show() function is simply:
void show( char str[] )
{
cout << str;
}Although in C/C++ we tend to use pointer notation:
void show (char* str)
{
cout << str;
}And now, given that your function is doing so little you might question its usefulness and move the code into main:
int main()
{
char str[] = "helloworld!";
cout << str;
return 0;
}Or, finally, you might prefer to use a string constant:
int main()
{
cout << "helloworld!";
return 0;
}None of which is particularly useful if all you wanted to know is why sizeof() returned 4.
-
Interesting example, Steve. In your example, does the compiler set N to 12 or 13? I ask because the
for
loop increments i up to but not including the size, but your template function call passes the value of N-1 to the function. So characters 0-11, all 12 of them, would only be output if the value of N was set to 13. 13 being the 'correct' size when you include the NULL terminator as part of the size of the string. Just an interesting little difference from a lot of the std C string functions. On a side note, is it the array reference that MSVC6 doesn't like or something about the template function declaration?Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]
Chris Meech wrote:
Interesting example, Steve. In your example, does the compiler set N to 12 or 13? I ask because the for loop increments i up to but not including the size, but your template function call passes the value of N-1 to the function. So characters 0-11, all 12 of them, would only be output if the value of N was set to 13. 13 being the 'correct' size when you include the NULL terminator as part of the size of the string. Just an interesting little difference from a lot of the std C string functions.
Mine (VS 2010) puts (as it should)
13
. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
You don't need the loop at all. cout is savvy enough to know where your string ends. Unless there's some other reason for the loop then your show() function is simply:
void show( char str[] )
{
cout << str;
}Although in C/C++ we tend to use pointer notation:
void show (char* str)
{
cout << str;
}And now, given that your function is doing so little you might question its usefulness and move the code into main:
int main()
{
char str[] = "helloworld!";
cout << str;
return 0;
}Or, finally, you might prefer to use a string constant:
int main()
{
cout << "helloworld!";
return 0;
}None of which is particularly useful if all you wanted to know is why sizeof() returned 4.
I assumed printing a string was used as an example of the general problem of passing an array and determining its size automatically.
Steve