Ahmed Hassan Dallal - UniMasr.com · Introduction The variable pointed to by the pointer can be of...

21
Ahmed Hassan Dallal Tutorial: 3 Feb 2011

Transcript of Ahmed Hassan Dallal - UniMasr.com · Introduction The variable pointed to by the pointer can be of...

Ahmed Hassan DallalTutorial: 3Feb 2011

Acknowledgement Parts of this presentation are captured from Eng. Islam

Badreldin tutorials on Data Structures and Algorithms in C.

Introduction Up till now, we accessed variables in memory directly

using the variable name.

Pointers provide a way of accessing a variable in an indirect way without referring to the variable itself but using the address of the variable in memory.

So, a pointer is a variable (location in memory) that holds an address of the location of some other variable.

Introduction The variable pointed to by the pointer can be of any

data type.

Remember: the unary operator ‘&’ gives the address of an object.

Pointer constant and pointer variable A pointer constant is an address while a pointer

variable is a memory location that stores addresses.

The array name is a constant pointer (address) to the first element of an array. You can not modify its value since the linker decides where the array will go and it will stay there during the program execution.

C enables you to define pointer variables where you can store addresses and also modify these addresses.

Declaring pointer variable The pointer variable definition looks like:

The asterisk* sign tells the compiler that this variable will hold an address and not a value.

The data type tells the compiler that the address carried in this pointer variable is the address of a variable of this particular data type.

<data type> * pointer variable name;

char *a; // pointer to char

The & operator to assign the an address of the variable to a pointer we

use the & operator.

Example:

char b=17; char *a=&b;

Pointer dereferencing The unary operator ‘*’ is the dereferencing operator.

When * applied to a pointer, it access the variable pointed by that pointer.

The *symbol has two uses , one is to tell the compiler that this variable is a pointer

and the other is to access the variable pointed to by the pointer.

The compiler can differentiate between them depending on the context of their appearance.

Pointer dereferencing

Pointers and Function Arguments We saw before with function that we can only return

one single value form the called function to the calling function, what if we want more that one variable to be modified by the called function?

One solution is to use pointers as the function input arguments,

The operands (arguments) are accessed indirectly through their addresses.

Pointers and Function Arguments Pointers arguments enable a function to access and

change objects in the function that called it.

Pointers and Arrays In C, there is a strong relation between pointers and

arrays, since the array’s name is a synonym for the location of the initial element.

The following 2 examples serve the same purpose:

Pointers and Arrays

Pointers and Arrays

Pointers and Arrays Example:

An array-and-index expression is equivalent to one written as a pointer and offset.

int a[10];int *pa;

pa = &a[0] // equivalent to pa = a// a[i] is equivalent to *(a+i)

Pointers and Arrays Notes:

A pointer is a variable, so pa = a and pa++ are legal, but

An array name is not a variable; constructions like a = pa and a++ are illegal.

Array names are considered as constant pointers.

When an array name is passed to a function, what is passed is the location of the initial element. Within the called function, this argument is a local variable, and so the array name parameter is a pointer.

Passing Arrays to functions This example illustrates how pointers can be used to

let a function access and modify array elements:

Bad pointers When a pointer is first allocated and not initialized, we

call it a bad pointer.

We do not know where the bad pointer is pointing at.

Do not dereference a bad pointer. It may result in program crash!

The NULL pointer The constant NULL is a special pointer value which

encodes the idea of “points to nothing”.

The C language uses the NULL symbol for this purpose.

It is a runtime error to dereference a NULL pointer (Segmentation fault – program crash)

The NULL pointer is nothing more than assigning a decimal value of zero as the address inside the pointer variable.

Thank you