C LANGUAGE MAKES EASY

C  LANGUAGE MAKES EASY



FUN WITH POINTERS

POINTER: A POINTER IS A VARIABLE THAT CONTAINS THE ADDRESS OF A VARIABLE.

INDIRECTION: THE MOST COMMON FORM OF INDIRECTION IS THE ACT OF MANIPULATING A VALUE THROUGH ITS MEMORY ADDRESS.

* %p REPRESENTS THE FORMAT SPECIFIER FOR POINTERS.

EXAMPLE 1: 

/* FUN WITH POINTERS :)
WRITTEN BY: YESHWANTH CH
DATE: 10-04-2020 */
 
#include<stdio.h>

void main() {

//local declarations
int a;
int b;
int c;

// declaring pointers
int* p;
int* q;
int* r;

//statements
a = 5;
b = 10;
p = &b; // p have address of b and value its pointing 10

q = p; // now q is also pointing to b and value is 10
r = &c; // now r had address of c the value is not declared

p = &a; //now is p had address of a and the value is 5
 
*q = 18; /* dereference or indirection we change the value 10 to 18 through the  memory address */
 
//*p is pointing to the address b that have value 10 now it is 18

*r = *p; //here little bit confused dont worry i will explain
 
/* if we use asterisk to the pointor we are using is called indirection operator, to access the value of the variable that pointed to by pointer we use (*).
It is also dereference because it is also used to dereference a pointer.
NOW *r pointing to the address c but c doesn't have value *r = *p
now c has value 5 why because *p is pointing to the variable value is 5 */
 
*r = *p + *q + *&c; // c has 5 and the value will changed after sum

printf("%d %d %d\n",a,b,c);
 
printf("%d %d %d\n",*p,*q,*r);/* here we are not printing the address we are 
 displaying the value of the variable that pointed by a pointer */

printf("%p %p %p\n",p,q,r);
                                           //displaying the address the pointer pointing too
                                            //we can use (&) like &a,&b,&c}

/* OUTPUT:
5 18 28
5 18 28
0xbfc25e20  0xbfc25e1c  0xbfc25e18 // Address maybe different in your system
*/
 
EXAMPLE 2:
 
/* FUN WITH POINTERS :)
WRITTEN BY: YESHWANTH CH
DATE: 10-04-2020 */
 
#include<stdio.h>
 
void main() {
int a;
int b;
int c;
int* pa = &a;
int* pb = &b;
int* pc = &c;
 
printf("Enter the First Number:");
scanf("%d",pa);
printf("Enter the Second Number:");
scanf("%d",pb);

*pc = *pa + *pb;

printf("\n%d + %d is %d",*pa,*pb,*pc);
}
 
/*OUTPUT:
Enter the First Number:10
Enter the Second Number:20

10 + 20 is 30 */
 
 NULL POINTERS: INITILIZE A NULL POINTER SO THAT IS DOES NOT POINT TO ANYTHING. NULL IS CONSTANT THAT IS DEFINED IN THE STANDARD LIBRARY. IT IS EQUIVALENT TO ZERO FOR A POINTER.
 
* Technically NULL is found in the stddef.h library. Traditionally, it may also be defined in stdio.h.

EXAMPLE 3:
 
/* FUN WITH POINTERS :)
WRITTEN BY: YESHWANTH CH
DATE: 10-04-2020 */
 
#include<stdio.h>
#include<stddef.h>
void main() {
int a = 10;
float c = 12.5;

int* pa = NULL;
float* pc = NULL;
pa = &a;
pc = &c;
 
//indirection
*pa = 11;
*pc = 31.2;
int sum = *pa + *pc;
printf("%d %d\n",*pa,a);
printf("%f %f\n",*pc,c);
printf("%d\n",sum);
printf("%p %p\n",pc,pa);
}
 
 
/* OUTPUT:
11 11
31.200001 31.200001
42
0xbf9af3fc  0xbf9af400
*/
 
 
 
 
 

Comments

Post a Comment

Popular Posts