Pointer
Pointer
Pointer in C language
Pointers
A pointer is a reference to another variable (memory
location) in a program
– Used to change variables inside a function (reference
parameters)
– Used to remember a particular member of a group (such
as an array)
– Used in dynamic (on-the-fly) memory allocation
(especially of arrays)
– Used in building complex data structures (linked lists,
stacks, queues, trees, etc.)
1
3/5/2024
Outline
Pointers
Basics
Variable declaration, initialization, NULL pointer
& (address) operator, * (indirection) operator
Pointer parameters, return values
Casting points, void *
Arrays and pointers
1D array and simple pointer
Passing as parameter
Dynamic memory allocation
calloc, free, malloc, realloc
Dynamic 2D array allocation (and non-square arrays)
Pointer Basics
Variables are allocated at addresses in computer memory
(address depends on computer/operating system)
Name of the variable is a reference to that memory address
A pointer variable contains a representation of an address of
another variable (P is a pointer variable in the following):
Name V P
Address v (some value) p (some value)
int V = 101;
Abstract
101
Representation
int *P = &V;
Concrete 4 bytes for 4 bytes for
Representation int value 101 mem address v
2
3/5/2024
3
3/5/2024
4
3/5/2024
Pointer Sample
int A = 3; Q = &B;
int B; if (P == Q)
int *P = &A; printf(“1\n”);
int *Q = P; if (Q == R)
int *R = &B; printf(“2\n”);
if (*P == *Q)
printf(“Enter value:“); printf(“3\n”);
scanf(“%d”,R); if (*Q == *R)
printf(“%d %d\n”,A,B); printf(“4\n”);
printf(“%d %d %d\n”, if (*P == *R)
*P,*Q,*R); printf(“5\n”);
Reference Parameters
To make changes to a variable that exist after a
function ends, we pass the address of (a pointer to)
the variable to the function (a reference parameter)
Then we use indirection operator inside the function
to change the value the parameter points to:
void changeVar(float *cvar) {
*cvar = *cvar + 10.0;
}
float X = 5.0;
changeVar(&X);
printf(“%.1f\n”,X);
5
3/5/2024
Pointers to Pointers
A pointer can also be made to point to a pointer
variable (but the pointer must be of a type that
allows it to point to a pointer)
Example:
int V = 101;
int *P = &V; /* P points to int V */
int **Q = &P; /* Q points to int pointer P */
6
3/5/2024
Pointer Types
Pointers are generally of the same size (enough bytes
to represent all possible memory addresses), but it
is inappropriate to assign an address of one type of
variable to a different type of pointer
Example:
int V = 101;
float *P = &V; /* Generally results in a Warning */
Warning rather than error because C will allow you
to do this (it is appropriate in certain situations)
Casting Pointers
When assigning a memory address of a variable of
one type to a pointer that points to another type it
is best to use the cast operator to indicate the cast
is intentional (this will remove the warning)
Example:
int V = 101;
float *P = (float *) &V; /* Casts int address to float * */
Removes warning, but is still a somewhat unsafe
thing to do
7
3/5/2024
8
3/5/2024
1D Array as Parameter
When passing whole array as parameter use syntax
ParamName[], but can also use *ParamName
Still treat the parameter as representing array:
int totalArray(int *A, int N) {
int total = 0;
for (I = 0; I < N; I++)
total += A[I];
return total;
}
For multi-dimensional arrays we still have to use the
ArrayName[][Dim2][Dim3]etc. form
Declarations Examples
int A A is a int
float B [5] B is a 1D array of size 5 of floats
int * C C is a pointer to an int
char D [6][3] D is a 2D array of size 6,3 of chars
int * E [5] E is a 1D array of size 5 of
pointers to ints
int (* F) [5] F is a pointer to a
1D array of size 5 of ints
int G (…) G is a function returning an int
char * H (…) H is a function returning
a pointer to a char
9
3/5/2024
10
3/5/2024
11
3/5/2024
calloc Example
float *nums;
int N;
int I;
12
3/5/2024
free Example
float *nums;
int N;
free(nums);
13
3/5/2024
14
3/5/2024
realloc Example
float *nums;
int I;
15
3/5/2024
Non-Square 2D Arrays
No need to allocate square 2D
arrays:
float **A; 0 1 2 3 4
int I;
A = (float **) calloc(5, 0
sizeof(float *)); 1
A 2
for (I = 0; I < 5; I++)
3
A[I] = (float **)
4
calloc(I+1,
sizeof(float));
16