You are on page 1of 5

I)Displaying using printf double x; printf("-->|%d|<--", 123); printf("-->|%5d|<--", 123); printf("-->|%-5d|<--", 123); printf("-->|%s|<--", "hello"); printf("-->|%9s|<--", "hello"); printf("-->|%-9s|<--",

"hello"); x = -17.89306213; printf("-->|%f|<--", x); printf("-->|%7.2f|<--", x); printf("-->|%e|<--", x); printf("-->|%g|<--", x);

-->|123|<--->| 123|<--->|123 |<--->|hello|<--->| hello|<--->|hello |<--->|-17.893062|<--->| -17.89|<--->|-1.789306e+01|<--->|-17.8931|<--

II)Some Additional Notes and Programs in C for Pointers

1) /* program to show CALL BY REFERENCE using pointers in a function */ #include <stdio.h> void DoubleIt(int *num) /*function name: DoubleIt , parameter is pointer to int*/ { *num*=2; } main() { int number=2; /* change this and accept input from user */ DoubleIt(&number); /*arugument sent is address of variable so reference*/ printf("%d",number); }

2) /* Some more pointer problems, checking greater than and less without functions */
main () { int data[100],i; int* p1; int *p2; for ( i = 0; i <100;i++) { data[i] = i; } p1 = &data [1]; p2 = &data [2]; if (*p1 > *p2) { printf ("\n\n p1 is greater than p2"); } else {

printf ("\n\n p2 is greater than p1"); } } 3) /* Accessing elements of 1 D array integer */ main () { int data[10],i; int* p1; for ( i = 0; i <10;i++) { data[i] = i; } p1 = data; //Assigning base address of an array to pointer for ( i = 0; i <10;i++) //Accessing Array using index { printf ("\n%d",p1[i]); } for (i = 0; i <10;i++) //Access Array using Pointer Arithmetic { printf ("\n%d",*(p1 +i)); } }

4) Pointers like any other data type can be arrayed. This array is called array of pointers and is quite usefull programming method. Array of pointers are declared as shown below data_type *variable_name [array_size]; For example to declare an array of 5 int pointers we will use declaration: int *array [5]; /* Program for array of pointers */ main() { int data[5],i; int *array[5]; for ( i = 0; i <5;i++) { data[i] = i; } //Assigning address of elements of array data to array of pointers. for ( i = 0; i <5;i++) { array[i] = &data[i]; } for ( i = 0; i <5;i++) //Accessing Array value using index

{ printf ("\n%d",data[i]); } for ( i = 0; i <5;i++) //Access Array value using array of pointers { printf ("\n%d",*array[i] ); } } 5) /* Multiple indirection example */ main() { int i = 10; int **p1; int *p2; p2 = &i; p1 = &p2; // Multiple indirection printf (" **p1 = %d And *p2 = %d", **p1,*p2); //Statement will show 10 twice. }

6) Passing arguments that need to be modified using functions In C, the values of arguments are passed to functions. Thus, a function usually only has access to copies of its arguments. If you send pointers as arguments, you can go to the addresses and change the values there. void swap(int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; } Call this function as follows: int i=3, j=5; swap(&i, &j);

(b) Pointers vs. arrays i) Using built-in functions : int a[3][6] //fixed sized array, no pointers Advantage: Easy. Disadvantages: Pre-determined size and must be a rectangle.

But inside memory it looks like the diagram below:

This is re-presented in the following program: ii) Using Dynamic memory allocation and pointer to 1D array /* Program for manipulating 2-D array using pointer to 1D array, output: 0 - 17 */ main() { int i, j, nr=3, nc=6, *x, k=0; x = (int *)malloc(sizeof(int) * nr*nc); /* Allocating 2D Array of 18 elements, each 4 bytes*/ for(i=0; i<nr; i++) { for(j=0; j<nc; j++) { x[i + j*nr] = k; /* stored by columns */ printf(" row=%d col=%d addr=%u value=%d\n",i, j, *(x+i+j*nr), x[i+j*nr]); k++; } } free(x); /* function to release memory previously allocated */ } Advantages: Dynamic memory allocation, flexible shape. Disadvantages: Cumbersome, ugly, prone to errors. ii) Using Dynamic memory allocation for 2 D Array and using double pointer main() { int i, j, nr=3, nc=6, **x, k=0; /* allocate space for the pointers to each row */ x = (int **)malloc(sizeof(int *)*nr); /* allocate space for each row */ for (i=0; i<nr; i++)

{ x[i] = (int *)malloc(sizeof(int)*nc); } /* Enter elements into 2D array */ For (i=0; i<nr; i++) for(j=0; j<nc; j++) x[i][j] = k++; /* using fixed size array concept /* Display elements into 2D array */ for(i=0; i<nr; i++) for(j=0; j<nc; j++) printf("[%d]",x[i][j]); /* free the memory allocated earlier */ for(i=0; i<nr; i++) free(x[i]); free(x); getch(); return 0; }

You might also like