



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
Arrays and Pointers, Array variables as parameters, Operator Precedence, Initializing array elements, Multidimensional arrays, Array of C strings, Function Pointer. As you can see in this file, how descriptive above mentioned points are explained in this lecture of computer programming. VU is one of best university for computer science in our country.
Typology: Study notes
1 / 5
This page cannot be seen from the preview
Don't miss anything!




- Chapter 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
provinces [2][3][8][1] = variable;
The size of the array would be very large. You can calculate the amount of memory required by multiplying each subscript together, and then multiplying by the size of each element. The size for this array would be 50 x 500 x 1000 x 4 x 2 bytes = 200,000, bytes of memory, or 190.73 megabytes, which would be unacceptable on today's computers.
An array of C-strings is an array of arrays. Here is an example.
char days[] = {"Mon", "Tue", "Wed", "Thu", "Fri"};*
In the above days array each element is a pointer to a string, and they don't have to be of the same size. For example,
char * greetings[] = {"Hello", "Goodbye", "See you later"};
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.