38
Operating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan ([email protected])

Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan ([email protected])

Embed Size (px)

Citation preview

Page 1: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

Operating Systems 2INC0 –

C course – Pointer Advanced

Dr. Ir. Ion Barosan ([email protected])

Page 2: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

Containt

• Pointers – Definition and Initilization

• Ponter Operators

• Pointer Arithmetic and Array

• Calling Functions by Reference

• Pointers to Pointers

• Returning A Pointer

• Pointers and const

• void Pointer

• Function Pointers

• Structure and Pointer to Structures

• Pointer to File

/ Faculteit Wiskunde en Informatica PAGE 1 14-9-2015

Page 3: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

Pointers – Definition and Initilization

• Pointer variables

• Contain memory addresses as their values

• Normal variables contain a specific value (direct reference)

− int a = 10;

• Pointers contain address of a variable that has a specific

value (indirect reference)

• Indirection – referencing a pointer value

int a = 10;

int *ptr = &a ;

/ Faculteit Wiskunde en Informatica PAGE 2 14-9-2015

10 10

a ptr

Page 4: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

Pointers – Definition and Initilization

• Pointer declarations

• * used with pointer variables

int *myPtr;

• Declares a pointer to an int (pointer of type int *)

• Multiple pointers require using a * before each variable

declaration

int *myPtr1, *myPtr2;

• Can declare pointers to any data type

• Initialize pointers to 0, NULL, or an address

• 0 or NULL – points to nothing (NULL preferred)

/ Faculteit Wiskunde en Informatica PAGE 3 14-9-2015

Page 5: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

Pointer Operators

• & (address operator)

• Returns address of operand

int y = 5;

int *yPtr;

yPtr = &y; // yPtr gets address of y

// yPtr “points to” y

/ Faculteit Wiskunde en Informatica PAGE 4 14-9-2015

yPtr

y

5

yptr

0x7A120 ox927C0

y

ox927C0 5

Address of y is

value of yptr

Address of y is

value of yptr

Address Address

Page 6: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

Ponter Operators

• * (indirection/dereferencing operator)

• Returns a synonym/alias of what its operand points to

• *yptr returns y (because yptr points to y)

• * can be used for assignment

− Returns alias to an object

*yptr = 7; // changes y to 7

• Dereferenced pointer (operand of *) must be an lvalue (no

constants)

• * and & are inverses

• They cancel each other out

/ Faculteit Wiskunde en Informatica PAGE 5 14-9-2015

Page 7: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

/ Faculteit Wiskunde en Informatica PAGE 6 14-9-2015

1 /*

2 Using the & and * operators */

3 #include <stdio.h>

4

5 int main()

6 {

7 int a; /* a is an integer */

8 int *aPtr; /* aPtr is a pointer to an integer */

9

10 a = 7;

11 aPtr = &a; /* aPtr set to address of a */

12

13 printf( "The address of a is %p"

14 "\nThe value of aPtr is %p", &a, aPtr );

15

16 printf( "\n\nThe value of a is %d"

17 "\nThe value of *aPtr is %d", a, *aPtr );

18

19 printf( "\n\nShowing that * and & are inverses of "

20 "each other.\n&*aPtr = %p"

21 "\n*&aPtr = %p\n", &*aPtr, *&aPtr );

22

23 return 0;

24 }

The address of a is 0012FF88

The value of aPtr is 0012FF88

The value of a is 7

The value of *aPtr is 7

Proving that * and & are complements of each other.

&*aPtr = 0012FF88

*&aPtr = 0012FF88

Page 8: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

More Pointers Example

/ Faculteit Wiskunde en Informatica PAGE 7 14-9-2015

Page 9: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

Pointers to Pointers

/ Faculteit Wiskunde en Informatica PAGE 8 14-9-2015

7 002DF71C 002DF728

x a b

002DF71C 002DF728

Page 10: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

Pointer Arithmetic and Array

