Professional Documents
Culture Documents
Ekt120 Week11 Pointers 25112019
Ekt120 Week11 Pointers 25112019
Address of
iNum is value
of piNum
EKT 120:Computer Programming
Similar pointer operation
*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;
}
EKT 120:Computer Programming
Calling Functions by Reference
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
Remember..last time
Do you want to continue?y
Enter character : I
Do you want to continue?y
#include <stdio.h> Enter character : k
#include <string.h> Do you want to continue?n
char fnRead(); Number of vowel : 1
void fnFindCountVC(char, int*, int*); Number of consonant : 2
void fnPrint(int,int);
void fnFindCountVC(char cCh1, int *piVowel, int
int main() *piConsonant)
{ char cCh, cChoice; int iCountV=0, iCountC=0; {
do switch(cCh1)
Functions that “return”
{ cCh = fnRead(); { case 'A':
more than one value i.e.
fnFindCountVC(cCh, &iCountV, &iCountC); case 'a':
arguments are passed by
printf("Do you want to continue? "); case 'E':
reference
scanf("%c", &cChoice); case 'e':
getchar(); case 'I':
}while((cChoice == 'y') ||(cChoice =='Y')); case 'i':
fnPrint(iCountV,iCountC); case 'O':
return 0; case 'o':
} case 'U':
case 'u': *piVowel = *piVowel +1;break;
char fnRead() default: *piConsonant = *piConsonant + 1;
{ char cCh1; }
printf("Enter character : "); }
scanf("%c", &cCh1); void fnPrint(int iVowel, int iConsonant)
getchar(); {
return(cCh1); printf("Number of vowel : %d\n", iVowel);
} printf("Number of consonant : %d\n", iConsonant);
} 11
Remember…last time
int main()
{
#include <stdio.h>
int aiListA [iArraySize] = {0};
int aiListB [iArraySize];
const int iArraySize = 10;
void fnInitializeArray (int aiX[], int iSizeX);
fnPrintArray (aiListA, iArraySize);
void fnFillArray (int aiX[], int iSizeX);
fnInitializeArray (aiListB, iArraySize);
void fnPrintArray (const int aiX[], int iSizeX);
fnPrintArray (aiListB, iArraySize);
int fnSumArray (const int aiX[], int iSizeX);
fnFillArray (aiListA, iArraySize);
int fnIndexLargestElement (const int aiX[], int iSizeX);
fnPrintArray (aiListA, iArraySize);
void fnCopyArray (const int aiX[], int aiY[], int iLength);
fnSumArray (aiListA, iArraySize);
fnCopyArray (aiListA, aiListB, iArraySize);
void fnInitializeArray (int aiX[ ], int iSizeX) fnPrintArray (aiListB, iArraySize);
{ return 0;
int iCounter; }
piVPtr2=&aiV[2];
piVPtr=&aiV[0];
iTemp=piVPtr2-piVPtr;
printf("Contents of iTemp : %d\n", iTemp);
piP=piQ;
printf("Contents of piP : %d piQ : %d\n", piP, piQ);
return 0;}
UniMAP SemII-09/10 EKT 120:Computer Programming
The Relationship between
Pointers and Arrays
Arrays and pointers are closely related
Array name like a constant pointer
Pointers can do array subscripting operations
Define an array aiB[5] and a pointer piBPtr
To set them equal to one another use:
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
Example 2
Let see how many ways I can do it
aiB[3] = 40
*(piBPtr + 3) = 40
*(aiB + 3) = 40
piBPtr[3] = 40
aiB[0] = 10
aiB[1] = 20
aiB[2] = 30
aiB[3] = 40
aiB[4] = 50
aiB[5] = 0
#include <stdio.h> aiB[6] = 0
int main() aiB[7] = 0
{ int *piBPtr ;int iIndex; aiB[8] = 0
aiB[9] = 0
int aiB[10]={10,20,30,40,50};
piBPtr = aiB;
printf("Address of piBPtr : %d Contents of piBPtr : %d\n", &piBPtr, piBPtr);
printf("Address of aiB : %d Contents of aiB[0]:%d %d %d\n", &aiB, aiB[0], *piBPtr, *aiB);
printf(“piBPtr points to aiB[0] = %d\n", *piBPtr);
printf("\nI am accessing element aiB[3]!!\nLet see how many ways I can do it\n");
printf(“aiB[3] = %d\n", aiB[3]);
printf("*(piBPtr + 3) = %d\n", *(piBPtr + 3));
printf("*(aiB + 3) = %d\n", *(aiB + 3));
printf(“piBPtr[3] = %d\n\n", piBPtr[3]);
for(iIndex=0;iIndex<10;iIndex++)
printf(“aiB[%d] = %d\n", iIndex, *(piBPtr+iIndex));
return 0;
} EKT 120:Computer Programming
Arrays of Pointers
Arrays can contain pointers
For example: an array of strings
char *acSuit[4] = {“Hearts”,“Diamonds”,“Clubs”,“Spades”};
acSuit[1] ’D’ ’i’ ’a’ ’m’ ’o’ ’n’ ’d’ ’s’ ’\0’