Pointer in c
Pointer in c
Pointer in C
Pointers
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
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
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 */
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
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
printf("%.1f\n",*theMin);
1D Array as Parameter
When passing whole array as parameter use syntax ParamName[], but can also use
*ParamName
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