Tutorialspoint

NULL pointers in C

c

#include <stdio.h>
int main() {
   int *p= NULL; //initialize the pointer as null.
   printf("The value of pointer is %u",p);
   return 0;
}

Compile and Execute C Online

c

#include <stdio.h>

int main()
{
    printf("Hello, World!\n");

    return 0;
}

How to declaring pointer variables in C

c

#include <stdio.h> 
int main() 
{ 
   // A normal integer variable 
   int a = 7; 

   // A pointer variable that holds address of a. 
   int *p = &a; 
 
   // Value stored is value of variable "a" 
   printf("Value of Variable : %d\n", *p); 
   //it will print the address of the variable "a" 
   printf("Address of Variable : %p\n", p); 

   // reassign the value.
   *p = 6;

   printf("Value of the variable is now: %d\n", *p); 

   return 0; 
} 

How to compare pointers in C

c

#include <stdio.h>
int main()
{
   int *p2;
   int *p1;

   p2 = (int *)300;
   p1 = (int *)200;
    
   if(p1 > p2) {
      printf("P1 is greater than p2");
   } else {
      printf("P2 is greater than p1");
   }
return(0);
}

Compile and Execute C Online

c

#include <stdio.h>

int main()
{
    printf("Hello, World!\n");

    return 0;
}

Passing arguments to a function as a reference in C

c

#include <stdio.h> 

void swap(int* a, int* b) 
{ 
   int t= *a; 
   *a= *b; 
   *b = t; 
} 

int main() 
{ 
   int m = 7, n= 6; 
   swap(&m, &n); 
   printf("%d %d\n", m, n); 
   return 0; 
} 

Dynamic Memory Allocation in C

c

#include <stdio.h>
#include <stdlib.h>

int main()
{
   int i, *ptr;

   ptr = (int*) malloc(3 * sizeof(int));
   if(ptr == NULL)                     
   {
      printf("Error! memory not allocated.");
      exit(0);
   }
    
   *(ptr+0)=1;
 
   *(ptr+1)=2;
 
   *(ptr+2)=3;
 
      printf("Elements are:");
    
      for(i = 0; i < 3; i++) {
         printf("%d ", *(ptr + i));
      
      }

      free(ptr);
      return 0;
}

To access array elements in C

c

#include <stdio.h> 
int main() 
{ 
   int a[] = { 60, 70, 20, 40 }; 
   printf("%d\n", *(a + 1)); 
   return 0; 
} 

pointers to character to store a list of strings in C

c

#include <stdio.h>
 
const int MAX = 4;
 
int main () {

   char *names[] = {
      "Zara Ali",
      "Hina Ali",
      "Nuha Ali",
      "Sara Ali"
   };
   
   int i = 0;

   for ( i = 0; i < MAX; i++) {
      printf("Value of names[%d] = %s\n", i, names[i] );
   }
   
   return 0;
}

stored in an array of pointers in C

c

#include <stdio.h>
 
const int MAX = 3;
 
int main () {
   int  var[] = {10, 100, 200};
   int i, *ptr[MAX];
 
   for ( i = 0; i < MAX; i++) {
      ptr[i] = &var[i]; /* assign the address of integer. */
   }
   
   for ( i = 0; i < MAX; i++) {
      printf("Value of var[%d] = %d\n", i, *ptr[i] );
   }
   
   return 0;
}

1 2 3 4 5 6 7 ... 944 Next
Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.