Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A...

38
Δείκτες-Pointers

Transcript of Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A...

Page 1: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

Δείκτες-Pointers

Page 2: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

Προηγούμενα

•Basic Data Types

•Arrays

•Structs

•Typedef

Page 3: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

Επισκόπιση

•What are Pointers?

•Operations on Pointers.

•Pointers and Structures.

•Pointers and Function Arguments.

•Pointers and Arrays.

Page 4: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

char c = ’A’;

A 0x2000

c:

Address in memory

Value

Example:

Page 5: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

The & Operator

• Gives the memory address of an object

A

c:

0x2000

char c = ’A’;

•Also known as the “address operator.”

&c yields the value 0x2000

Page 6: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

Example:

“conversion specifier” for

printing a memory address

char c;

printf(“The address of c: %p \n”, &c);

Page 7: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

Pointers

•A pointer is a variable.

•Contains a memory address.

•Points to a specific data type.

•Pointer variables are usually named varPtr.

Page 8: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

cPtr:

char* cPtr;

Example:

Can store an address of variables of type char

• We say cPtr is a pointer to char.

0x2000

Page 9: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

Notes on Pointers

• You can print the address stored in a pointer using the %p conversion specifier.

printf(“%p”, numPtr); Example:

int* numPtr;

float* xPtr; Example:

• We can have pointers to any data type.

Page 10: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

Notes on Pointers (cont.)

int *numPtr;

float * xPtr; Example:

• The * can be anywhere between the

type and the variable.

int *aPtr, bPtr, *cPtr; Example:

• Each pointer must be declared with an *.

aPtr and cPtr are integer pointers, but not bPtr.

Page 11: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

Pointers and the & Operator

Example:

A

c:

0x2000

char c = ’A’;

cPtr:

char *cPtr;

0x2004

cPtr = &c;

0x2000

Assigns the

address of c to

cPtr.

Page 12: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

The * Operator

•Allows pointers to access to variables they

point to.

•Also known as “dereferencing operator.”

•Should not be confused with the * in the

pointer declaration.

•Ρητή απο-αναφοροποίηση (explicit

dereference)

Page 13: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

Pointers and the Operator

Example:

A

c:

0x2000

char c = ’A’;

cPtr:

char *cPtr;

0x2004

cPtr = &c;

0x2000

*cPtr = ’B’;

Changes the value

of the variable which

cPtr points to.

B

Page 14: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

Implicit Dereference (ίδιο σύμβολο – διαφορετική

σημασιολογία)

x = x + 1

Τιμή (dereference)

Έμμεση αποαναφοροποίηση Αναφορά

Page 15: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

Τι σημαίνει *xPtr; Ρητή απο-αναφοροποίηση

int x;

x = 0;

x = x + 1;

int *xPtr;

xPtr = &x;

*xPtr = 0;

*xPtr = *xPtr +1;

Αναφορά

στην x

Τιμή

της x

Αναφορά

στην x

Τιμή

της x

&x x xPtr

Αναφορά

στην xPtr

Τιμή

της xPtr

Page 16: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

int x = 1, y = 2, z[3];

int* aPtr;

aPtr = &x;

y = *aPtr;

*aPtr = 0;

aPtr = &y;

*aPtr = 4;

aPtr = &z[0];

*aPtr = y;

x:

y:

z:

aPtr:

0x2000

0x2004

0x2008

0x2020

Page 17: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

Pointers and Structures

Example:

typedef struct PartRec Part;

struct PartRec

{

char name[25];

int number;

};

Part newPart;

newPart:

newPart.name:

newPart.number:

Part* partPtr = &newPart;

partPtr:

addr of

newPart

Page 18: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

•newPart.number = 120;

strcpy(newPart.name,“Bolt”);

•(*partPtr).number = 120;

strcpy((*partPtr).name,“Bolt”);

•partPtr-> number = 120;

strcpy(partPtr-> name,“Bolt”);

partPtr: newPart:

newPart.name:

newPart.number:

Bolt

120

addr of

newPart

Page 19: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

•partPtr-> number = 120;

strcpy(partPtr-> name,“Bolt”);

partPtr: newPart:

newPart.name:

newPart.number:

Bolt

120

addr of

newPart

structure pointer operator

Page 20: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

Operations

aPtr = &object assigns the address of object to aPtr.

*aPtr allows the pointer to access the object.

(έμμεση αναφορά)

(*aPtr).member is the same as aPtr member.

Type object;

Type* aPtr;

Page 21: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

Pointers and Function Arguments

•Example: swap the values of two variables

x: 1

y: 2

swap

x: 2

y: 1

Page 22: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

#include <stdio.h>

Void swap1(int a, int b)

