Home
Diploma Practice C Online People
Certificate Contact Us

Assignments on using pointer variables


1.  Define an integer pointer variable with name 'pi'. Define this pointer
    as global variable, not local variable. Now print the following:

    - Size of pointer variable using sizeof operator
    - Print the address of this pointer variable
    - Print the value present in this pointer variable (which is supposed to 
      be an address).

    Could you guess, what will be the value, present in this pointer variable ?

2.  Define an integer pointer variable. Assign any one number into this
    pointer variable. For example put 100 into this pointer variable.
    You need to typecast 100 as a pointer to avoid the warning.
    Next print the value in the pointer variable. It should print the value
    you have assigned. Now store some other number into this pointer and
    again print it.

3.  Define three pointer pointer variables pi, ps and pc. One is integer 
    pointer, second is short pointer and last is char pointer. Print the 
    size of each pointer using sizeof operator. What do you expect the size 
    of these pointers?

    Now assign all these three pointers with a value of 100. Use typecast
    to avoid warning. Now print all the three pointers. Next increment or
    add one (pi++; or pi = pi + 1;) to all three pointer variables.
    After incrementing, print the values of all three pointers. You should
    be able to expect the values it is going to print.

4.  Define an integer pointer variable.  Ask user to enter some number or
    address (Please note that address is also a number only). Now use
    scanf() function and read the number into this pointer variable.
    Finally print this pointer varible.

    Note: You are always doing this with integer variables, that is reading
    a number from the user. Now you are doing the same thing with pointer
    variable. That is you are reading an address (number) from the user
    into this pointer variable.

5.  Define one integer variable and one pointer to integer variable.
    Store the address of integer variable into pointer variable.

    - Assign a value of 100 to integer variable.
    - Print the content of integer variable, using pointer variable.
    - Assign a new value (ex: 200) into integer variable by using pointer.
    - Print the new value in integer, using integer variable.
    - Read a new value into integer from user, using scanf() function 
    - Print the new value in integer using pointer variable.
    - Read a new value into integer, through pointer variable using scanf() 
      function
    - Print the new value in integer using integer variable 

6   Define an array of 5 integers and one integer pointer.
    Store the starting address of array into pointer variable.

    - Using scanf() read 5 numbers into the array, from the user
    - Print these 5 numbers by using, pointer variable
    - Again read 5 numbers from the user, now through the pointer variable
    - Print the values in the array

7.  Define a structure variable of type 'struct Student', and structure pointer 
    variable. Store the address of structure variable into the pointer variable.

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

    - Using scanf() function read into the structure variable
    - Print the content of the structure variable, using pointer variable
    - Using scanf() function read into the structure, through pointer variable
    - Print the content of structure variable using structure variable itself

8.  Define an integer variable, one integer pointer variable, one short
    pointer and one char pointer variables. Assign value of 0x12345678 to
    the integer variable. Initialize all three pointers with the address of
    the integer variable. Use type casting while assigning integer address to
    short and char pointers. 

    Now using three pointer variables, print the values pointed by these
    pointers. Expect the values that are going to print.
    Increment the short pointer and print again.
    Increment the char pointer and print again.
    Increment the char pointer and print again.
    Increment the char pointer and print again.

9.  Define an integer pointer variable. Using malloc() function, allocate
    4 bytes of memory and store the allocated address in the pointer variable.
    Now this pointer is pointing to the dynamically allocated integer.

    Read a value from the user into this allocated memory using scanf().
    Print the value present in the allocated memory.

    Note: 
      This assignment is similar to assignment 5. In assignment 5,
      pointer is holding the address of integer variable. This integer variable
      is statically allocated. In this assignment, pointer is pointing to 
      dynamically allocated variable. 


10. Define an integer pointer variable. Using malloc() function allocate
    20 bytes of memory (to hold 5 integers). Store the allocated address in
    the pointer variable. So now this pointer varialbe is pointing to an array
    of 5 integers.

    Read 5 integers from the user and fill the allocated array.
    Print the values in the allocated array.

    Note:
      This assignment is similar to assignment 6. In assignment 6, pointer
      is pointing to statically allocated array. Here pointer is pointing
      to dynamically allocated array.

11. Define a pointer to structure of type struct Student. Look at the
    assignment 7 above, for 'struct Student' type. Using malloc() function
    allocate memory for this structure variable and store the address in the
    pointer.

    - Read student information into this dynamically allocated structure 
      from the user
    - Print the content of dynamically allocated structure variable 

    Note:
      This assignment is similar to assignment 7. There pointer is pointing
      to the statically allocated structure variable. Statically allocated
      variable is already existing and it will have its own name. And by
      putting its address in pointer, we are making pointer to point to already
      existing variable. So pointer is just using already existing variable.
      In this way pointer is not very useful.

      Now in this assignment, pointer is pointing to dynamically allocated
      variable. This variable is allocated just now at run time. One can
      access this dynamically allocated variable only through pointer.
      Dynamically allocated variables do not have any name associated with
      them. So these can be accessed only through the pointer variables.
      This is the real use of pointer variables.

12. Define a pointer to structure of type struct Student. Using malloc() 
    function allocate memory for 4 student structures. Store this address in 
    the poiner. Now pointer is pointing to an array of 4 student structures.
    Now use this pointer, just like an array of 4 student structures.

    - Read 4 students information into this array of 4 structures from user. 
    - Print the 4 students information entered by the user




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