
































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

































Introduction, Pointer Arithmetic, Pointers for Inter-Function Communication, Pointers toPointers, Arrays and Pointers- Array of Pointers, Pointer to Array, Pointers to void, Pointers toFunctions,CommandLineArguments.DynamicMemoryAllocationFunctions,ProgrammingAp plications. POINTERS: ļ· A pointer is a variable which contains the address in memory of another variable. We can have a pointer to any variable type. The unary or monadic operator & gives the address of a variable''. The indirection or dereference operator * gives thecontents of an object pointed to by a pointer''. **Syntax data-type *variablename; Example: int *x; float f; char y; STRUCTURED PROGRAMMING USING C MRS.UTHAYA CHANTHIRIGA, 1
Uses of a Pointer ļ· Allows to keep track of address of memory locations. ļ· Allows to easily manipulate data in different memory locations. ļ· Allows dynamic allocation of memory ļ· Allows accessing array elements, passing arrays and strings to functions, creating data structures such as linked lists, trees, graphs and so on. Example of a Pointer ļ· Consider the following declaration int num=5; ļ· The compiler will automatically assign memory for this data item. The data item can be accessed if we know the location (that is the address) of the first memory cell. Initializing a Pointer Variable STRUCTURED PROGRAMMING USING C MRS.UTHAYA CHANTHIRIGA, 2
Symbols used in pointer Symbol Name Description & (ampersand sign) address of operator determines the address of a variable.
/* display q's value using ptr variable */ printf("%d", ptr); return 0; } Output 50 #include int main() { int var =10; int p; p= &var; printf ( "\n Address of var is: %u", &var); printf ( "\n Address of var is: %u", p); printf ( "\n Address of pointer p is: %u", &p); / Note I have used %u for p's value as it should be an address/ printf( "\n Value of pointer p is: %u", p); printf ( "\n Value of var is: %d", var); printf ( "\n Value of var is: %d", *p); printf ( "\n Value of var is: %d", *( &var)); } Output: Address of var is: 00XBBA Address of var is: 00XBBA Address of pointer p is: 77221111 Value of pointer p is: 00XBBA STRUCTURED PROGRAMMING USING C MRS.UTHAYA CHANTHIRIGA, 5
**syntax of pointer to pointer int p2; pointer to pointer example Let's see an example where one pointer points to the address of another pointer. Example: STRUCTURED PROGRAMMING USING C MRS.UTHAYA CHANTHIRIGA, 7
# include #include void main(){ int number=50; int *p;//pointer to int int **p2;//pointer to pointer clrscr(); p=&number;//stores the address of number variable p2=&p; printf("Address of number variable is %x \n",&number); printf("Address of p variable is %x \n",p); printf("Value of p variable is %d \n",p); printf("Address of p2 variable is %x \n",p2); printf("Value of p2 variable is %d \n",p); getch(); } Output Address of number variable is fff Address of p variable is fff Value of *p variable is 50 Address of p2 variable is fff Value of **p variable is 50 POINTERS AND ARRAYS ļ· Array is a collection of similar data type elements stored under common name. When we declare an array the consecutive memory location are located to the array of elements. The elements of an array can be efficiently accessed by using pointers. STRUCTURED PROGRAMMING USING C MRS.UTHAYA CHANTHIRIGA, 8
p+2=&x[2]=1004; p+3=&x[3]=1006; p+4=&x[4]=1008; Example: Program to compute sum of elements stored in an array #include #include void main() { int *p, sum, i; int x[5]={1,2,3,4,5}; i=0; p=x; printf(ā Enter Value Address\n\nā); while(i<5) { printf(āx[%d] %d %u\nā, i, p, p); sum=sum+p; i++;p++; } printf(ā\n Sum=%d\nā, sum); printf(ā\n &x[0]=%u\nā, &x[0]); printf(ā\n p=%u\nā, p); } OUTPUT STRUCTURED PROGRAMMING USING C MRS.UTHAYA CHANTHIRIGA, 10
x[0] 1 200 x[1] 2 202 x[2] 3 204 x[3] 4 206 x[4] 5 208 sum = 15 &x[0] = 200 p = 210 Pointer to Array We can use a pointer to point to an Array, and then we can use that pointer to access the array. Lets have an example, int i; int a[5] = {1, 2, 3, 4, 5}; int p = a; _// same as intp = &a[0]_ for (i=0; i<5; i++) { printf("%d", *p); p++; } In the above program, the pointer *p will print all the values stored in the array one by one. We can also use the Base address ( a in above case) to act as pointer and print all the values. STRUCTURED PROGRAMMING USING C MRS.UTHAYA CHANTHIRIGA, 11
Since, the addresses of both are the same, the values of arr and &arr[0] are also the same. *arr[0] is equivalent to arr (value of an address of the pointer) Similarly, &arr[1] is equivalent to (arr + 1) AND, arr[1] is equivalent to *(arr + 1). &arr[2] is equivalent to (arr + 2) AND, arr[2] is equivalent to *(arr + 2). &arr[3] is equivalent to (arr + 3) AND, arr[3] is equivalent to *(arr + 3). . . &arr[i] is equivalent to (arr + i) AND, arr[i] is equivalent to *(arr + i). Example: Program to find the sum of six numbers with arrays and pointers #include int main() { int i, classes[6],sum = 0; printf("Enter 6 numbers:\n"); for(i = 0; i < 6; ++i) { // (classes + i) is equivalent to &classes[i] scanf("%d",(classes + i)); // *(classes + i) is equivalent to classes[i] sum += *(classes + i); } printf("Sum = %d", sum); return 0; } Output STRUCTURED PROGRAMMING USING C MRS.UTHAYA CHANTHIRIGA, 13
Enter 6 numbers: 2 3 4 5 3 4 Sum = 21 Pointer Arithmetic and Arrays Pointer holds address of a value, so there can be arithmetic operations on the pointer variable. There are four arithmetic operators that can be used on pointers: o Increment(++) o Decrement(--) o Addition(+) o Subtraction(-) Increment pointer:
Like increment, we can decrement a pointer variable. formula of decrementing pointer new_address= current_address - i * size_of(data type) Example: #include void main(){ int number=50; int *p;//pointer to int p=&number;//stores the address of number variable printf("Address of p variable is %u \n",p); p=p-1; printf("After decrement: Address of p variable is %u \n",p); } Output Address of p variable is 3214864300 After decrement: Address of p variable is 3214864296 Addition(+) We can add a value to the pointer variable. formula of adding value to pointer new_address= current_address + (number * size_of(data type)) Note: 32 bit For 32 bit int variable, it will add 2 * number. 64 bit STRUCTURED PROGRAMMING USING C MRS.UTHAYA CHANTHIRIGA, 16
For 64 bit int variable, it will add 4 * number. Example: #include void main(){ int number=50; int *p;//pointer to int p=&number;//stores the address of number variable printf("Address of p variable is %u \n",p); p=p+3; //adding 3 to pointer variable printf("After adding 3: Address of p variable is %u \n",p); } Output Address of p variable is 3214864300 After adding 3: Address of p variable is 3214864312 Subtraction (-) Like pointer addition, we can subtract a value from the pointer variable. The formula of subtracting value from pointer variable. new_address= current_address - (number * size_of(data type)) Example: #include void main(){ int number=50; int *p;//pointer to int p=&number;//stores the address of number variable STRUCTURED PROGRAMMING USING C MRS.UTHAYA CHANTHIRIGA, 17
3) Formal parameters as an unsized array ā void myFunction(int param[10]) { . . . } Example1: pass an entire array to a function argument #include /* function declaration / double getAverage(int arr[], int size); int main () { / an int array with 5 elements / int balance[5] = {1000, 2, 3, 17, 50}; double avg; / pass pointer to the array as an argument / avg = getAverage( balance, 5 ) ; / output the returned value */ printf( "Average value is: %f ", avg ); return 0; } double getAverage(int arr[], int size) { int i; double avg; double sum = 0; for (i = 0; i < size; ++i) { sum += arr[i]; STRUCTURED PROGRAMMING USING C MRS.UTHAYA CHANTHIRIGA, 19
avg = sum / size; return avg; } Output Average value is: 214. Example2: pass an entire array to a function argument #include myfuncn( int *var1, int var2) { for(int x=0; x