0% found this document useful (0 votes)
8 views

Pointer in c

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Pointer in c

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

BCAC303

Data Structure and Algorithm

Pointer in C
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.)
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):
Pointer Variable Definition

Basic syntax: Type *Name


Examples:
int *P; /* P is var that can point to an int var */
float *Q; /* Q is a float pointer */
char *R; /* R is a char pointer */
Complex example:
int *AP[5]; /* AP is an array of 5 pointers to ints */
more on how to read complex declarations later
Address (&) Operator

The address (&) operator can be used in front of any variable object in C -- the result of
the operation is the location in memory of the variable
Syntax: &VariableReference
Examples:
int V;
int *P;
int A[5];
&V - memory location of integer variable V
&(A[2]) - memory location of array element 2 in array A
&P - memory location of pointer variable P
Pointer Variable Initialization/Assignment

NULL - pointer lit constant to non-existent address


used to indicate pointer points to nothing
Can initialize/assign pointer vars to NULL or use the address (&) op to get address of a
variable
variable in the address operator must be of the right type for the pointer (an integer
pointer points only at integer variables)
Examples:
int V;
int *P = &V;
int A[5];
P = &(A[2]);
Indirection (*) Operator

A pointer variable contains a memory address


To refer to the contents of the variable that the pointer points to, we use indirection
operator
Syntax: *PointerVariable
Example:
int V = 101;
int *P = &V;
/* Then *P would refer to the contents of the variable V (in this case, the integer 101) */
printf(“%d”,*P); /* Prints 101 */
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);
Pointer Return Values

A function can also return a pointer value:


float *findMax(float A[], int N) {
int I;
float *theMax = &(A[0]);

for (I = 1; I < N; I++)


if (A[I] > *theMax) theMax = &(A[I]);

return theMax;
}

void main() {
float A[5] = {0.0, 3.0, 1.5, 2.0, 4.1};
float *maxA;

maxA = findMax(A,5);
*maxA = *maxA + 1.0;
printf("%.1f %.1f\n",*maxA,A[4]);
}
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 */

printf(“%d %d %d\n”,V,*P,**Q); /* prints 101 3 times */


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
The General (void) Pointer

A void * is considered to be a general pointer


No cast is needed to assign an address to a void * or from a void * to another pointer
type
Example:
int V = 101;
void *G = &V; /* No warning */
float *P = G; /* No warning, still not safe */
Certain library functions return void * results (more later)
1D Arrays and Pointers

int A[5] - A is the address where the array starts (first element), it is equivalent to &(A[0])
A is in some sense a pointer to an integer variable
To determine the address of A[x] use formula:
(address of A + x * bytes to represent int) (address of array + element num * bytes for
element size)
The + operator when applied to a pointer value uses the formula above:
A + x is equivalent to &(A[x])
*(A + x) is equivalent to A[x]
1D Array and Pointers Example

float A[6] = {1.0,2.0,1.0,0.5,3.0,2.0};


float *theMin = &(A[0]);
float *walker = &(A[1]);

while (walker < &(A[6])) {


if (*walker < *theMin)
theMin = walker;
walker = walker + 1;
}

printf("%.1f\n",*theMin);
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
Understanding Complex Declarations

Right-left rule: when examining a declaration, start at the identifier, then read the first
object to right, first to left, second to right, second to left, etc.
objects:
Type
* - pointer to
[Dim] - 1D array of size Dim
[Dim1][Dim2] - 2D of size Dim1,Dim2
( Params ) - function
Can use parentheses to halt reading in one direction
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

You might also like