lecture 2 pointers“λώσσα c... · the & operator • gives the memory address of an...
Post on 20-Jun-2020
8 Views
Preview:
TRANSCRIPT
Δείκτες-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’;
A 0x2000
c:
Address in memory
Value
Example:
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
Example:
“conversion specifier” for
printing a memory address
char c;
printf(“The address of c: %p \n”, &c);
Pointers
•A pointer is a variable.
•Contains a memory address.
•Points to a specific data type.
•Pointer variables are usually named varPtr.
cPtr:
char* cPtr;
Example:
Can store an address of variables of type char
• We say cPtr is a pointer to char.
0x2000
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.
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.
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.
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)
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
Implicit Dereference (ίδιο σύμβολο – διαφορετική
σημασιολογία)
x = x + 1
Τιμή (dereference)
Έμμεση αποαναφοροποίηση Αναφορά
Τι σημαίνει *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
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
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
•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
•partPtr-> number = 120;
strcpy(partPtr-> name,“Bolt”);
partPtr: newPart:
newPart.name:
newPart.number:
Bolt
120
addr of
newPart
structure pointer operator
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;
Pointers and Function Arguments
•Example: swap the values of two variables
x: 1
y: 2
swap
x: 2
y: 1
#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
#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
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);
Notes on Pointers and Function Arguments (cont.)
•Large structure.
Example:
struct StudentRec
{
char name[MAXNAME];
int mark;
};
typedef struct StudentRec Student;
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
Επιστροφή Συνάρτησης
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:
Παράμετρος Συνάρτησης
Επανάληψη
•Pointers
•Pointer Operations
–address operator (&), and dereferencing operator (*)
•Pointers and Structures.
–structure pointer operator (->)
•Pointers and Function Arguments.
–Look at “swap” example
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:
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:
#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:
#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
Pointers and Arrays
•The name array is equivalent to &array[0]
Type array[size];
•The name *array is equivalent to array[0]
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)
Pointers and Arrays (cont.)
Type array[size];
•Array as a function argument
–pass the address of the first element:
array or &array[0]
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:
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
•More of scanf demystified...
int num;
char str[100];
scanf(“%d %s”, &num, str);
str is an array of char
top related