Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8677003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:20:31+00:00 2026-06-12T20:20:31+00:00

My code: typedef struct { int sizeOfMyIntArray; int* myIntArray; float someFloat; } my_struct_foo; int

  • 0

My code:

typedef struct {
    int sizeOfMyIntArray;
    int* myIntArray;
    float someFloat;
} my_struct_foo;

int main() {
    printf("The size of float is: %d\n", sizeof(float));
    printf("The size of int is: %d\n", sizeof(int));
    printf("The size of int* is: %d\n", sizeof(int*));
    printf("The size of my_struct_foo is: %d\n", sizeof(my_struct_foo));
    return 0;
}

I’d imagine that this is very simple. Although, I am a bit surprised by the resulting output of this program…

The size of float is: 4
The size of int is: 4
The size of int* is: 8
The size of my_struct_foo is: 24

I have one float, one int, and one pointer-to-an-int. In my head I am thinking: 4 + 4 + 8 = 16…not 24. Why is the size of my structure 24 instead of 16?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T20:20:32+00:00Added an answer on June 12, 2026 at 8:20 pm

    Alignment and padding. The int* should be aligned on an eight-byte boundary, so the compiler inserts four bytes of padding between the int and the pointer, and after (or before) the float to make the size of the struct a multiple of the pointer size and have the pointer correctly aligned.

    If you reorder the members,

    typedef struct {
        int sizeOfMyIntArray;
        float someFloat;
        int* myIntArray;
    } my_struct_foo;
    

    the pointer would be correctly aligned without any padding, so the size of the struct would (very probably, the compiler is allowed to add padding even if it is not needed, but I know of none that does) be 16.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the code: main() { typedef struct { int data; } Information; typedef
I am curious why this code works: typedef struct test_struct { int id; }
Here is my code : typedef struct node { int data; struct node *next;
Given the code below: typedef struct {int a;} test_t; arbitrary_t test_dosomething(test_t* test) { if
I have the following code: typedef struct AdjMatrix { int nodes; int **adjMat; }
Consider the following C code snippet: typedef struct node{ int val; struct node* left;
so usually this typedef makes code cleaner: typedef struct { int x; } X;
If I will code some thing like following: typedef struct { int a; PTR1
I have this C code : #include<stdio.h> typedef struct { int foo; } MyStruct;
I encountered this in a code review: typedef struct C { int i; };

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.