473,395 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

sizeof error


i was in a cpp program calling a c program where in the cpp program
had defined a 80 character string passing to the c program where sizeo
had reported it was 4 when it passed the answer back it was wrong. thi
may be off-topic. all i want to know is why did it happen? or is anyon
else having this problem? tkanks for your patience and understandin
...most of my answers are off-topic sorry about that
-
MarcSmit
-----------------------------------------------------------------------
Posted via http://www.codecomments.co
-----------------------------------------------------------------------

Nov 14 '05 #1
4 2478
MarcSmith <Ma**************@mail.codecomments.com> 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. this
may be off-topic. all i want to know is why did it happen? or is anyone
else having this problem? tkanks for your patience and understanding
..most of my answers are off-topic sorry about that.


That's because you were applying 'sizeof' to a pointer and not to
an array. What you passed to the C function was a pointer to the
array (or its first element to be precise), not the array itself.
So what sizeof tells you is the size of the pointer. When you pass
an array to a function and you need want to know its size within
the function you have to pass the size together with the pointer
to the (first element of the) array.

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2
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++
Nov 14 '05 #3
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. this
may be off-topic. all i want to know is why did it happen?


Object types have sizes. Strings have lengths.
#include <string.h> and use strlen() to find string length.
The length of a string does not include the null byte.

The type of "\0\0\0\0\0\0" is (char[7]).

sizeof (char[7]) is seven.
sizeof "\0\0\0\0\0\0" is seven.
strlen("\0\0\0\0\0\0") is zero.

--
pete
Nov 14 '05 #4
pete wrote:
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. this
may be off-topic. all i want to know is why did it happen?


Object types have sizes. Strings have lengths.
#include <string.h> and use strlen() to find string length.
The length of a string does not include the null byte.

You didn't really answer his question, nor is your solution necessarily
correct. The string may not be what is desired at all, as the function
may be looking to append to the string. In that case, the size of the
buffer is desired. Not only that, but arrays do have sizes that can be
reported correctly by sizeof, just not in the case that was mentioned.
It wouldn't have mattered whether it was a string, an array of double,
or any other sort of array.

The reason that the OP encountered that condition is that arrays can't
be passed to functions. The name of the array is converted to a pointer
to the first element. So sizeof reported back the sizeof a char *,
which on that system happened to be 4.

Brian
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: besaar | last post by:
i got the current project that got the serial no.of a hard disk for software protection but i got an error,if u solve it send me,thanx alllllll. 1.file1 Option Strict On Option Explicit On...
19
by: Martin Pohlack | last post by:
Hi, I have a funtion which shall compute the amount for a later malloc. In this function I need the sizes of some struct members without having an instance or pointer of the struct. As...
18
by: Kenneth Brody | last post by:
Is sizeof() legal within a #if preprocessor line? Specifically, I would like to use the following "sanity check" at compile time, but I'm getting an error on the compile about the #if line. ...
43
by: Richard | last post by:
Could someone point me to why "sizeof x" is/isnt preferable to "sizeof(x)",
9
by: CptDondo | last post by:
I am missing something about structure declarations.... I am trying to get the size of a structure member using sizeof. my xml.h file (beware of line wrap): struct fieldSchedule_t { uint8_t...
5
by: Francois Grieu | last post by:
Does this reliably cause a compile-time error when int is not 4 bytes ? enum { int_size_checked = 1/(sizeof(int)==4) }; Any better way to check the value of an expression involving sizeof...
6
by: Daniel Rudy | last post by:
Hello Everyone, I've ran into a little snag with the sizeof operator. Can you get the size of a member inside a struct using sizeof? I looked through the FAQ and I couldn't find the answer to...
11
by: nevergone | last post by:
Hello Everybody In <<Modern C++ Design>Compile-Time Assertions there is : template <boolstruct CompileTimeChecker { CompileTimeChecker(...); }; template <struct CompileTimeChecker<false{ };
7
by: Yen Kwoon | last post by:
Note: This problem is related to gcc but after some back and forth in group gnu.gcc.help it seems to have morph into more of a c++ specificiation question, hence the transplanting to this group. ...
3
by: sh.vipin | last post by:
In the following program, with gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-56) there is no error. I have two queries. 1. isn't it illegal to apply sizeof on function type. 6.5.3.4 The...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.