Comment obtenir la taille d'un tableau en C

Satishkumar Bharadwaj 12 octobre 2023
  1. L’opérateur sizeof() permet de déterminer la taille d’un tableau en C
  2. Obtenir la longueur du tableau en C
Comment obtenir la taille d'un tableau en C

Ce tutoriel présente la façon de déterminer la longueur d’un tableau en C. L’opérateur sizeof() est utilisé pour obtenir la taille/longueur d’un tableau.

L’opérateur sizeof() permet de déterminer la taille d’un tableau en C

L’opérateur sizeof() est un opérateur unaire de compilation. Il est utilisé pour calculer la taille de son opérande. Il retourne la taille d’une variable. L’opérateur sizeof() donne la taille dans l’unité de l’octet.

L’opérateur sizeof() est utilisé pour tout type de données telles que les primitives comme int, float, char, et aussi les types de données non primitives comme un array, struct. Il retourne la mémoire allouée à ce type de données.

The output may be different on different machines like a 32-bit system can show different output and a 64-bit system can show different of the same data types.

Syntaxe de sizeof() :

sizeof(operand)
  • L’operand est un type de données ou un opérande quelconque.

Opérateur pour les types de données primitives en C

Ce programme utilise un int, float comme type de données primitives.

#include <stdio.h>

int main(void) {
  printf("Size of char data type: %u\n", sizeof(char));
  printf("Size of int data type: %u\n", sizeof(int));
  printf("Size of float data type: %u\n", sizeof(float));
  printf("Size of double data type: %u\n", sizeof(double));

  return 0;
}

Production :

Size of char data type : 1 Size of int data type : 4 Size of float data
    type : 4 Size of double data type : 8

Obtenir la longueur du tableau en C

Si nous divisons la taille totale du tableau par la taille de l’élément du tableau, nous obtenons le nombre d’éléments du tableau. Le programme est le suivant :

#include <stdio.h>

int main(void) {
  int number[16];

  size_t n = sizeof(number) / sizeof(number[0]);
  printf("Total elements the array can hold is: %d\n", n);

  return 0;
}

Production :

Total elements the array can hold is: 16

Lorsqu’un tableau est passé en paramètre à la fonction, il est traité comme un pointeur. L’opérateur sizeof() retourne la taille du pointeur au lieu de la taille du tableau. Donc à l’intérieur des fonctions, cette méthode ne fonctionnera pas. A la place, passez un paramètre supplémentaire size_t size pour indiquer le nombre d’éléments dans le tableau.

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

void printSizeOfIntArray(int intArray[]);
void printLengthIntArray(int intArray[]);

int main(int argc, char* argv[]) {
  int integerArray[] = {0, 1, 2, 3, 4, 5, 6};

  printf("sizeof of the array is: %d\n", (int)sizeof(integerArray));
  printSizeOfIntArray(integerArray);

  printf("Length of the array is: %d\n",
         (int)(sizeof(integerArray) / sizeof(integerArray[0])));
  printLengthIntArray(integerArray);
}

void printSizeOfIntArray(int intArray[]) {
  printf("sizeof of the parameter is: %d\n", (int)sizeof(intArray));
}

void printLengthIntArray(int intArray[]) {
  printf("Length of the parameter is: %d\n",
         (int)(sizeof(intArray) / sizeof(intArray[0])));
}

Production (dans un OS Linux 64 bits) :

sizeof of the array is : 28 sizeof of the parameter is : 8 Length of the array
    is : 7 Length of the parameter is : 2

Production (dans un système d’exploitation Windows 32 bits) :

sizeof of the array is : 28 sizeof of the parameter is : 4 Length of the array
    is : 7 Length of the parameter is : 1

Article connexe - C Array