Bifalgorithm post · Computer science one · Using Arrays with Functions

How are arrays passed to functions in C?

How are arrays passed to functions in C? (Computer science one · Using Arrays with Functions) — "Since arrays are represented using pointers, we can pass and r…

Ideas in this flowchart

  • Les tableaux peuvent être transmis aux fonctions à l’aide de pointeurs
  • Les tableaux sont représentés à l’aide de pointeurs
  • Un pointeur permet de transmettre et de retourner un tableau depuis une fonction
  • La taille du tableau doit également être communiquée à la fonction
  • La fonction ne dispose d’aucun moyen fiable pour déterminer elle-même la taille du tableau
  • Le pointeur accompagné d’un entier indique implicitement qu’il s’agit d’un tableau plutôt que d’une seule valeur
  • La fonction computeSum fournit un exemple de traitement d’un tableau d’entiers
  • La fonction reçoit un pointeur vers les éléments et un entier représentant leur nombre
  • Elle parcourt les éléments du tableau et calcule leur somme
  • Elle retourne la somme calculée
Feed/

How are arrays passed to functions in C?

No cover

Channel

Computer science one

1 page

"Since arrays are represented using pointers, we can pass and return arrays to and from functions simply by passing in a pointer. When passing an array to a function, we also need to make sure that we communicate to the function the size of the array as there is no reliable way for the function to determine this. By passing both a pointer and an integer representing the size, we are implicitly indicating that we are passing an array and not just a single value. As an example, the following function takes an array of integers and computes their sum.\n\n1 /**\n\n2 * This function computes the sum of elements in the\n\n3 * given array which contains n elements\n\n4 */\n\n5 int computeSum(int *arr, int n) {\n\n6 int i;\n\n7 int sum = 0;\n\n8 for(i=0; i<size; i++) {\n\n9 sum += arr[i];\n\n10 }\n\n11 return sum;\n\n12 }"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.