Download presentation
Presentation is loading. Please wait.
1
1 CS 201 Introduction to Pointers Debzani Deb
2
2 Incoming Exam Next Monday (5th March), we will have Exam #1. Closed book. Sit with an empty space in either side of you. Calculators that have text-allowing memory is not permitted. Covers Chapter 1-6 (Lecture Materials, Readings, Quiz, Linux basics, Makefile, Eclipse, Pointer & Labs). You should be able to answer correctly 80-85% of the exam, if you get hold of the above materials. The rest 15- 20% will be challenging. Email me/TA any question you have or see us at our office times. Don’t just wait for the last minute.
3
3 What is a Pointer? A pointer is a variable which points to a place in computer’s memory. It contains memory address as its value ( POINTER = ADDRESS) Pointer variable: a variable dedicated to hold memory addresses. A variable name directly references a value (i.e. content of a memory cell). A pointer indirectly references a value. Referencing a value through a pointer is called indirection. x int 35 06510 p int * 06582 06571 06582 content Address
4
4 Pointers and Addresses Addr1 Addr2 Addr3 Addr4 Addr5 Addr6... Addr11... Addr15 Content1 Content2 Content11 Content15 Memory can be conceptualized as a linear sequence of addressable locations. Variable reference the content of a location. Pointers have a value of the address of a location.
5
5 Size of Variable Types Everything in a computer program must fit into high speed memory. High speed memory is simply a string of bytes. Lots and Lots and Lots of bytes, but just that…bytes. Some number of bytes are allocated every time you define a variable.
6
6 High Speed Memory (RAM) 1073741823 0 1 2 3 4 5 1Gb of RAM
7
7 So What Is The Size Of Different Types? printf(“sizeof(int) = %d\n”, sizeof(int)); If you ran a short program with a bunch of these statements. sizeof(int) = 4 sizeof(long) = 4 sizeof(long int) = 4 sizeof(long long int) = 8 sizeof(char) = 1 sizeof(float) = 4 sizeof(double) = 8 sizeof(long double) = 12
8
8 So What Is The Size Of Different Types? sizeof(int) = 4 sizeof(long) = 4 sizeof(long int) = 4 (long long illegal on WinXp C++.Net) sizeof(char) = 1 sizeof(float) = 4 sizeof(double) = 8 sizeof(long double) = 8 Now, on to pointers! Depends on hardware, brand of OS and compiler. Here is Visual Studio.Net on Windows Xp Professional. (Differences are marked in red.)
9
9 Again What is Pointer? A variable containing the address of another variable. (Hence it “points” to the other location). There are “things” and “pointers to things” 3 thing thing_ptr ReferenceExplanationValue thingDirect value of the variable thing3 thing_ptrDirect value of variable thing_ptrPointer to location containing 3 *thing_ptrIndirect value of thing_ptr3
10
10 Structure Of A Pointer Symbol Table 0F45AB14 0F45AB18 0F45AB1C 0F45AB20 0F45AB24 0F45AB28 0F45AB2C int x=2,y=4,z=6; memory Address shown in hex. name address
11
11 Structure Of A Pointer Symbol Table X Y 0F45AB14 0F45AB18 0F45AB1C 0F45AB20 0F45AB24 0F45AB28 0F45AB2C Z int x=2,y=4,z=6; memory name address
12
12 Structure Of A Pointer Symbol Table X Y Z 0F45AB14 0F45AB18 0F45AB1C 0F45AB20 0F45AB24 0F45AB28 0F45AB2C X Y Z0F45AB1C 0F45AB28 0F45AB14 int x=2,y=4,z=6; 2 memory 4 6 name address Note, I have shown the value of the variable in memory, not the actual representation!
13
13 Structure Of A Pointer Symbol Table X Y Z 0F45AB14 0F45AB18 0F45AB1C 0F45AB20 0F45AB24 0F45AB28 0F45AB2C X Y Z0F45AB1C 0F45AB28 0F45AB14 int x=2,y=4,z=6; int * px; px = &x; memory 2 6 4 name address
14
14 Structure Of A Pointer Symbol Table X Y Z 0F45AB14 0F45AB18 0F45AB1C 0F45AB20 0F45AB24 0F45AB28 0F45AB2C X Y Z0F45AB1C 0F45AB28 0F45AB14 px memory px 0F45AB24 2 6 4 0F45AB14 name address int x=2,y=4,z=6; int * px; px = &x;
15
15 So, what is the size of a pointer? sizeof(int *) = 4 sizeof(long long int *) = 4 sizeof(float *) = 4 sizeof(double *) = 4 sizeof (char * ) = 4 Pointers to different types are all the same size. Once again, their size may be dependent on your hardware, OS, or even compiler. Now, back to my diagram.
16
16 Structure Of A Pointer Symbol Table X Y Z 0F45AB14 0F45AB18 0F45AB1C 0F45AB20 0F45AB24 0F45AB28 0F45AB2C X Y Z0F45AB1C 0F45AB28 0F45AB14 px memory px 0F45AB24 2 6 4 0F45AB14 name address int x=2,y=4,z=6; int * px; px = &x;
17
17 Structure Of A Pointer Symbol Table X Y Z 0F45AB14 0F45AB18 0F45AB1C 0F45AB20 0F45AB24 0F45AB28 0F45AB2C X Y Z0F45AB1C 0F45AB28 0F45AB14 px 0F45AB24 memory 2 6 4 px 0F45AB14 printf(“%d\n”,x); name address int x=2,y=4,z=6; int * px; px = &x;
18
18 Structure Of A Pointer Symbol Table X Y Z 0F45AB14 0F45AB18 0F45AB1C 0F45AB20 0F45AB24 0F45AB28 0F45AB2C X Y Z0F45AB1C 0F45AB28 0F45AB14 px 0F45AB24 memory 2 6 4 px 0F45AB14 printf(“%d\n”,x);2 name address int x=2,y=4,z=6; int * px; px = &x;
19
19 Structure Of A Pointer Symbol Table X Y Z 0F45AB14 0F45AB18 0F45AB1C 0F45AB20 0F45AB24 0F45AB28 0F45AB2C X Y Z0F45AB1C 0F45AB28 0F45AB14 px 0F45AB24 memory 2 6 4 px 0F45AB14 printf(“%d\n”,x);2 printf(“%d\n”,*px); int x=2,y=4,z=6; int * px; px = &x;
20
20 Structure Of A Pointer Symbol Table X Y Z 0F45AB14 0F45AB18 0F45AB1C 0F45AB20 0F45AB24 0F45AB28 0F45AB2C X Y Z0F45AB1C 0F45AB28 0F45AB14 int x,y,z; int * px = &x; px 0F45AB24 memory 2 6 4 px 0F45AB14 printf(“%d\n”,x);2 printf(“%d\n”,*px); 2
21
21 Pointer Declaration int * p; // pointer is declared p is a pointer pointing at an variable of type int. * tells the compiler that the variable named p is to be a pointer. The name of the pointer variable is p (not *p ). The size of the memory addressed by a pointer depends on the type of the object mentioned in declaration. int * p; // p points at 4 consecutive bytes double * p; // p points at 8 consecutive bytes
22
22 Operators Related To Pointers & address-of printf(“%p\n”,&variable); * dereference printf(“%d\n”, * pointer_variable)
23
23 Address of Operator (&) int x = 3; // Variable defined int * p; // Pointer declared p = &x; // Pointer p is set up to point at // the variable x. Address of variable x is assigned to pointer p. & is an unary address operator which returns the memory address of variable. &x is read as “address of x”
24
24 About Asterisk (*) In declaration * tells the compiler that a new variable is to be a pointer (read as “pointer to”) In Regular use It provides the content of the memory location specified by a pointer. It mean “follow the pointer”. It can also stand on the left side of an assignment. int * p x = * p * p = 10
25
25 Typical Uses Of Operators int thing; /* declare an integer (a thing) */ thing = 4; int * thing_ptr; /* declare a pointer to an integer */ Don’t need _ptr, just helps readability. At this moment, it is pointing to a random values since it has not been initialized yet. Such garbage can be harmful. thing_ptr = NULL; thing_ptr is a null pointer means “pointing to nothing”. thing_ptr = &thing; /* store address in pointer, points to thing */
26
26 Cont. *thing_ptr = 5; /* set thing to 5 */ *thing_ptr is a thing. thing_ptr is a pointer. Of course, thing_ptr could be set to point to other variables besides thing. thing_ptr = &something_else;
27
27 Illegal and Strange Uses *thing is illegal. thing is not a pointer variable. &thing_ptr is legal, but strange. “a pointer to a pointer”
28
28 Pointing to the Same Thing int something; int *first_ptr; /* one pointer */ int *second_ptr; /* another pointer */ something=1; first_ptr = &something; second_ptr = first_ptr; something=1; *first_ptr=1; *second_ptr=1; All identical results (Only one integer)
29
29 Again uses of & and * p = &x; // p points at x now. y = * p; // y has the value pointed out by the pointer p. * p= 13; // 13 was inserted to the // place pointed out by p. x int 35 06510 p int * 06510 06571 06510 y int 35 06587 x int 13 06510 p int * 06510 06571 06510
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.