Docsity

Structured programming c language, Schemes and Mind Maps of Programming Languages

Toggle sidebar
bg1
UNIT-IV: POINTERS:
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 the ``contents 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,
ASST.PROFESSOR, SLNEC
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28

Partial preview of the text

Download Structured programming c language and more Schemes and Mind Maps Programming Languages in PDF only on Docsity!

UNIT-IV: POINTERS :

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.

  • (asterisk sign) indirection operator accesses the value at the address. Dereferencing of Pointer Once a pointer has been assigned the address of a variable. To access the value of variable, pointer is dereferenced, using the indirection operator . int a,p; a = 10; p = &a; printf("%d",p); //this will print the value of a. printf("%d",&a); //this will also print the value of a. printf("%u",&a); //this will print the address of a. printf("%u",p); //this will also print the address of a. printf("%u",&p); //this will also print the address of p. EXAMPLE: #include int main() { int ptr, q; q = 50; / address of q is assigned to ptr */ ptr = &q; STRUCTURED PROGRAMMING USING C MRS.UTHAYA CHANTHIRIGA, 4

/* 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

ELEMENT VALUE ADDRESS

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:

  1. Incrementing Pointer is generally used in array because we have contiguous memory in array and we know the contents of next memory location.
  2. Incrementing Pointer Variable Depends Upon data type of the Pointer variable. The formula of incrementing pointer is given below: new_address= current_address + i * size_of(data type) Three rules should be used to increment pointer STRUCTURED PROGRAMMING USING C MRS.UTHAYA CHANTHIRIGA, 14

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