Understanding Pointers, Memory Management, and Dynamic Allocation, Study notes of Computer Science

An introduction to pointers in c programming, explaining how they use memory addresses as values, the difference between pointers and ordinary variables, pointer operations, output parameters, pointers and arrays, structures with pointers as values, and dynamic memory allocation using malloc and free functions.

Typology: Study notes

Pre 2010

Uploaded on 08/09/2009

koofers-user-zce-1
koofers-user-zce-1 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Pointers
Pointers use memory addresses as
values
Ordinary variables refer to memory
locations of various types
Integer variable int mint
Double variable double xval
Character variable char ch
Pointer variable: int * mptr
The value of this variable is the
location of an int
Address Contents
0 -24
1 .001
2 ‘C’
3
4
5 998
6
.
.
.
.
.
997
998 -17
999
pf3
pf4
pf5

Partial preview of the text

Download Understanding Pointers, Memory Management, and Dynamic Allocation and more Study notes Computer Science in PDF only on Docsity!

Pointers

•^

Pointers use memory addresses asvalues

•^

Ordinary variables refer to memorylocations of various types^ –

Integer variable

int mint

–^

Double variable

double xval

–^

Character variable

char ch

•^

Pointer variable:

int * mptr

  • The value of this variable is the

location of an int

Address

Contents

‘C’

Pointer operations •^

Value-pointed-to:^ –

int * mptr

-^

*mptr

•^

Address-of^ –

mptr = & mint;

-^

The variable is reassigned tobe the location of mint

-^

Now:

*mptr

Address

Contents

‘C’

mint

Pointers and arrays •^

When you declare an array,memory is allocated for the array.^ –

double values[ 7 ]

•^

Actually, the name of the array is apointer to the first location^ –

Equivalently, you could declare apointer

-^

int * valuesPtr;valuesPtr = values;

•^

Pointer arithmetic can be used toaccess elements of the array

•^

Can be Dangerous!

Structures with Pointer as values •^

You can create structures likelinked lists:^ –

struct entry{

int

value;

struct entry *next;

}

-^

Example: Using a linked list:struct entry node1, node2;node1.next = &node2;