



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 6
This page cannot be seen from the preview
Don't miss anything!




19&20-
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)
type float.
19&20-
We can write the pointer variable declaration
Note that when we declare more than two pointer variables in one line each pointer name requires an asterisk:
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.
We will apply the & operator only to variables, e.g. &77 or &(x+1) are not valid.
Example:
19&20-
Example:
int x; int * pt; pt = &x; /* another way to assign an address to a pointer variable */
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.
dereferencing operator, since a pointer "indirectly" references a value in a storage location. Example:
19&20-
A null pointer points to nothing. We often depict it as
ptr
19&20-
19&20-
#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-
19&20-
The array name in C is assigned the address of the first element of the array.
The assignment statement above is equivalent to:
19&20-
19&20-