0% found this document useful (0 votes)
23 views17 pages

02 Pointers

This document discusses pointers and objects in C. It explains that pointers store references to other variables or data structures, allowing access to and manipulation of that data. Structures are used to group related data as one type, and pointers to structures can link complex objects and allow them to be manipulated without costly copying of the entire data structures.

Uploaded by

Lộc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views17 pages

02 Pointers

This document discusses pointers and objects in C. It explains that pointers store references to other variables or data structures, allowing access to and manipulation of that data. Structures are used to group related data as one type, and pointers to structures can link complex objects and allow them to be manipulated without costly copying of the entire data structures.

Uploaded by

Lộc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Pointers and Objects

Data Structures and Algorithm Analysis in C, by Mark Allen Weiss, 2nd


edition, 1997, Addison-Wesley, ISBN 0-201-49840-5
Danang University of Science and Technology

Dang Thien Binh


[email protected]
Readings and References
 Reading

 Other References
 Pointers and Memory, by Parlante

 Chapters 5 and 6, The C Programming Language, Kernighan and


Ritchie

Data Structures 2
What is a pointer?
 A pointer is a reference to another item
 the other item is sometimes called the "pointee"
 the pointee is often a variable
 the pointee can also be a function (a procedure)

 The contents of a pointer tell you where to look in order to


find the object of interest
 The declaration of the pointer says what it is supposed to
point to

Data Structures 3
Some declarations

int num; /* an integer */


int *numP; /* pointer to integer */

double sum; /* a double value */


double *sumP; /* pointer to double */

struct Symbol mySym; /* a Symbol */


struct Symbol *symP /* pointer to Symbol */

Data Structures 4
Reference and dereference

 Use "&" operator to take the address of a variable and


store it in a pointer variable
 numP = #
 sumP = ∑
 symP = &mySym;

 Then use "*" operator to dereference a pointer


 *numP = 42 is the same as num = 42
 *sumP = 17.0 is the same as sum = 17.0
 (*symP).val = 2 is the same as mySym.val = 2

Data Structures 5
Pointers and Pointees

numP = #
sumP = ∑
symP = &mySym;

*numP = 42;
*sumP = 17.0;
(*symP).val = 2;

printf("%i %3.1f %lu\n",num,sum,mySym.val);


printf("%i %3.1f %lu\n",*numP,*sumP,(*symP).val);
-----------------
42 17.0 2
42 17.0 2

Data Structures 6
Example from Pointers & Memory

A simple int variable.


The current value is the
num 42 integer 42. This variable
also plays the role of
pointee for the pointer
numP below.
numP &num A pointer variable. The
current value is a reference
to the pointee num above.

Data Structures 7
What form does data take?
 Integers
 -1, 0, 255, 65535, ...

 Floating point
 1.5, 3.14159, 1E75, …

 character strings
 "abc", "def"

 and that was about it in the old days

Data Structures 8
But real data is more complex
 Airplane definition
 engine count, crew count, passenger capacity, range, operating
cost per seat mile, …
 Student record
 name, student id, major, school address, home address, credits to
date, current enrollment, …
 Major fields of study
 responsible department, curriculum, students, ...

Data Structures 9
Why have structs?

 Because the logical objects


struct Symbol {
that you use in your programs char *name;
unsigned long val;
are more complex than just a };
single int or double value
struct Symbol oneSym;
 A structured block lets you struct Symbol twoSym;
struct Symbol mySym;
manipulate related data as one
oneSym.name = "one";
element oneSym.val = 1;

twoSym.name = "two";
twoSym.val = oneSym.val+1;

Data Structures 10
What is a struct variable?

 A single variable declared


as a struct refers to a oneSym
particular block of memory char *
 the individual fields are at unsigned long
twoSym
fixed offsets from the start char *
of the block unsigned long
mySym
char *
unsigned long

Data Structures 11
What can you do with a struct?

 The legal operations on a struct Symbol oneSym;


struct Symbol twoSym;
structure are struct Symbol mySym;
 accessing its members
oneSym.name = "one";
 copying it or assigning to it as a oneSym.val = 1;
unit
twoSym.name = "two";
 taking its address with & twoSym.val = oneSym.val+1;

mySym = twoSym;

Data Structures 12
How can structs be costly?
 Copying a struct is a nice automatic feature
 but it can lead to a lot of copying

 Our Symbol structs only require a few bytes


 but imagine the size of some of the other examples - airplanes,
student records, department descriptions
 Copying complete structs can get very costly very
quickly

Data Structures 13
Pointers to the rescue
 Take the address of a struct variable and store it in a
pointer variable
 Then you can manipulate the pointers, leaving the original
data where it is and just moving pointer values around
 An array of pointer values is one way to define a list of
objects (struct variables)

Data Structures 14
A short array of Symbol pointers

Symbol * char *
unsigned long
Symbol *

char

char *
unsigned long

char

Data Structures 15
Pointers allow you to link objects

nodeA nodeB nodeC


Node *next Node *next Node *next

Symbol *element Symbol *element Symbol *element

oneSym char *
struct Node { unsigned long
struct Node *next; twoSym char *
struct Symbol *element;
}; unsigned long

threeSym char *
struct Node *nodeA;
struct Node *nodeB; unsigned long
struct Node *nodeC;

Data Structures 16
nodeA = malloc(sizeof(struct Node));
FatalErrorMemory(nodeA);

nodeB = malloc(sizeof(struct Node));


FatalErrorMemory(nodeB);

nodeC = malloc(sizeof(struct Node));


FatalErrorMemory(nodeC);

(*nodeA).next = nodeB;
(*nodeA).element = &oneSym; Three Linked Nodes
nodeB->next = nodeC;
nodeB->element = &twoSym;
0x80498f0 (one) points to 0x8049900
0x8049900 (two) points to 0x8049910
nodeC->next = NULL;
0x8049910 (three) points to (nil)
nodeC->element = &threeSym;

nodeP = nodeA;
while (nodeP != NULL) {
printf("%p (%s) points to %p\n",
nodeP,nodeP->element->name,nodeP->next);
nodeP = nodeP->next;
}

Data Structures

You might also like