{

int tmp;

tmp = a;

a = b;

b = tmp;

}

main()

{

int x = 1, y = 2;

swap1(x, y);

printf(“%d %d\n”, x, y);

}

x:

y:

a:

b:

tmp:

Solution(?) 1

Page 23: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

#include <stdio.h>

void

swap2(int* a, int* b)

{

int tmp;

tmp = *a;

*a = *b;

*b = tmp;

}

main()

{

int x = 1, y = 2;

swap2(&x, &y);

printf(“%d %d\n”, x, y);

}

x:

y:

a:

b:

tmp:

Solution 2

Page 24: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

Notes on Pointers and Function Arguments

•Change the value of a parameter.

scanf demystified.

char ch;

int numx;

float numy;

scanf(“%c %d %f”, &ch, &numx, &numy);

Page 25: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

Notes on Pointers and Function Arguments (cont.)

•Large structure.

Example:

struct StudentRec

{

char name[MAXNAME];

int mark;

};

typedef struct StudentRec Student;

Page 26: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

Student readStudent1(void)

{

Student next;

scanf(“%s %d”, next.name, &(next.mark));

(δείκτης ήδη) (βασικός τύπος)

return next;

}

main()

{

Student student;

student = readStudent1();

}

next:

next.name:

next.mark:

student:

student.name:

student.mark:

Bill

92

Bill

92

Επιστροφή Συνάρτησης

Page 27: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

void readStudent2(Student *nextPtr)

{

scanf(“%s %d”, nextPtr->name, &(nextPtr->mark));

} (δείκτης ήδη) (βασικός τύπος)

main()

{

Student student;

readStudent2(&student);

}

student:

student.name:

student.mark:

Bill

92

With pointers:

addr of student

nextPtr:

Παράμετρος Συνάρτησης

Page 28: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

Επανάληψη

•Pointers

•Pointer Operations

–address operator (&), and dereferencing operator (*)

•Pointers and Structures.

–structure pointer operator (->)

•Pointers and Function Arguments.

–Look at “swap” example

Page 29: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

Notes on Pointer Assignment

•We can assign the value of a pointer to another

pointer.

Example:

int x, y;

int* a = &x;

int* b = &y;

1

2

x:

y:

addr of x

addr of y

a:

b:

Page 30: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

Notes on Pointer Assignment

•We can assign the value of a pointer to

another pointer.

Example:

int x, y;

int* a = &x;

int* b = &y;

a = b; 1

2

x:

y:

addr of y

addr of y

a:

b:

Page 31: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

#include <stdio.h>

void

swap2(int* a, int* b)

{

int tmp;

tmp = *a;

*a = *b;

*b = tmp;

}

main()

{

int x = 1, y = 2;

swap2(&x, &y);

printf(“%d %d\n”, x, y);

}

x:

y:

addr of x

addr of y

a:

b:

tmp:

Page 32: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

#include <stdio.h>

void

swap2(int* a, int* b)

{

int tmp;

tmp = *a;

a = b;

*b = tmp;

}

main()

{

int x = 1, y = 2;

swap2(&x, &y);

printf(“%d %d\n”, x, y);

}

1 x:

y:

addr of y

a:

b:

1 tmp:

addr of y

1

Page 33: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

Pointers and Arrays

•The name array is equivalent to &array[0]

Type array[size];

•The name *array is equivalent to array[0]

Page 34: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

Example:

Student class[MAXCLASS];

class:

class[0]

class[1]

class[2]

Student* list;

list:

list[0]

list[1]

list[2]

list = class;

addr of class[0]

*(list + 0)

*(list + 1)

*(list + 2)

Page 35: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

Pointers and Arrays (cont.)

Type array[size];

•Array as a function argument

–pass the address of the first element:

array or &array[0]

Page 36: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

main()

{

Student class[MAXCLASS];

int size = 10;

readAll(class, size);

}

Example:

readAll(const Student *list, int size)

{

int i;

for (i = 0; i < size; i++)

readStudent2(&(list[i]));

} class:

class[0]

class[1]

class[2]

addr of class[0]

list:

Page 37: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

main()

{

Student class[MAXCLASS];

int size = 10;

readAll(&class[0], size);

}

Example:

readAll(const Student *list, int size)

{

int i;

for (i = 0; i < size; i++)

readStudent2(&(list[i]));

} class:

class[0]

class[1]

class[2]

addr of class[0]

list:

also ok

Page 38: Lecture 2 Pointers“λώσσα C... · The & Operator • Gives the memory address of an object A c: 0x2000 char c = ’A’; •Also known as the “address operator.” &c yields

•More of scanf demystified...

int num;

char str[100];

scanf(“%d %s”, &num, str);

str is an array of char