Top Banner
Ahmed Hassan Dallal Tutorial: 3 Feb 2011
21

Ahmed Hassan Dallal - UniMasr.com · Introduction The variable pointed to by the pointer can be of any data type. Remember: the unary operator ‘&’ gives the address of an object.

May 15, 2018

Download

Documents

lamdiep
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Ahmed Hassan Dallal - UniMasr.com · Introduction The variable pointed to by the pointer can be of any data type. Remember: the unary operator ‘&’ gives the address of an object.

Ahmed Hassan DallalTutorial: 3Feb 2011

Page 2: Ahmed Hassan Dallal - UniMasr.com · Introduction The variable pointed to by the pointer can be of any data type. Remember: the unary operator ‘&’ gives the address of an object.

Acknowledgement Parts of this presentation are captured from Eng. Islam

Badreldin tutorials on Data Structures and Algorithms in C.

Page 3: Ahmed Hassan Dallal - UniMasr.com · Introduction The variable pointed to by the pointer can be of any data type. Remember: the unary operator ‘&’ gives the address of an object.
Page 4: Ahmed Hassan Dallal - UniMasr.com · Introduction The variable pointed to by the pointer can be of any data type. Remember: the unary operator ‘&’ gives the address of an object.

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.

Page 5: Ahmed Hassan Dallal - UniMasr.com · Introduction The variable pointed to by the pointer can be of any data type. Remember: the unary operator ‘&’ gives the address of an object.

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

data type.

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

Page 6: Ahmed Hassan Dallal - UniMasr.com · 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.

Page 7: Ahmed Hassan Dallal - UniMasr.com · Introduction The variable pointed to by the pointer can be of any data type. Remember: the unary operator ‘&’ gives the address of an object.

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

Page 8: Ahmed Hassan Dallal - UniMasr.com · Introduction The variable pointed to by the pointer can be of any data type. Remember: the unary operator ‘&’ gives the address of an object.

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

use the & operator.

Example:

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

Page 9: Ahmed Hassan Dallal - UniMasr.com · 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 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.

Page 10: Ahmed Hassan Dallal - UniMasr.com · 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 dereferencing

Page 11: Ahmed Hassan Dallal - UniMasr.com · Introduction The variable pointed to by the pointer can be of any data type. Remember: the unary operator ‘&’ gives the address of an object.

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.

Page 12: Ahmed Hassan Dallal - UniMasr.com · Introduction The variable pointed to by the pointer can be of any data type. Remember: the unary operator ‘&’ gives the address of an object.

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

change objects in the function that called it.

Page 13: Ahmed Hassan Dallal - UniMasr.com · Introduction The variable pointed to by the pointer can be of any data type. Remember: the unary operator ‘&’ gives the address of an object.

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:

Page 14: Ahmed Hassan Dallal - UniMasr.com · Introduction The variable pointed to by the pointer can be of any data type. Remember: the unary operator ‘&’ gives the address of an object.

Pointers and Arrays

Page 15: Ahmed Hassan Dallal - UniMasr.com · Introduction The variable pointed to by the pointer can be of any data type. Remember: the unary operator ‘&’ gives the address of an object.

Pointers and Arrays

Page 16: Ahmed Hassan Dallal - UniMasr.com · Introduction The variable pointed to by the pointer can be of any data type. Remember: the unary operator ‘&’ gives the address of an object.

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)

Page 17: Ahmed Hassan Dallal - UniMasr.com · Introduction The variable pointed to by the pointer can be of any data type. Remember: the unary operator ‘&’ gives the address of an object.

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.

Page 18: Ahmed Hassan Dallal - UniMasr.com · Introduction The variable pointed to by the pointer can be of any data type. Remember: the unary operator ‘&’ gives the address of an object.

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

let a function access and modify array elements:

Page 19: Ahmed Hassan Dallal - UniMasr.com · Introduction The variable pointed to by the pointer can be of any data type. Remember: the unary operator ‘&’ gives the address of an object.

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!

Page 20: Ahmed Hassan Dallal - UniMasr.com · Introduction The variable pointed to by the pointer can be of any data type. Remember: the unary operator ‘&’ gives the address of an object.

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.

Page 21: Ahmed Hassan Dallal - UniMasr.com · Introduction The variable pointed to by the pointer can be of any data type. Remember: the unary operator ‘&’ gives the address of an object.

Thank you