C - Unary operators - Using sizeof()

The sizeof() unary operator is often used, so it is important to know some tricks that we can do with it.

It is used to calculate the sizes of datatypes and it takes one argument: the type.

The main purpose of sizeof() is of course to allocate memory in conjuction with malloc.

1. Displaying size of types

In the example below, we can note the 1 byte of the *myString.

Indeed if we test the size of myString, we obtain 4 because this is a pointer on a char.
But in the case of *myString, we test the size of the first element pointed by the myString pointer.

So we can say that myString is equal to 4 bytes and can store 256 characters (starting at 0 up to 255). And that is why the size of *myString is 1.

Indeed, the size of *myString is equal to the size of "H", in this case 1, because "H" is a char. "H" because it is the first letter of the sentence "Hello World!".

The size of 1 byte is the same for **str, a pointer of a pointer. Indeed *str is a pointer and str too. Let's see an example of the sizeof() unary operator:

/*
** Made by BadproG
*/

#include <stdio.h>       

int     main()
{
  char	*myString;
  char 	**str;

  myString = "Hello World!";
  str = &myString;
  printf("Size of a char = %d byte.\n", sizeof(char));
  printf("Size of a short = %d bytes.\n", sizeof(short));
  printf("Size of a short* = %d bytes.\n", sizeof(short*));
  printf("Size of a int = %d bytes.\n", sizeof(int));
  printf("Size of a int* = %d bytes.\n", sizeof(int*));
  printf("Size of a long = %d bytes.\n", sizeof(long));
  printf("Size of a char* = %d bytes.\n", sizeof(char*));
  printf("Size of a *myString = %d bytes.\n", sizeof(*myString));
  printf("Size of a myString = %d bytes.\n", sizeof(myString));
  printf("Size of a **str = %d bytes.\n", sizeof(**str));
  printf("Size of a *str = %d bytes.\n", sizeof(*str));
  printf("Size of a str = %d bytes.\n", sizeof(str));
  printf("Size of a float = %d bytes.\n", sizeof(float));
  printf("Size of a double* = %d bytes.\n", sizeof(double*));
  printf("Size of a double = %d bytes.\n", sizeof(double));
  printf("Value of myString[0] = %c.\n", myString[0]);
  printf("Value of myString = %s\n", myString);
}

Result:

Size of a char = 1 byte.
Size of a short = 2 bytes.
Size of a short* = 4 bytes.
Size of a int = 4 bytes.
Size of a int* = 4 bytes.
Size of a long = 4 bytes.
Size of a char* = 4 bytes.
Size of a *myString = 1 bytes.
Size of a myString = 4 bytes.
Size of a **str = 1 bytes.
Size of a *str = 4 bytes.
Size of a str = 4 bytes.
Size of a float = 4 bytes.
Size of a double* = 4 bytes.
Size of a double = 8 bytes.
Value of myString[0] = H.
Value of myString = Hello World!

2. Displaying the size of an array

You need to stock the size of your array (for example with the type char *) to use it after in your code.

No problem, we can use the sizeof unary operator:

#include        <stdio.h>

int     main()
{
  char *charT[4] = {"f1","f2","f3","f4", "f5"};

  int   size1 = sizeof(charT);
  int   size2 = sizeof(charT) / sizeof(char *);

  printf("size1 = %d\n", size1);
  printf("size2 = %d\n", size2);
  return 0;
}

The result:

size1 = 40
size2 = 4

So it is important to notice that we have to divide the result of sizeof(myArray) by the size of the array type to obtain what we wanted: the size of the elements in the array.

Otherwise, as we can see, it returns the number of bits in the array!
In our case we have an array of char* and there is five elements inside.
We know that a char* is equal to 4 bytes.
We also know that a byte is equal to 8 bits, so the result of sizeof(myArray) is 5 * 8 = 40.

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.