Arrays and Pointers - Computer Programming - Lecture Notes, Study notes of Computer Engineering and Programming

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

2011/2012

Uploaded on 11/06/2012

ahsen
ahsen 🇵🇰

4.6

(88)

84 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 3
Arrays................................................................................................................................. 2
Subscripts start at zero......................................................................................2
Array variables as parameters...........................................................................2
Operator Precedence ........................................................................................3
Initializing array elements..................................................................................3
Multi-dimensional arrays ...................................................................................3
Array of C-strings ..............................................................................................4
Function Pointers ..............................................................................................4
Define a Function Pointer..................................................................................5
Summary ..............................................................................................................5
Tips .......................................................................................................................5
pf3
pf4
pf5

Partial preview of the text

Download Arrays and Pointers - Computer Programming - Lecture Notes and more Study notes Computer Engineering and Programming in PDF only on Docsity!

 - Chapter 
  • Arrays
    • Subscripts start at zero......................................................................................
    • Array variables as parameters...........................................................................
    • Operator Precedence ........................................................................................
    • Initializing array elements..................................................................................
    • Multi-dimensional arrays ...................................................................................
    • Array of C-strings ..............................................................................................
    • Function Pointers ..............................................................................................
    • Define a Function Pointer..................................................................................
  • Summary ..............................................................................................................
  • Tips .......................................................................................................................

Arrays

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.

Subscripts start at zero

Subscript ranges always start at zero.

float x[100];

  • first element of array is x[0]
  • last element of array is x[99]

Array variables as parameters

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.

Array of C-strings

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

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

Define a Function Pointer

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);*

Tips

  • Arrays should be used carefully since there is no bound checking done by the compiler.
  • Arrays are always passed by “reference” to some function
  • The name of the array itself is a constant pointer
  • Use function pointer only where it is required.

Summary

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.