Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1
Introduction
Pointer Variable Definitions and Initialization
Pointer Operators
Calling Functions by Reference
Using the const Qualifier with Pointers
Pointer Expressions and Pointer Arithmetic
Relationship between Pointers and Arrays
Arrays of Pointers
Pointer is the address (i.e. a specific memory location) of an
object.
It can refer to different objects at different times.
Pointers are used in C programs for a variety of purposes:
To return more than one value from a function(using pass
by reference)
To create and process strings
To manipulate the contents of arrays and structures
To construct data structures whose size can grow or shrink
dynamically
Pointer variables
Contain memory addresses as their values
Normal variables contain a specific value (direct
reference) iNum
7
piNum iNum
7
Pointer definitions
* used with pointer variables
int *piNum;
Defines a pointer to an int (pointer of type int *)
Multiple pointers require using a * before each variable
definition
int *piNum1, *piNum2;
Can define pointers to any data type
Initialize pointers to 0, NULL, or an address
0 or NULL points to nothing (NULL preferred)
int *piNum = NULL; or int *piNum = 0;
Symbol & is called address operator
Returns address of operand
int iNum = 7;
int *piNum;
piNum = &iNum; /* piNum gets address of iNum */
piNum points to iNum
piNum iNum
iNum
7 500000 600000 600000 7
piNum
Address of
iNum is value
of piNum
Symbol * is called indirection/dereferencing operator
Returns a synonym/alias of what its operand points to
e.g. *piNum returns iNum (because piNum points to iNum)
* can also be used for assignment
Returns alias to an object
e.g. *piNum = 10; /* changes iNum to 10 */
Dereferenced pointer (operand of *) must be an
lvalue (no constants)
* and & are inverses
They cancel each other out
#include <stdio.h>
int main()
{
int iNum;
int *piNum;
int iNum1=5;
iNum = 7;
*piNum = 15;
printf("Dereferencing pointer, *piNum = %d\n", *piNum);
iNum = iNum + iNum1;
printf(iNum = %d\n, iNum);
printf("*piNum = %d\n", *piNum);
printf("*piNum + iNum1 = %d\n", *piNum + iNum1);
return 0;
}
number = 7
#include <stdio.h> piNum points to iNum whereby the value is = 7
int main() Address of piNum : 1245060 Contents of piNum : 1245064
{ Address of iNum : 1245064
int iNum;
int *piNum;
Dereferencing pointer, *piNum = 15
int iNum1=5;
iNum = 20
iNum = 7; *piNum = 20
*piNum + iNum1 = 25
printf("number = %d\n", iNum);
piNum = &iNum;
printf(piNum points to iNum whereby the value is = %d\n",*piNum);
printf("Address of piNum : %d Contents of piNum : %d\n", &piNum, piNum);
printf("Address of iNum : %d\n\n", &iNum);
*piNum = 15;
printf("Dereferencing pointer, *piNum = %d\n", *piNum);
iNum = iNum + iNum1;
printf(iNum = %d\n, iNum);
printf("*piNum = %d\n", *piNum);
printf("*piNum + iNum1 = %d\n", *piNum + iNum1);
return 0;
}
number = 7
#include <stdio.h> piNum points to iNum whereby the value is = 7
int main() Address of piNum : 1245060 Contents of piNum : 1245064
{ Address of iNum : 1245064
int iNum;
int *piNum;
Dereferencing pointer, *piNum = 15
int iNum1=5;
iNum = 20
iNum = 7; *piNum = 20
*piNum + iNum1 = 25
printf("number = %d\n", iNum);
piNum = &iNum;
printf(piNum points to iNum whereby the value is = %d\n",*piNum);
printf("Address of piNum : %d Contents of piNum : %d\n", &piNum, piNum);
printf("Address of iNum : %d\n\n", &iNum);
*piNum = 15;
printf("Dereferencing pointer, *piNum = %d\n", *piNum);
iNum = iNum + iNum1;
printf(iNum = %d\n, iNum);
printf("*piNum = %d\n", *piNum);
printf("*piNum + iNum1 = %d\n", *piNum + iNum1);
return 0;
}
number = 7
#include <stdio.h> piNum points to iNum whereby the value is = 7
int main() Address of piNum : 1245060 Contents of piNum : 1245064
{ Address of iNum : 1245064
int iNum;
int *piNum;
Dereferencing pointer, *piNum = 15
int iNum1=5;
iNum = 20
iNum = 7; *piNum = 20
*piNum + iNum1 = 25
printf("number = %d\n", iNum);
piNum = &iNum;
printf(piNum points to iNum whereby the value is = %d\n",*piNum);
printf("Address of piNum : %d Contents of piNum : %d\n", &piNum, piNum);
printf("Address of iNum : %d\n\n", &iNum);
*piNum = 15;
printf("Dereferencing pointer, *piNum = %d\n", *piNum);
iNum = iNum + iNum1;
printf(iNum = %d\n, iNum);
printf("*piNum = %d\n", *piNum);
printf("*piNum + iNum1 = %d\n", *piNum + iNum1);
return 0;
}
number = 7
#include <stdio.h> piNum points to iNum whereby the value is = 7
int main() Address of piNum : 1245060 Contents of piNum : 1245064
{ Address of iNum : 1245064
int iNum;
int *piNum;
Dereferencing pointer, *piNum = 15
int iNum1=5;
iNum = 20
iNum = 7; *piNum = 20
*piNum + iNum1 = 25
printf("number = %d\n", iNum);
piNum = &iNum;
printf(piNum points to iNum whereby the value is = %d\n",*piNum);
printf("Address of piNum : %d Contents of piNum : %d\n", &piNum, piNum);
printf("Address of iNum : %d\n\n", &iNum);
*piNum = 15;
printf("Dereferencing pointer, *piNum = %d\n", *piNum);
iNum = iNum + iNum1;
printf(iNum = %d\n, iNum);
printf("*piNum = %d\n", *piNum);
printf("*piNum + iNum1 = %d\n", *piNum + iNum1);
return 0;
}
number = 7
#include <stdio.h> piNum points to iNum whereby the value is = 7
int main() Address of piNum : 1245060 Contents of piNum : 1245064
{ Address of iNum : 1245064
int iNum;
int *piNum;
Dereferencing pointer, *piNum = 15
int iNum1=5;
iNum = 20
iNum = 7; *piNum = 20
*piNum + iNum1 = 25
printf("number = %d\n", iNum);
piNum = &iNum;
printf(piNum points to iNum whereby the value is = %d\n",*piNum);
printf("Address of piNum : %d Contents of piNum : %d\n", &piNum, piNum);
printf("Address of iNum : %d\n\n", &iNum);
*piNum = 15;
printf("Dereferencing pointer, *piNum = %d\n", *piNum);
iNum = iNum + iNum1;
printf(iNum = %d\n, iNum);
printf("*piNum = %d\n", *piNum);
printf("*piNum + iNum1 = %d\n", *piNum + iNum1);
return 0;
}
number = 7
#include <stdio.h> piNum points to iNum whereby the value is = 7
int main() Address of piNum : 1245060 Contents of piNum : 1245064
{ Address of iNum : 1245064
int iNum;
int *piNum;
Dereferencing pointer, *piNum = 15
int iNum1=5;
iNum = 20
iNum = 7; *piNum = 20
*piNum + iNum1 = 25
printf("number = %d\n", iNum);
piNum = &iNum;
printf(piNum points to iNum whereby the value is = %d\n",*piNum);
printf("Address of piNum : %d Contents of piNum : %d\n", &piNum, piNum);
printf("Address of iNum : %d\n\n", &iNum);
*piNum = 15;
printf("Dereferencing pointer, *piNum = %d\n", *piNum);
iNum = iNum + iNum1;
printf(iNum = %d\n, iNum);
printf("*piNum = %d\n", *piNum);
printf("*piNum + iNum1 = %d\n", *piNum + iNum1);
return 0;
}
number = 7
#include <stdio.h> piNum points to iNum whereby the value is = 7
int main() Address of piNum : 1245060 Contents of piNum : 1245064
{ Address of iNum : 1245064
int iNum;
int *piNum;
Dereferencing pointer, *piNum = 15
int iNum1=5;
iNum = 20
iNum = 7; *piNum = 20
*piNum + iNum1 = 25
printf("number = %d\n", iNum);
piNum = &iNum;
printf(piNum points to iNum whereby the value is = %d\n",*piNum);
printf("Address of piNum : %d Contents of piNum : %d\n", &piNum, piNum);
printf("Address of iNum : %d\n\n", &iNum);
*piNum = 15;
printf("Dereferencing pointer, *piNum = %d\n", *piNum);
iNum = iNum + iNum1;
printf(iNum = %d\n, iNum);
printf("*piNum = %d\n", *piNum);
printf("*piNum + iNum1 = %d\n", *piNum + iNum1);
return 0;
}
number = 7
#include <stdio.h> piNum points to iNum whereby the value is = 7
int main() Address of piNum : 1245060 Contents of piNum : 1245064
{ Address of iNum : 1245064
int iNum;
int *piNum;
Dereferencing pointer, *piNum = 15
int iNum1=5;
iNum = 20
iNum = 7; *piNum = 20
*piNum + iNum1 = 25
printf("number = %d\n", iNum);
piNum = &iNum;
printf(piNum points to iNum whereby the value is = %d\n",*piNum);
printf("Address of piNum : %d Contents of piNum : %d\n", &piNum, piNum);
printf("Address of iNum : %d\n\n", &iNum);
*piNum = 15;
printf("Dereferencing pointer, *piNum = %d\n", *piNum);
iNum = iNum + iNum1;
printf(iNum = %d\n, iNum);
printf("*piNum = %d\n", *piNum);
printf("*piNum + iNum1 = %d\n", *piNum + iNum1);
return 0;
}
Call by reference with pointer arguments
Passes address of argument using & operator
Allows you to change actual location in memory
Arrays are not passed with & because the array name is
already a pointer
* operator
Used as alias or nickname for variable inside of function
void fnFun1 (int *piNumber)
{
*piNumber = 2 * (*piNumber);
}
*piNumber used as nickname for the variable passed
#include <stdio.h>
void fnFun1(int, int*, int*); //function prototype
int main(void)
{
int iA=5,iB=10,iC=15;
printf("Before fun 1\n);
printf(iA = %d iB = %d iC = %d,iA,iB,iC);
fnFun1(iA, &iB, &iC); //function call
printf(\n\nAfter fun 1\n);
printf(iA = %d iB = %d iC = %d,iA,iB,iC);
return 0;
}
void fnFun1(int iAA,int *iBB, int *iCC)//function definition
{
++iAA;
--*iBB;
++*iCC;
printf("\n\nInside fnFun1\n);
printf(iAA = %d iBB = %d iCC= %d,iAA,*iBB,*iCC);
} 18
#include <stdio.h>
void fnFun1(int, int*, int*); //function prototype
int main(void)
{
int iA=5,iB=10,iC=15;
printf("Before fun 1\n);
printf(iA = %d iB = %d iC = %d,iA,iB,iC);
fnFun1(iA, &iB, &iC); //function call
printf(\n\nAfter fun 1\n);
printf(iA = %d iB = %d iC = %d,iA,iB,iC);
return 0;
}
void fnFun1(int iAA,int *iBB, int *iCC)//function definition
{
++iAA;
Functions that return more than one value
--*iBB; i.e. arguments are passed by reference
++*iCC;
printf("\n\nInside fnFun1\n);
printf(iAA = %d iBB = %d iCC= %d,iAA,*iBB,*iCC);
} 19
#include <stdio.h>
void fnFun1(int, int*, int*); //function prototype
int main(void)
{
int iA=5,iB=10,iC=15;
printf("Before fun 1\n);
printf(iA = %d iB = %d iC = %d,iA,iB,iC);
fnFun1(iA, &iB, &iC); //function call OUTPUT
printf(\n\nAfter fun 1\n);
printf(iA = %d iB = %d iC = %d,iA,iB,iC);Output
return 0; Before fnFun1
} iA = 5 iB = 10 iC = 15
void fnFun1(int iAA,int *iBB, int *iCC)//function definition
{ Inside fnFun1
++iAA; iAA = 6 iBB = 9 iCC = 16
--*iBB;
++*iCC; After fnFun1
printf("\n\nInside fnFun1\n); iA = 5 iB = 9 iC = 16
printf(iAA = %d iBB = %d iCC= %d,iAA,*iBB,*iCC);
} 20
#include <stdio.h> void fnMultiplyArray (int aiX[ ], int
iSizeX)
int iArraySize = 5; {
int iCounter;
int fnSumArray (int aiX[], int iSizeX); for (iCounter = 0; iCounter < iSizeX;
void fnPrintArray(int aiX[], int iSizeX); iCounter++)
void fnPrintArray(inaiX[], int iSizeX); aiX[iCounter] = aiX[iCounter] * 10;
}
const pointers
Point to a constant memory location
Must be initialized when defined
int *const piMyPtr = &iX;
Type int *const constant pointer to an int
const int *piMyPtr = &iX;
Regular pointer to a const int
const int *const piPtr = &iX;
const pointer to a const int
iX can be changed, but not *piPtr
Arithmetic operations can be performed on pointers
Increment/decrement pointer (++ or --)
Add an integer to a pointer( + or += , - or -=)
Pointers may be subtracted from each other
Operations meaningless unless performed on an array
5 element int array on machine with 4 byte ints
piVPtr points to first element aiV[0]
at location 3000 (piVPtr = 3000)
piVPtr += 2; sets piVPtr to 3008
piVPtr points to aiV[2] (incremented by 2), but the machine
has 4 byte ints, so it points to address 3008
location
3000 3004 3008 3012 3016
piVPtr= aiV;
printf("Address of piVPtr : %d \nContents of piVPtr : %d\n", &piVPtr, piVPtr);
printf("Address of aiV[0] : %d\n", &aiV);
piVPtr +=2;
printf("Address of piVPtr + 2: %d\n", piVPtr);
piVPtr +=2;
printf("Address of piVPtr + 4: %d\n", piVPtr);
piVPtr2=&aiV[2];
piVPtr=&aiV[0];
iTemp=piVPtr2-piVPtr;
printf("Contents of iTemp : %d\n", iTemp);
return 0;
}
#include <stdio.h>
int main()
{
int *piVPtr, *piVPtr2;
int aiV[5] = {10,20,30,40,50}, iTemp;
piVPtr= aiV;
printf("Address of piVPtr : %d \nContents of piVPtr : %d\n", &piVPtr, piVPtr);
printf("Address of aiV[0] : %d\n", &aiV);
piVPtr +=2;
printf("Address of piVPtr + 2: %d\n", piVPtr);
piVPtr +=2;
Address of piVPtr : 1245064
printf("Address of piVPtr + 4: %d\n", piVPtr);
Contents of piVPtr : 1245020
piVPtr2=&aiV[2];
Address of aiV[0] : 1245020
piVPtr=&aiV[0]; Address of piVPtr + 2: 1245028
iTemp=piVPtr2-piVPtr; Address of piVPtr + 4: 1245036
printf("Contents of iTemp : %d\n",Contents
iTemp); of iTemp : 2
return 0;
}
Arrays and pointers are closely related
Array name like a constant pointer
Pointers can do array subscripting operations
piBPtr = aiB;
The array name (aiB) is actually the address of first element of the
array aiB[5]
piBPtr = &aiB[0];
Explicitly assigns piBPtr to the address of first element of aiB
Element aiB[3]
Can be accessed by *(piBPtr + 3)
Where * is the offset. Called pointer/offset notation
piBPtr = aiB;
for(iIndex=0;iIndex<10;iIndex++)
printf(aiB[%d] = %d\n", iIndex, *(piBPtr+iIndex));
return 0;
}
Address of piBPtr : 1245064
Contents of piBPtr : 1245016
Address of aiB : 1245016
#include <stdio.h> Contents of aiB[0]:10 10 10
int main()
{ piBPtr points to aiB[0] = 10
int *piBPtr ;int iIndex;
int aiB[10]={10,20,30,40,50}; I am accessing element aiB[3]!!
Let see how many ways I can do it
piBPtr = aiB; aiB[3] = 40
*(piBPtr + 3) = 40
printf("Address of piBPtr : %d \nContents of =
*(aiB + 3) piBPtr
40 : %d\n", &piBPtr,
piBPtr);
piBPtr[3]
printf("Address of aiB : %d \nContents = 40
of aiB[0]:%d %d %d\n", &aiB,
aiB[0], *piBPtr, *aiB);
printf(piBPtr points to aiB[0] = %d\n",
aiB[0]*piBPtr);
= 10
aiB[1] = 20
printf("\nI am accessing element aiB[3]!!\nLet
aiB[2] = 30 see how many ways I can do
it\n");
aiB[3] = 40
printf(aiB[3] = %d\n", aiB[3]); aiB[4] = 50
aiB[5]
printf("*(piBPtr + 3) = %d\n", *(piBPtr + = 0
3));
printf("*(aiB + 3) = %d\n", *(aiB +aiB[6]
3)); = 0
printf(piBPtr[3] = %d\n\n", piBPtr[3]);
aiB[7] = 0
aiB[8] = 0
for(iIndex=0;iIndex<10;iIndex++) aiB[9] = 0
printf(aiB[%d] = %d\n", iIndex, *(piBPtr+iIndex));
return 0;
}
Arrays can contain pointers
For example: an array of strings
char *acSuit[4] = {Hearts,Diamonds,Clubs,Spades};
Strings are pointers to the first character
char * each element of acSuit is a pointer to a char
The strings are not actually stored in the array acSuit, only
pointers to the strings are stored
acSuit[0] H e a r t s \0
acSuit[1] D i a m o n d s \0
acSuit[2] C l u b s \0
acSuit[3] S p a d e s \0
int main()
{
char *acStudentName[N]; int iIndex;
for(iIndex=0;iIndex<5;iIndex++)
{
printf("Enter student[%d] name : ", iIndex);
return 0;
}
#include <stdio.h>
#define N 5