


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Learn about arrays as a data structure for storing homogeneous data, their subscripts, multi-dimensional arrays, and passing arrays as parameters in c. Discover pointer concepts, including function pointers, and their importance in programming.
Typology: Study notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!



An array is a collection of elements of same type. An array stores many values in memory using only one name. "Array" in programming means approximately the same thing as array, matrix, or vector does in math. Unlike math, you must declare the array and allocate a fixed amount of memory for it. Subscripts are enclosed in square brackets []. Arrays are essentially sequential areas of memory (i.e. a group of memory addresses). However, we do not keep track of the whole array at once. This is because we only have a limited size of data to work with.
Subscript ranges always start at zero.
float x[100];
When an array is passed as a parameter, only the memory address of the array is passed (not all the values). An array as a parameter is declared similarly to an array as a variable, but no bounds are specified. The function doesn't know how much space is allocated for an array.
One important point to remember is that array indexes start from 0. Let’s say our array name is a of 10 ints, its first element will be a[0] while the last one will be a[9]. Other languages like Fortran carry out 1-based indexing. Due to this 0 based indexing for arrays in C language, programmers prefer to start loops from 0.
Arrays can also be multi-dimensional. In C language, arrays are stored in row major order that a row is stored at the end of the previous row. Because of this storage methodology, if we want to access the first element of the second row then we have to jump as many numbers as the number of columns in the first row. This fact becomes important when we are passing arrays to functions. In the receiving function parameters, we have to write all the dimensions of the array except the extreme-left one. When passing arrays to
functions, it is always call by reference by default; it is not call by value as in the default behavior of ordinary variables.
C contains many operators, and because of operator precedence, the interactions between multiple operators can become confusing. Operator precedence describes the order in which C evaluates expressions
For example, ( ) operator has higher precedence then [ ] operator.
The following table shows the precedence of operators in C. Where a statement involves the use of several operators, those with the lowest number in the table will be applied first.
float x[3] = {1.1, 2.2, 3.3}; float y[] = {1.1, 2.2, 3.3, 4.4};
Initializing an array can be taken place in many ways. In the first line of code, we are declaring and initializing an array having three elements in it. In the second array, we are initializing and declaring an array of 4 elements. Note that we have not specified the size of the array on the left side of assignment operator. In this case, the compiler will itself calculate the dimension or the size of the array after counting the initializer given in parenthesis, and will declare an array of corresponding size.
The number of dimensions an array may have is almost endless. To add more dimensions to an array simply add more subscripts to the array declaration. The example below will show how this can be done.
int provinces [50]; // This will declare an one dimensional array of 50 provinces.
int provinces[50][500]; // This will declare a two dimensional array of 50 provinces each including 500 cities.
int provinces[50][500][1000]; // This will declare a three dimensional array.
When using this n-dimensional array, each item would be having a unique position in memory. For example to access the person in the second province, third city, eighth home, and the first person in the home the syntax would be:
Function Pointers are pointers, i.e. variables, which point to the address of a function. You must keep in mind, that a running program gets a certain space in the main-memory. Both, the executable compiled program code and the used variables, are put inside this memory. Thus a function in the program code is, like e.g. a character field, nothing else than an address. It is only important how you, or better your compiler/processor, interpret the memory a pointer points to.
int (f1)(void);* // Pointer to function f1 returning int
As a function pointer is nothing else than a variable, it must be defined as usual. In the following example we define two function pointers named ptr2Function. It points to a function, which take one float and two char and return an int.
// define a function pointer
int (pt2Function) (float, char, char);*
The importance and use of Arrays should be very clear till now. Arrays are basically a data structure that is used to so store homogenous or same type of data in it. A very useful thing which we have analyzed here is that when we pass the name of the array as an argument to some function, then only the memory address of array is passed as parameters to the function and not all the values of an array are passed. In the end we have mentioned what the function pointers are? That is such pointers which points to the address of the function.