?
Solved

size of a struct?

Posted on 2003-03-27
10
Medium Priority
?
340 Views
Last Modified: 2010-04-15
Hi,

I need to find the size of a struct, please see below for the code. I am working on Linux. I need to find the size of user[].

Cheers,


struct login
{
     char username[10];
     char time[10];
};

struct login user[];
0
Comment
Question by:daveyj1572
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 

Expert Comment

by:svatOpluk
ID: 8221162
struct login 20 bytes cause each char is 1 byte long
0
 

Expert Comment

by:svatOpluk
ID: 8221179
also u can use sizeof to see the size of any structure

size_t s = sizeof(struct STRUCT_NAME);

printf("%d", s);
0
 

Expert Comment

by:svatOpluk
ID: 8221203
if user[NUM_ELEM]
you can calculate it by sizeof(struct login) * NUM_ELEM
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:daveyj1572
ID: 8221211
the second option gives me:

'sizeof applied to incomplete type'

that's what I thought I would use but was presented with this error


0
 

Author Comment

by:daveyj1572
ID: 8221223
the second option gives me:

'sizeof applied to incomplete type'

that's what I thought I would use but was presented with this error


0
 
LVL 6

Expert Comment

by:gj62
ID: 8221351
Because C can have internal structure padding, you CANNOT simply add up the sizes of the internal members of the structs - that's what the sizeof operator is for:

As shown above, the correct way to get the size of the struct is:

size_t s = sizeof(struct STRUCT_NAME);

Now you CAN'T say:

struct login user[];

You have to give C the array size when you initialize a variable.

You could do:

struct login *user;

which creates a pointer to structures of type login, but allocates no storage.  At runtime, you could allocate 10 structures by saying

user = malloc(10*sizeof(login));

and then access them like:

strcpy(user[0].time, "09:15");
strcpy(user[5].username,"GLENN");

etc...

The size of memory allocated would be 10 * sizeof(login), same as in the malloc.  However the size of user would be the size of a pointer (4 bytes, on a 32 bit system).
0
 
LVL 1

Accepted Solution

by:
umangjoshi earned 150 total points
ID: 8223321
pls try this

sizeof(user);
0
 
LVL 1

Expert Comment

by:vamshi_pavan
ID: 8223382
umangjoshi is correct
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8223478
>> the second option gives me:

>> 'sizeof applied to incomplete type'

I think what daveyj1572 did was simply:

size_t s = sizeof(struct STRUCT_NAME);

instead of:

size_t s = sizeof ( struct login ) ;

Mayank.

0
 
LVL 1

Expert Comment

by:sarda_ramesh
ID: 8224068
ya mayank,
  i think u r rite ... he must be using it like that.

daveyj1572
  this is the complete code -

struct login
{
    char username[10];
    char time[10];
};

struct login user[5];//5 is the number of elements ..
                    //u can change it according to ur need


size_t s = sizeof(struct login);

//this will give u the size of one element of type login

size_t s = sizeof(user);

//this will give u the size of the array user


I THING THIS IS PREETY CLEAR NOW ..

regards
ramesh


0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses
Course of the Month9 days, 3 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question