/ Faculteit Wiskunde en Informatica PAGE 9 14-9-2015

• Type of a is float *

• a[2] *(a + 2)

ptr = &(a[2])

ptr = &(*(a + 2))

ptr = a + 2

• a is a memory address constant

• ptr is a pointer variable

float a[4];

float *ptr;

ptr = &(a[2]);

*ptr = 3.14;

ptr++;

*ptr = 9.0;

ptr = ptr - 3;

*ptr = 6.0;

ptr += 2;

*ptr = 7.0;

Page 11: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

Calling Functions by Reference

• Call by reference with pointer arguments

• Pass address of argument using & operator

• Allows you to change actual location in memory

• Arrays are not passed with & because the array name is

already a pointer

• * operator

• Used as alias/nickname for variable inside of function

void double( int *number )

{

*number = 2 * ( *number );

} // *number used as nickname for the variable

passed

/ Faculteit Wiskunde en Informatica PAGE 10 14-9-2015

Page 12: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

Calling Functions by Reference

• Modify behaviour in argument passing

/ Faculteit Wiskunde en Informatica PAGE 11 14-9-2015

void f(int j)

{

j = 5;

}

void g()

{

int i = 3;

f(i);

}

void f(int *ptr)

{

*ptr = 5;

}

void g()

{

int i = 3;

f(&i);

}

i = 3 i = 3 i = 5 i = 5

Page 13: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

Calling Functions by Reference

/ Faculteit Wiskunde en Informatica PAGE 12 14-9-2015

Page 14: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

Calling Functions by Reference

/ Faculteit Wiskunde en Informatica PAGE 13 14-9-2015

Pointers are also used in C to

enable a function to modify a

variable held by the caller:

Pointers are also used in C to

enable a function to modify a

variable held by the caller:

Page 15: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

Calling Functions by Reference

– Copy Example

/ Faculteit Wiskunde en Informatica PAGE 14 14-9-2015

Page 16: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

Pointers to Pointers

• A pointer can point to a pointer. One use of this is to

pass a pointer so that a function can modify it:

/ Faculteit Wiskunde en Informatica PAGE 15 14-9-2015

Page 17: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

Returning a Pointer (Good)

• Pointers can also be used as return values:

/ Faculteit Wiskunde en Informatica PAGE 16 14-9-2015

Page 18: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

Returning a Pointer (Bad)

• NEVER return a pointer to an automatic local object:

/ Faculteit Wiskunde en Informatica PAGE 17 14-9-2015

gcc compiler

warning: function

returns address

of local variable

Page 19: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

Pointers and const

/ Faculteit Wiskunde en Informatica PAGE 18 14-9-2015

Page 20: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

Pointers and const

• This provides safety against inadvertent changes to

a pointer and/or its target, and is certainly an under-

used feature in C.

/ Faculteit Wiskunde en Informatica PAGE 19 14-9-2015

Page 21: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

void Pointers

• In C, a pointer may be declared of type void:

void* p; // target can be of ANY type;

//no compile-time.type-checking occurs

• void pointers are not useful in many situations:

− the return value from malloc() is actually a void*

− they can be used to achieve generic programming, often

with data structures, but also with a number of useful

functions:

− void *memcpy(void *str1, const void *str2,

size_t n)

/ Faculteit Wiskunde en Informatica PAGE 20 14-9-2015

Page 22: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

void Pointers - Example

/ Faculteit Wiskunde en Informatica PAGE 21 14-9-2015

Page 23: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

void Pointer

• A void pointer can be really useful if the programmer is

not sure about the data type of data inputted by the end

user.

• The programmer can use a void pointer to point to the

location of the unknown data type

/ Faculteit Wiskunde en Informatica PAGE 22 14-9-2015

Page 24: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

void Pointer

• Pointer arithmetic can not be performed in a void

pointer.

/ Faculteit Wiskunde en Informatica PAGE 23 14-9-2015

ERROR

Page 25: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

Function Pointers

