Size of struct value C++

goth_nerd

Expert Member
Joined
Apr 4, 2013
Messages
1,228
Reaction score
5
Location
Roodepoort
Hi guys

So I need some help, let's say I have a struct called "Customer" and let's say it looked like this:

Code:
struct Customer
{
char FirstName[20];
char Surname[20];
char ID[15];
char Country[50];
}

When populated the firstname is 10 characters long, I need to find the size of firstname. When I use sizeof(firstname) it brings back the size as 20 and not 10. Can someone explain this to me, I am very confused.

Thanks.
 
Size of returns the number of bytes used by the struct, which will indeed always be 20. It sounds as though you want the length of the string contained in the struct. Use strlen(...) for that.
 
Don't really know C++, but doesn't Sizeof just return the size of the CHAR - and not the size of the data in the CHAR?
 
Hi guys

So I need some help, let's say I have a struct called "Customer" and let's say it looked like this:

Code:
struct Customer
{
char FirstName[20];
char Surname[20];
char ID[15];
char Country[50];
}

When populated the firstname is 10 characters long, I need to find the size of firstname. When I use sizeof(firstname) it brings back the size as 20 and not 10. Can someone explain this to me, I am very confused.

Thanks.

AFAIK sizeof is always the size of the type of the variable, not the size of the actual variable. You want to use firstname.size() if I remember correctly.
 
Last edited:
the sizeof() works with structs, hence why we can use it there, even in C.
HOWEVER
using sizeof() of an arbitary variable returns the size of it i.e. 1 if it is a byte or char, etc.. hence the size of the type
 
AFAIK sizeof is always the size of the type of the variable, not the size of the actual variable. You want to use firstname.size() if I remember correctly.

It's just a char array, it has no methods. :). Strlen will do it, but it assumes that you have a null terminated string. Also, use strnlen(...) if you need to be safe.
 
It refers to the size of FirstName which you defined in struct as char[20]
cguy was right, strlen scans the char[20] looking for the 1st null character to determine the actual length.
 
It's just a char array, it has no methods. :). Strlen will do it, but it assumes that you have a null terminated string. Also, use strnlen(...) if you need to be safe.

I thought there was some magic behind the scenes:

// array::size
#include <iostream>
#include <array>

int main ()
{
std::array<int,5> myints;
std::cout << "size of myints: " << myints.size() << std::endl;
std::cout << "sizeof(myints): " << sizeof(myints) << std::endl;

return 0;
}

or is this effectively different ?
 
Last edited:
I thought there was some magic behind the scenes:

// array::size
#include <iostream>
#include <array>

int main ()
{
std::array<int,5> myints;
std::cout << "size of myints: " << myints.size() << std::endl;
std::cout << "sizeof(myints): " << sizeof(myints) << std::endl;

return 0;
}

or is this effectively different ?

elements count vs byte size.
 
I thought there was some magic behind the scenes:

// array::size
#include <iostream>
#include <array>

int main ()
{
std::array<int,5> myints;
std::cout << "size of myints: " << myints.size() << std::endl;
std::cout << "sizeof(myints): " << sizeof(myints) << std::endl;

return 0;
}

or is this effectively different ?

It's different - you are creating an array object from an array class there. The char a[N] style definition is a non-class type. Also size() for std::array, gives the structure size (in elements as pointed out), and doesn't related to the size of the represented string at all.
 
It's different - you are creating an array object from an array class there. The char a[N] style definition is a non-class type. Also size() for std::array, gives the structure size (in elements as pointed out), and doesn't related to the size of the represented string at all.

Ah, ok
 
So I have this stupid if statement that's driving me crazy....

Code:
int size = 0;
if (size == strlen(rec->BankAccountNo))
{

}
else
{

}

so when the condition is true it goes in to the "else" statement instead of going inside the true statement... I hope this makes sense.... Does someone have any idea what I could've done wrong?
 
I think you are trying to do this?

Code:
if (strlen(rec->BankAccountNo) == 0){

     // do something
} else {

     // It is not equal to zero

}

Some implementations return -1 if I remember correctly if there's no string...
so use a cast
 
Last edited:
Top
Sign up to the MyBroadband newsletter
X