Home
Diploma Practice C Online People
Certificate Contact Us

Solutions for assignments on using pointer variables

as01.c

#include <stdio.h>

int *pi;

int main()
{
  printf("Size of integer pointer variable is    : %d\n", sizeof(pi));
  printf("Address of integer pointer variable is : 0x%x\n", (int)&pi);
  printf("Value in  integer pointer variable is  : %d\n", (int)pi);
}

as02.c

#include <stdio.h>

int main()
{
  int *pi;

  pi = (int *) 100;
  printf("Value in  integer pointer variable is  : %d\n", (int)pi);

  pi = (int *) 12345678;
  printf("Value in  integer pointer variable is  : %d\n", (int)pi);
}

as03.c

#include <stdio.h>

int main()
{
  int   *pi;
  short *ps;
  char  *pc;

  printf("Size of integer pointer variable is    : %d\n", sizeof(pi));
  printf("Size of short pointer variable is      : %d\n", sizeof(ps));
  printf("Size of char pointer variable is       : %d\n", sizeof(pc));

  pi = (int *)   100;
  ps = (short *) 100;
  pc = (char *)  100;

  //printf("Values of pi, ps and pc are: %d, %d, %d\n",pi,ps,pc);
  printf("Values of pi, ps and pc are: %d, %d, %d\n",(int)pi,(int)ps,(int)pc);

  pi++;
  ps++;
  pc++;

  printf("After increment values of pi, ps and pc are: %d, %d, %d\n",
                                                     (int)pi,(int)ps,(int)pc);
}

as04.c

#include <stdio.h>

int main()
{
  int *pi;

  printf("Enter some address to store in a pointer variable : ");
  scanf("%d", &pi);
  printf("Value in  integer pointer variable is  : %d\n", pi);
}

as05.c

#include <stdio.h>

int main()
{
  int ival;
  int *pi;

  pi = &ival;

  //Initializing ival variable directly, and printing through  pointer
  ival = 100;
  printf("Content of int variable through  pointer: %d\n", *pi);

  //Initializing ival variable through pointer, and printing directly 
  *pi = 200;
  printf("Content of int variable through variable: %d\n", ival);

  printf("Enter some integer number to store in int variable: ");
  scanf("%d", &ival);
  printf("Content int variable through  pointer: %d\n", *pi);

  printf("Enter some integer number to store in int variable: ");
  scanf("%d", pi);
  printf("Content int variable through  pointer: %d\n", *pi);

}

as06.c

#include <stdio.h>

int main()
{
  int ii;
  int array[5];
  int *pi;

  pi = array;

  for(ii=0; ii<5; ii++)
  {
    printf("Enter number %d : ", ii+1);
    scanf("%d", &array[ii]);
  }

  printf("Following are the numbers read from you:\n");

  for(ii=0; ii<5; ii++)
  {
    printf("Number %d is %d\n", ii+1, pi[ii]);
  }

  for(ii=0; ii<5; ii++)
  {
    printf("Enter number %d : ", ii+1);
    scanf("%d", &pi[ii]); //method 1
    //scanf("%d", pi+ii);   //method 2
  }

  printf("Following are the numbers read from you:\n");

  for(ii=0; ii<5; ii++)
  {
    printf("Number %d is %d\n", ii+1, pi[ii]);
  }
}

as07.c

#include <stdio.h>

struct Student
{
  char      name[40];
  long long mobile;
  int       yearOfBirth; // Year in which one has born
};

int main()
{
  struct Student st;;
  struct Student *pSt;

  pSt = &st;

  //Reading structure using structure variable
  printf("Enter student's name: "); 
  gets(st.name);
  printf("Enter mobile number : "); 
  scanf("%lld", &st.mobile);
  printf("Enter year of birth : "); 
  scanf("%d", &st.yearOfBirth);
 
  //Printing structure using structure pointer variable 
  printf("Name: %s   mobile: %lld     Year born: %d\n",
                pSt->name, pSt->mobile, pSt->yearOfBirth); 

  //Reading structure using structure pointer variable 
  printf("Enter student's name: "); 
  __fpurge(stdin);
  gets(pSt->name);
  printf("Enter mobile number : "); 
  scanf("%lld", &pSt->mobile);
  printf("Enter year of birth : "); 
  scanf("%d", &pSt->yearOfBirth);
 
  //Printing structure using structure variable 
  printf("Name: %s   mobile: %lld     Year born: %d\n",
                st.name, st.mobile, st.yearOfBirth); 
}

