C Programming: Understanding Pointers and Dynamic Memory Allocation, Assignments of Computer Science

An introduction to pointer variables, the & (address) and * (indirection) operators, and dynamic memory allocation in c programming. It includes examples and explanations of how to declare pointer variables, assign addresses to pointers, and use the indirection operator to access memory locations. It also covers the concept of call-by-reference in function calls and the use of pointers in dynamic data structures like linked lists.

Typology: Assignments

Pre 2010

Uploaded on 03/16/2009

koofers-user-rqb
koofers-user-rqb 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
19&20-2
Know how to declare pointer variables.
Understand the & (address) and *(indirection)
operators.
Dynamic Memory Allocation
Related Chapter: FER Chapter 12.1, 12.2, 19.1,20
19&20-3
Pointer variables are variables that store memory addresses.
Pointers variables are useful in passing storage addresses in
function calls (call-by-reference) and for applications involving
dynamic data structures (e.g., linked lists)
Example: int *intPtr;
float *floatPtr;
declares intPtr to be a pointer variable to an object of type
integer. and floatPtr to be a pointer variable to an object of
type float.
19&20-4
We can write the pointer variable declaration
as int* intPtr;
or as int * intPtr;
Note that when we declare more than two pointer
variables in one line each pointer name requires an
asterisk:
int *ptr1, *ptr2;
otherwise we are just declaring a regular variable ,
not a pointer variable.
pf3
pf4
pf5

Partial preview of the text

Download C Programming: Understanding Pointers and Dynamic Memory Allocation and more Assignments Computer Science in PDF only on Docsity!

19&20-

• Know how to declare pointer variables.

• Understand the & (address) and *(indirection)

operators.

• Dynamic Memory Allocation

• Related Chapter: FER Chapter 12.1, 12.2, 19.1,

19&20-

Pointer variables are variables that store memory addresses.

Pointers variables are useful in passing storage addresses in function calls (call-by-reference) and for applications involving dynamic data structures (e.g., linked lists)

Example: int *intPtr;

float *floatPtr;

declares intPtr to be a pointer variable to an object of type

integer. and floatPtr to be a pointer variable to an object of

type float.

19&20-

We can write the pointer variable declaration

as int* intPtr;

or as int * intPtr;

Note that when we declare more than two pointer variables in one line each pointer name requires an asterisk:

int *ptr1, *ptr2;

otherwise we are just declaring a regular variable , not a pointer variable.

19&20-

There are two C operators that are necessary when using pointer variables.

& - the address operator returns the address of a variable

x

ptrX^1000

ptrX holds the address of x.

We say ptrX “points” to x.

We will apply the & operator only to variables, e.g. &77 or &(x+1) are not valid.

The declaration of ptrX initializes the variable ptrX = &x;

Example:

int x = 3;

int * ptrX = &x;

19&20-

Example:

int x; int * pt; pt = &x; /* another way to assign an address to a pointer variable */

Suppose the address of the variable x is 9640

Then, the effect of pt = &x; will be:

x pt

Address

19&20-

int x; int * pt; pt = &x; To assign the value “3” to the variable x in C: x = 3; We can use the “ * ” operator to indirectly reference x. We can assign “3” to x by:

*pt = 3;

Here we use the fact that “pt” points to integer values.

x pt

Address

9640^3^9640

* - means "a pointer to" and is called an indirection operator or

dereferencing operator, since a pointer "indirectly" references a value in a storage location. Example:

19&20-

double *ptr;

ptr = NULL;

Assigning a NULL value (zero) to a pointer

A null pointer points to nothing. We often depict it as

ptr

/called a null pointer/

19&20-

x

y

Main Memory while executing swap , after *ptrY = temp;

but before return;

ptrX 1000

ptrY 10043004

temp 1

19&20-

1. Problem Definition Write a program that reads a list of

real numbers into an array from the keyboard (or a file

using Unix redirection). The array can be of arbitrary

length but the user must first specify the length of the

array. The program then calculates the average value,

and then prints a list of differences. The differences

are computed by taking the original values in the list

minus the average.

2. Refine, Generalize, Decompose the problem definition

(i.e., identify sub-problems, I/O, etc.)

Input = Since the length of the array is specified at run-

time we must use Dynamic Memory Allocation. This is

accomplished in C by the use of the built-in function

calloc (or malloc). Use data-type double to hold the

values.

Output= The average and the list of differences.

#include <stdio.h> #include <stdlib.h>

void main(void) { int k,num; double * datValptr; /* pointer used in DMA / double datAve; double sum = 0.0; / prompt the user for the number of array elements / printf("Please enter the number of array elements:"); scanf("%i",&num); / use calloc to allocate a block of memory / datValptr = calloc(num,sizeof(double)); / verify that calloc was successful */ if (datValptr = = NULL) { printf("Memory not allocated!\n"); return; } (continued on next slide)

/* read the values and compute the average */

k = 0; for(k=0;k<num;++k) {

scanf("%lf", &datValptr[k]);/* use pointer as array name */ sum += datValptr[k]; }

/* compute the average */

datAve = sum /num;

printf("The average is:%f \n", datAve); /* compute and print the diff list */ for(k=0;k<num;++k) {

printf("%f\n", datValptr[k]-datAve); } /* end of for*/

/* free up any memory that was dynamically allocated */ free(datValptr);

} /* end of main */

19&20-

dclsn75> ./a.out

Please enter the number of array elements:

The average is:3.

dclsn75>

19&20-

The array name in C is assigned the address of the first element of the array.

The following code will assign the address of x[0] to

xPtr :

float x[50];

float * xPtr;

xPtr = x;

The assignment statement above is equivalent to:

xPtr = &x[0];

19&20-

In C, we can dynamically allocate storage with either of

the following two functions (both are in <stdlib.h>).

• malloc(numberOfBytes)

• calloc(numberOfItems, itemSize)

Both functions return a pointer to the address of the block

of storage that has been allocated. If no memory is

available, a NULL value will be returned.

Function calloc returns a contiguous block of

locations that are initialized to 0 and that can be

referenced as array locations, using pointer operations.

19&20-

In both cases above (calloc or malloc) return memory

locations to the system ("memory manager") with:

free (ptr);

Example: Declare ptr as a pointer variable to data-

type double and use dynamic memory allocation

(calloc or malloc) to allocate 100 elements of data-

type double.

double * ptr;

ptr = calloc(100,sizeof(double));

or

ptr = malloc(100*sizeof(double));