w3resource

C Language: C program to find the size of a structure and why does the structure's area differ from each member's?

C – Find the size of a structure.

The sizeof( ) operator returns the number of bytes needed to store a variable or data type, so on most sytems, sizeof( int ) would yield 4, as would sizeof(number) if number were a variable of type int.

Code:

# include <stdio.h>

struct example {
    int a;
    char b;
    char str_name[20];
    double c;
    float d;
};

int main() {
    struct example e;
    printf("Size of example structure: %ld bytes\n", sizeof(e));
    printf("\nSize occupied by int a: %d\n",sizeof(e.a));
	printf("Size occupied by char b: %d\n",sizeof(e.b));
	printf("Size occupied by string str_name: %d\n",sizeof(e.str_name));
	printf("Size occupied by double c: %d\n",sizeof(e.c));
	printf("Size occupied by float d: %d\n",sizeof(e.d));
    return 0;
}

Output:

Size of example structure: 48 bytes

Size occupied by int a: 4
Size occupied by char b: 1
Size occupied by string str_name: 20
Size occupied by double c: 8
Size occupied by float d: 4

In the above program, we define a struct called example that contains three members of different types. We then create an instance of this struct, e, and use sizeof() to print the size of the struct.

Why does the structure's area differ from each member's?

The size of a structure is not always equal to the sum of the sizes of its members. Compilers may add padding between members to ensure that they are aligned properly in memory. Alignment requirements can vary depending on the CPU architecture and compiler options. The amount of padding between members can affect the structure's size.

Contribute your code and comments through Disqus.



Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

C Programming - How do you pass a function as a parameter in C?

Declaration

A prototype for a function which takes a function parameter looks like the following:

void func ( void (*f)(int) );

This states that the parameter f will be a pointer to a function which has a void return type and which takes a single int parameter. The following function (print) is an example of a function which could be passed to func as a parameter because it is the proper type:

void print ( int x ) {
  printf("%d\n", x);
}

Function Call

When calling a function with a function parameter, the value passed must be a pointer to a function. Use the function's name (without parentheses) for this:

func(print);

would call func, passing the print function to it.

Function Body

As with any parameter, func can now use the parameter's name in the function body to access the value of the parameter. Let's say that func will apply the function it is passed to the numbers 0-4. Consider, first, what the loop would look like to call print directly:

for ( int ctr = 0 ; ctr < 5 ; ctr++ ) {
  print(ctr);
}

Since func's parameter declaration says that f is the name for a pointer to the desired function, we recall first that if f is a pointer then *f is the thing that f points to (i.e. the function print in this case). As a result, just replace every occurrence of print in the loop above with *f:

void func ( void (*f)(int) ) {
  for ( int ctr = 0 ; ctr < 5 ; ctr++ ) {
    (*f)(ctr);
  }
}

Ref : https://bit.ly/3skw9Um