You are on page 1of 9

Προηγούµενα ∆είκτες-Pointers

• • • • Basic Data Types Arrays Structs Typedef

• • • • • What are Pointers? Operations on Pointers. Pointers and Structures. Pointers and Function Arguments. Pointers and Arrays.


char c = ’A’; c:



Address in memory


2 . Contains a memory address. Points to a specific data type. Pointer variables are usually named varPtr.” Pointers • • • • A pointer is a variable. &c). printf(“The address of c: %p \n”. cPtr: 0x2000 Can store an address of variables of type char • We say cPtr is a pointer to char. Example: char* cPtr.The & Operator • Gives the memory address of an object char c = ’A’. A yields the value 0x2000 &c “conversion specifier” for printing a memory address •Also known as the “address operator. c: 0x2000 Example: char c.

Pointers and the & Operator Example: char c = ’A’. xPtr. c: 0x2000 cPtr: 0x2004 A 0x2000 3 . float * xPtr. Example: printf(“%p”. *cPtr. Notes on Pointers (cont. Example: int *numPtr. char *cPtr. numPtr). cPtr = &c. Assigns the address of c to cPtr.Notes on Pointers • We can have pointers to any data type. The * Operator •Allows pointers to access to variables they point to.) • The * can be anywhere between the type and the variable. Example: int *aPtr.” •Should not be confused with the * in the pointer declaration. aPtr and cPtr are integer pointers. • You can print the address stored in a pointer using the %p conversion specifier. but not bPtr. • Each pointer must be declared with an *. bPtr. •Also known as “dereferencing operator. Example: int* float* numPtr.

number = 120. y = 2. cPtr = &c. }. *aPtr = y. newPart: newPart.number = newPart. • (*partPtr).number: partPtr: Bolt 120 addr of newPart • newPart. aPtr = &y. newPart: newPart. *aPtr = 0. int x = 1. int* addr of newPart Part* partPtr = &newPart. strcpy(partPtr-> name.Pointers and the ∗ Operator Example: char c = ’A’. int number. y = *aPtr. z[3]. typedef struct PartRec newPart. char *cPtr. Part newPart.“Bolt”). x: 0x2000 aPtr = &x. strcpy(newPart. 4 . aPtr = &z[0].“Bolt”). y: 0x2004 z: 0x2008 c: 0x2000 cPtr: 0x2004 *aPtr = 4. *cPtr = ’B’.number: partPtr: • partPtr-> number = aPtr: 0x2020 B A 0x2000 Pointers and Structures Example: struct PartRec { char name[25].“Bolt”). Changes the value of the variable which cPtr points to. strcpy((*partPtr).

tmp = newPart. swap1(x. #include <stdio. • *aPtr allows the pointer to access the object. printf(“%d %d\n”.number: partPtr: Bolt 120 addr of newPart Operations Type Type* object.member is the same as aPtr−> member. y = 2.newPart: newPart. • (*aPtr).“Bolt”). structure pointer operator • aPtr = &object assigns the address of object to aPtr. • partPtr-> number = 120. strcpy(partPtr-> name. y). } tmp: a: b: x: y: 1 2 swap x: y: 2 1 main() { int x = 1. } x: y: 5 . x. b = tmp. a = b.h> Solution 1 Pointers and Function Arguments • Example: swap the values of two variables Void swap1(int a. int b) { int tmp. y). aPtr.

}. &y). y = 2. Notes on Pointers and Function Arguments (cont. next. x. printf(“%d %d\n”.mark: Bill 92 scanf(“%s %d”. &numy). *a = *b. } main() { int x = 1. next. &(next. swap2(&x. &numx. *b = tmp. typedef struct StudentRec Student. x: y: float numy. int* b) { int Student readStudent1(void) { Student next. • scanf demystified. int mark. } tmp: a: b: Notes on Pointers and Function Arguments • Change the value of a parameter. Example: struct StudentRec { char name[MAXNAME].#include <stdio. y). scanf(“%c %d %f”.) • Large structure.mark: Bill 92 6 . Επιστροφή Συνάρτησης next: next. } main() { Student student. tmp = *a.h> Solution 2 void swap2(int* a. } student: student. student = readStudent1() student.mark)). int numx. (δείκτης ήδη) (βασικός τύπος) return next. &ch. char ch.

Bill 92 – Look at “swap” example readStudent2(&student). } student: student. } (δείκτης ήδη) (βασικός τύπος) • Pointers and Structures. a = &x. Example: int int* int* x. Example: int int* int* x. a: addr of x b: addr of y x: y: Notes on Pointer Assignment • We can assign the value of a pointer to another pointer.mark: • Pointers and Function Arguments. b = &y. b = &y. y. a: addr of y b: addr of y x: y: 1 2 a = b.Παράµετρος Συνάρτησης With pointers: nextPtr : addr of student Revision • Pointers • Pointer Operations – address operator (&). a = & student. 1 2 7 . nextPtr->name. &(nextPtr->mark)). and deferencing operator (*) void readStudent2(Student *nextPtr) { scanf(“%s %d”. Notes on Pointer Assignment • We can assign the value of a pointer to another pointer. – structure pointer operator (->) main() { Student student. y.

x. y). &y). y = 2. *b = tmp.h> #include <stdio. } tmp: a: addr of x b: addr of y } main() { int x = 1. tmp = *a. a = b. x. swap2(&x. } main() { int x = 1. list = class. *b = tmp. b: addr of y void swap2(int* a. y = 2. y). printf(“%d %d\n”. &y). int* b) { int tmp. *a = *b. } tmp = *a. tmp: a: 1 addr of y x: y: x: y: 1 1 Pointers and Arrays Type array[size]. printf(“%d %d\n”. int* b) { int tmp.h> Recall: void swap2(int* a. list: addr of class[0] • The name array is equivalent to &array[0] • The name *array is equivalent to array[0] *(list + 0) *(list + 1) *(list + 2) list[0] list[1] list[2] class: class[0] class[1] class[2] (more on this later) 8 . swap2(&x.#include <stdio. Example: Student class[MAXCLASS]. Student* list.

size). readAll(&class[0]. i < size. i < size. scanf(“%d %s”.Pointers and Arrays (cont. } main() { Student class[MAXCLASS]. size).. int size = 10. i++) readStudent2(&(list[i])). readAll(class.) Type array[size]. for (i = 0. char str[100]. class: class[0] class[1] class[2] str is an array of char also okay 9 . int size) { int i. int size = 10. } • More of scanf demystified. for (i = 0. &num.. str). } • Array as a function argument – pass the address of the first element: array or &array[0] class: class[0] class[1] class[2] Example: list: addr of class[0] Recall: readAll(const Student *list. } main() { Student class[MAXCLASS]. int size) { int i. i++) readStudent2(&(list[i])). Example: list: addr of class[0] readAll(const Student *list. int num.