as08.c

#include <stdio.h>

int main()
{
  int ival;
  int   *pi;
  short *ps;
  char  *pc;

  ival = 0x12345678;
  pi = &ival;
  ps = (short *) &ival;
  pc = (char *) &ival;

  printf("*pi, *ps, *pc are            : %x, %x, %x\n", *pi, *ps, *pc);

  ps = ps + 1;
  printf("*ps after ps increment       : %x\n", *ps);

  pc = pc + 1;
  printf("*pc after pc first  increment: %x\n", *pc);
  pc = pc + 1;
  printf("*pc after pc second increment: %x\n", *pc);
  pc++;
  printf("*pc after pc third  increment: %x\n", *pc);
}

as09.c

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

int main()
{
  int *pi;

  //pi = malloc(4);
  pi = malloc(sizeof(int));

  printf("Enter a number to store in dynamic allocated memory: ");
  scanf("%d", pi);
  printf("Value in dynamic memory: %d\n", *pi);
}

as10.c

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

int main()
{
  int ii;
  int *pi;

  //pi = malloc(20);
  pi = malloc(5 * sizeof(int));

  for(ii=0; ii<5; ii++)
  {
    printf("Enter number %d : ", ii+1);
    scanf("%d", &pi[ii]); //method 1
    //scanf("%d", pi+ii);   //method 2
  }

  printf("Following are the numbers read from you:\n");
  for(ii=0; ii<5; ii++)
  {
    printf("Number %d is %d\n", ii+1, pi[ii]);
  }
}

as11.c

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

struct Student
{
  char      name[40];
  long long mobile;
  int       yearOfBirth; // Year in which one has born
};

int main()
{
  struct Student *pSt;

  pSt = malloc(sizeof(struct Student));

  printf("Enter student's name: "); 
  gets(pSt->name);
  printf("Enter mobile number : "); 
  scanf("%lld", &pSt->mobile);
  printf("Enter year of birth : "); 
  scanf("%d", &pSt->yearOfBirth);
 
  //Printing structure using structure pointer variable 
  printf("Name: %s   mobile: %lld     Year born: %d\n",
                pSt->name, pSt->mobile, pSt->yearOfBirth); 
}

as12.c

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

#define MAX_STUDENTS 5

struct Student
{
  char      name[40];
  long long mobile;
  int       yearOfBirth; // Year in which one has born
};

int main()
{
  int ii;
  struct Student *pSt;

  pSt = malloc(MAX_STUDENTS * sizeof(struct Student));

  for(ii=0; ii<MAX_STUDENTS; ii++)
  {
    printf("Enter student %d name: ", ii+1); 
    __fpurge(stdin);
    gets(pSt[ii].name);
    printf("Enter mobile number : "); 
    scanf("%lld", &pSt[ii].mobile);
    printf("Enter year of birth : "); 
    scanf("%d", &pSt[ii].yearOfBirth);
  }
 
  for(ii=0; ii<MAX_STUDENTS; ii++)
  {
    printf("Name: %s   mobile: %lld     Year born: %d\n",
                pSt[ii].name, pSt[ii].mobile, pSt[ii].yearOfBirth); 
  }
}
#endif

as13.c

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

struct Student
{
  char      name[40];
  long long mobile;
  int       yearOfBirth; // Year in which one has born
};

int main()
{
  int ii;
  int studentsCount;
  struct Student *pSt;

  printf("Enter number of students: ");
  scanf("%d", &studentsCount);
  pSt = malloc(studentsCount * sizeof(struct Student));

  for(ii=0; ii<studentsCount; ii++)
  {
    printf("Enter student %d name: ", ii+1); 
    __fpurge(stdin);
    gets(pSt[ii].name);
    printf("Enter mobile number : "); 
    scanf("%lld", &pSt[ii].mobile);
    printf("Enter year of birth : "); 
    scanf("%d", &pSt[ii].yearOfBirth);
  }
 
  for(ii=0; ii<studentsCount; ii++)
  {
    printf("Name: %s   mobile: %lld     Year born: %d\n",
                pSt[ii].name, pSt[ii].mobile, pSt[ii].yearOfBirth); 
  }
}


#401, Sai Sushma Homes, 23/A, S.R. Nagar Main Road, Hyderabad-500038, India.
Ph: +91-40-48508764, 7702028989, depik.help@gmail.com