Docsity

Understanding Pointers in C: L-values, R-values, Dereferencing, and Memory Allocation, Study notes of Computer Science

Partial preview of the text

Download Understanding Pointers in C: L-values, R-values, Dereferencing, and Memory Allocation and more Study notes Computer Science in PDF only on Docsity!

z^ What is a pointer? z^ & and * operators z^ Using a pointer to access array elements z^ Using pointers to implement referenceparameters in C z^ New and delete z^ Using pointers to create dynamically sizedarrays

CSE 20232^ CSE 20232

Lecture 26 & 27Lecture

26 & 27 –

- PointersPointers

L-values and R-values z^ The l-value of an object is its address z^ The r-value of an object is its data value z^ The compiler determines from the C/C++statement whether to use an object’s l-valueor r-value

What is a pointer? z^ A pointer is an object that has as its value theaddress of another object z^ Or put another way, a pointer is an object that hasas its r-value the l-value of some other object z^ We say the pointer “point to” the other object z^ Why bother?^ z^

Pointers facilitate dynamic memory management z Pointers can be used to create linear and non-linear linkeddata structures (lists, trees, graphs, …) z Swapping pointer values is often more efficient thanmoving what they point to in momory

Pointer Related Operators z^ The & operator is used to explicitly return thel-value of an object^ z

Notice this is slightly different than the use of & inthe declaration of reference parameters

z^ The * operator is used to dereference apointer and return the r-value of the object itpoints to z^ Format of pointer declaration^ z

<data_type> *<pointer_name>;

Pointer Example

q

p

c

b

a

L-value R-Value Object

// declare integers int a(3), b, c; // declare pointers to integers int * p, *q;

Pointer Example

q

p

c

b

a

L-value R-Value Object

// declare integers int a(3), b, c; // declare pointers to integers int * p, *q; // set b to equal a b = a;

Pointer Example

q

p

c

b

a

L-value R-Value Object

// declare integers int a(3), b, c; // declare pointers to integers int * p, *q; // set b to equal a b = a; // set p to point to a p = &a; // set what p points to to 12 *p = 12;

Pointer Example

q

p

c

b

a

L-value R-Value Object

// declare integers int a(3), b, c; // declare pointers to integers int * p, *q; // set b to equal a b = a; // set p to point to a p = &a; // set what p points to to 12 *p = 12; // set q to point to what p does q = p;

Pointer Example

q

p

c

b

a

L-value R-Value Object

// declare integers int a(3), b, c; // declare pointers to integers int * p, *q; // set b to equal a b = a; // set p to point to a p = &a; // set what p points to to 12 *p = 12; // set q to point to what p does q = p; // move p to next int in memory p++; // sum what p & q point to c = *p + *q;

Using pointers to access andarray z^ Basic steps … z^ Set pointer (p) to point to first element z^ Use (*p) to set value of the array element thatp points to z^ Advance p to next element (p++) z^ Note: amount of increment is based on thesize of what is pointed to – sizeof(int), etc

Using a pointer to accessarray elements

A[3]
A[4]

i

A[2]
A[1]
A[0]

L-value R-Value Object

// declare array of doubles double A[5]; // and declare a loop index I int i; // use loop index to set array// all values to 100 for (i=0; i<5; i++)A[i] = 100.0;

// pass # 1

Using a pointer to accessarray elements

A[3]
A[4]

i

A[2]
A[1]
A[0]

L-value R-Value Object

// declare array of doubles double A[5]; // and declare a loop index I int i; // use loop index to set array// all values to 100 for (i=0; i<5; i++)A[i] = 100.0;

// pass # 2

Using a pointer to accessarray elements

A[3]
A[4]

i

A[2]
A[1]
A[0]

L-value R-Value Object

// declare array of doubles double A[5]; // and declare a loop index I int i; // use loop index to set array// all values to 100 for (i=0; i<5; i++)A[i] = 100.0;

// pass # 4

Using a pointer to accessarray elements

A[3]
A[4]

i

A[2]
A[1]
A[0]

L-value R-Value Object

// declare array of doubles double A[5]; // and declare a loop index I int i; // use loop index to set array// all values to 100 for (i=0; i<5; i++)A[i] = 100.0;

// pass # 5