Showing posts with label Pointers. Show all posts
Showing posts with label Pointers. Show all posts

Tuesday, May 2, 2017

Use of Pointer to add two numbers.


#include <stdio.h>
int main()
{
   int num1, num2, *ptr_num1, *ptr_num2, total;

   printf("Enter two numbers:");
   scanf("%d%d", &num1, &num2);
   
//Initialize pointers with memory addresses of num1 and num2

   ptr_num1 = &num1;
   ptr_num2 = &num2;

   total = *ptr_num1 + *ptr_num2;

   printf("Addition of two numbers = %d.",total);
   return 0;
}

Output of program

Enter two numbers:10 20
Addition of two numbers = 30.


Friday, April 14, 2017

Accessing structure using Pointer

This program will use members of structure using pointer.

#include <stdio.h>
struct student
{
   int rollno;
   float age;
};

int main()
{
    struct student *ptr_student, student1;
    // Referencing pointer to memory address of student1
ptr_student = &student1;            

    printf("Enter rollno: ");
    scanf("%d",&(*ptr_student).rollno);

    printf("Enter age: ");
    scanf("%f",&(*ptr_student).age);

    printf("Student Details: ");
    printf("%d,%f",(*ptr_student).rollno,(*ptr_student).age);

    return 0;
}

Output of program

Enter rollno: 101
Enter age: 21.5
Student Details: 101,21.50

Thursday, April 13, 2017

String and Pointer

Lets learn how to manipulate string using pointer.

This program will find length of given string using pointer.


#include<stdio.h>
 int main() {
   char str[20], *p;
   int cnt=0;
   
   printf("Enter your string : ");
   gets(str);

   p = &str[0];
   
   while(*p != '\0')
   {
    cnt++;
    p++;
   }
   
   printf("The length of \"%s\" is: %d", str, cnt);
   return 0;
}

Output of program

Enter your string : hello students
The length of "hello students" is: 14


Thursday, March 2, 2017

Array of Pointers

To store many pointer variables, one can use array of pointers.

#include <stdio.h>
int main () {

   int  number[5], i, *ptr[5];

   for(i=0; i<5; i++)
   {
     printf("Enter number[%d]:",i);
     scanf("%d",&number[i]);
   }
   /* Loop to assign the address of integer into pointer array */
   for ( i = 0; i < 5; i++) {
      ptr[i] = &number[i]; 
   }
   printf("Value printed using Pointer array.\n");
   for ( i = 0; i < 5; i++) {
      printf("Value of number[%d] = %d\n", i, *ptr[i] );
   }
   return 0;
}

Output of program:

Enter number[0]:2
Enter number[1]:4
Enter number[2]:6
Enter number[3]:8
Enter number[4]:10
Value printed using Pointer array.
Value of number[0] = 2
Value of number[1] = 4
Value of number[2] = 6
Value of number[3] = 8
Value of number[4] = 10

Wednesday, March 1, 2017

Dynamic memory allocation

Dynamic memory allocation means, programmer can manually handle memory space during runtime of program. Malloc( ) function allocates requested size of bytes and returns a pointer to first byte of allocated memory.

Following program will demonstrate use of malloc() function for allocating memory at run time.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int *ptr;

    //Following statement allocates 2 int size memory space
    ptr = (int*)malloc(2*sizeof(int));  
    
    if(ptr == NULL)                     
    {
        printf("Error during memory allocation.");
        exit(0);
    }

    printf("Enter your number1: ");
    scanf("%d", ptr);
    
    printf("Enter your number2: ");
    scanf("%d", ptr+1);
   
    printf("Your number1 = %d\n", *ptr);
    printf("Your number2 = %d", *(ptr+1));
    
    free(ptr); //allotted memory will be cleared.

    return 0;
}

Output of the program:

Enter your number1: 101
Enter your number2: 202
Your number1 = 101
Your number2 = 202


* * * * *

Saturday, September 24, 2016

Use of pointer to access the Array elements.



Use of pointer to access the Array elements.

#include <stdio.h>

int main ()
{
       int marks[] = {60, 70, 80, 90, 100};
       int i, *p1;
       /* let us use pointer to refer array elements */
       p1 = marks;
       for ( i = 0; i < 5; i++)
       {
              printf("Value stored at marks[%d] = %d\n", i, *p1 );
              p1++;
       }
       return 0;
}



Output of the Program:


Value stored at marks[0] = 60
Value stored at marks[1] = 70
Value stored at marks[2] = 80
Value stored at marks[3] = 90
Value stored at marks[4] = 100

C Program to demonstrate use of null pointer.


C Program to demonstrate use of null pointer.

#include <stdio.h>
int main ()
{
       int *p1=NULL;
      
       printf("Value of *p1 = %d", p1);
             
       return 0;
}


Output of the Program:

Value of *p1 = 0





C program to print Memory Addresses of Variables.

C Program to print Memory Addresses of Variables.

#include<stdio.h>
int main ()
{
  int rollno;
  char name[10];

  printf("Memory Address of rollno variable: %d\n", &rollno );
  printf("Memory Address of name variable: %d\n", &name );

  return 0;
}


Output of the Program:

Memory Address of rollno variable: 2293436
Memory Address of name variable: 2293426

Note: You may get different memory address on your computer.

* * * * *