![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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/1.jpg)
Δείκτες-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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/2.jpg)
Προηγούμενα
•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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/3.jpg)
Επισκόπιση
•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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/15.jpg)
Τι σημαίνει *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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/16.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/17.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/18.jpg)
•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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/19.jpg)
•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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/20.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/21.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/22.jpg)
#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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/23.jpg)
#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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/24.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/25.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/26.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/27.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/28.jpg)
Επανάληψη
•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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/29.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/30.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/31.jpg)
#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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/32.jpg)
#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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/33.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/34.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/35.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/36.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/37.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081405/5f09e4dc7e708231d42902a0/html5/thumbnails/38.jpg)
•More of scanf demystified...
int num;
char str[100];
scanf(“%d %s”, &num, str);
str is an array of char