• Function Pointers are pointers - variables, which point to

the address of a function.

• Running program gets a certain space in the main-

memory:

• Thus a function in the program code is nothing else than an

address.

• It is only important how you, or better your

compiler/processor, interpret the memory a pointer points

to.

/ Faculteit Wiskunde en Informatica PAGE 24 14-9-2015

Page 26: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

Function Pointer int (*POINTER_NAME)(int a, int b);

/ Faculteit Wiskunde en Informatica PAGE 25 14-9-2015

Page 27: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

C structures : Overview

• A struct is a data structure composed for simpler data

types.

• Like a class in Java/C++ but without methods or

inheritance.

/ Faculteit Wiskunde en Informatica PAGE 26 14-9-2015

struct point {

int x;

int y;

};

void PrintPoint(point p)

{

printf(“(%d,%d)”, p.x, p.y);

}

Page 28: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

C structures: Pointers to them

• The C arrow operator (->) dereferences and extracts a

structure field with a single operator.

• The following are equivalent:

/ Faculteit Wiskunde en Informatica PAGE 27 14-9-2015

struct point *p;

printf(“x is %d\n”, (*p).x);

printf(“x is %d\n”, p->x);

Page 29: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

How big are structs?

• Recall C operator sizeof() which gives size in bytes

(of type or variable)

• How big is sizeof(struct p)?

struct p {

char x;

int y;

};

• 5 bytes? 8 bytes?

• Compiler may word align integer y

/ Faculteit Wiskunde en Informatica PAGE 28 14-9-2015

Page 30: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

Example Pointer To Structures

/ Faculteit Wiskunde en Informatica PAGE 29 14-9-2015

Page 31: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

Structure Declarations

• struct tag { member-list } variable-list;

struct P { typedef struct {

int a; int a;

char b; char b;

float c; float c;

double y; double y;

} Z[20], *ptrp; } Simple;

Simple x;

Simple y[20], *ptr;

/ Faculteit Wiskunde en Informatica PAGE 30 14-9-2015

Page 32: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

Example

/ Faculteit Wiskunde en Informatica PAGE 31 14-9-2015

Page 33: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

Structure Members

• struct COMPLEX {

float f;

int a[20];

long *lp;

struct Simple s;

struct Simple sa[10];

struct Simple *sp;

};

Struct COMPLEX comp;

((comp.sa)[4]).c;

comp.sa[4].c; // equivalent

/ Faculteit Wiskunde en Informatica PAGE 32 14-9-2015

Page 34: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

Structure Declarations

struct REF1 { struct REF1{

int a; int a;

struct REF1 b; struct REF1 *b;

float c; float c;

double y; double y;

}; // ERROR };

° The compiler knows the size of the *b pointer, even

before the size of the structure has be determined

° The pointer will be pointing to a different structure of the

same type

/ Faculteit Wiskunde en Informatica PAGE 33 14-9-2015

Page 35: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

Structure Declarations

° Structure that are mutually dependent:

struct B;

struct A { struct B{

char d; int a;

double c; float f[10];

struct B b ; struct A *pb;

}; };

° The solution to this problem is the incomplete declaration struct B, which declares an identifier to be a structure

tag.

/ Faculteit Wiskunde en Informatica PAGE 34 14-9-2015

Page 36: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

Pointer To Files

/ Faculteit Wiskunde en Informatica PAGE 35 14-9-2015

Page 37: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

36

Advice and Precaution

• Pros

• Efficiency

• Convenience

• Cons

• Error-prone

• Difficult to debug

Page 38: Operating Systems 2INC0 C course Pointer …tozceleb/2INC0/Slides_C_Pointers_Advanced.pdfOperating Systems 2INC0 – C course – Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl)

37

Summary

• A pointer stores the address (memory location) of another entity

• Address-of operator (&) gets the address of an entity

• De-reference operator (*) makes a reference to the referee of a pointer

• Pointer and array

• Pointer arithmetic