0

I was studying how to define a structure in various ways. While outputting the structure in various formats, the size of the structure I calculated and the size of the output structure came out different.

#include <stdio.h>
#include <stdlib.h>

/* Way #1 */


typedef struct listNode {
             char data[5];
             int a;
             double c;
        }nodelist;

/* Way #2 */
/*
typedef struct listNode *listPointer;
typedef struct listNode {
             char data[5];
             int a;
             double c;
        }nodelist;
*/
void main()
{
    nodelist *mylist = malloc(sizeof(nodelist));
    printf("size of mylist = %lu\n", sizeof(*mylist));
    printf("size of mylist = %lu\n", sizeof(nodelist));
    printf("size of mylist = %lu\n", sizeof(struct listNode));
    free(mylist);
}

In my opinion, the value when calculating the structure is a total of 17 bytes ( char(1byte) * 5 + int(4byte) * 1 + double(8byte) *1 == 5 + 4 + 8 == 17byte ) should appear The actual output was 25 bytes.

Why is this happening?

  • You should better not use the term "value" here. Instead it is called the "size". "Value" would refer to the content of the struct members. – Gerhardh Apr 19 '22 at 17:10

0 Answers0