MarcSmith wrote:
i was in a cpp program calling a c program where in the cpp program i
had defined a 80 character string passing to the c program where sizeof
had reported it was 4 when it passed the answer back it was wrong.
The answer may have been correct. Your understanding of what was
going on may have been wrong.
this may be off-topic.
Nope, definitely on topic.
all i want to know is why did it happen?
You didn't provide any code. However, the small program below
may demonstrate what you were doing wrong.
#include <stdio.h>
#include <string.h>
int main (void)
{
char str1 [] = "12345678" ;
char *str2 = "This is a string" ;
printf ("str1, %d %d\n", sizeof (str1), strlen (str1));
printf ("str2 %d %d\n", sizeof (str2), strlen (str2));
return 0;
}
On my machine (whcih is correct) it outputs:
str1 9 8
str2 4 16
For strings, you should probably use strlen instead of sizeof.
Erik
--
+-----------------------------------------------------------+
Erik de Castro Lopo
no****@mega-nerd.com (Yes it's valid)
+-----------------------------------------------------------+
"When your hammer is C++, everything begins to look like a thumb."
-- Steve Haflich, in comp.lang.c++