Home | Lab 1 | Lab 2 | Lab 3 | Lab 4 | Lab 5 | Lab 6 | Lab 7 | Lab 8 | Lab 9 | Lab 10 | Lab 11 | Lab 12

Linked at EntranceNepal.com, i-geek blog & Ekendra's Share World

Object I. To write programs, using pointer, that takes three variables a, b, c and rotates the value stored such that value a goes to b, b to c and c to a.

Object II. To write to a program to find the largest of a given set of numbers using pointer.

Download this full program | Lab 9 | in pdf format

Background Theory:

Pointers: Very powerful, but primitive facility contained in the C language for determining memory address of entered values. However, pointers are variables in themselves.

A variable is an area of memory that has been given a name. Eg: int x; is an area of memory that has been given the name x. The advantage of this scheme is that you can use the name to specify where to store data. Eg: x=lO; is an instruction to store the data value 10 in the area of memory named x. The variable is such a fundamental idea that using it quickly becomes second nature, but there is another way of working with memory.

 The computer access its own memory not by using variable names but by using a memory map with each location of memory uniquely defined by a number,  called the address of that memory location. A pointer is a variable that stores this location of memory. In more fundamental terms, a pointer stores the address of a variable . In more picturesque terms, a pointer points to a variable. A pointer has to be declared just like any other variable - remember a pointer is just a variable that stores an address. Eg: int *p; is a pointer to an integer. Adding an asterisk in front of a variable's name declares it to be a pointer to the declared type. Notice that the asterisk applies only to the single variable name that it is in front of, so: int *p , q; declares a pointer to an int and an int variable, not two pointers.

 Once you have declared a pointer variable you can begin using it like any other

variable, but in practice you also need to know the meaning of two new operators: & and *. The & operator returns the address of a variable. You can remember this easily because & is the 'A'mpersand character and it gets you the 'A'ddress. Eg: int *p , q; declares p, a pointer to int, and q an int and the instruction: p=&q; stores the address of q in p. After this instruction you can think of p as pointing at q. Compare this to: p=q; which attempts to store the value in q in the pointer p - something which has to be considered an error. The second operator * is a little more difficult to understand. If you place * in front of a pointer variable then the result is the value stored in the variable pointed at. That is, p stores the address, or pointer.

Code:

#include <stdio.h>

#include <conio.h> 

void main()

{

            int *ptr1, *ptr2, *ptr3, a, b, c;

            clrscr();

            printf("\nEnter any three integers\n");

            scanf("%d%d%d", &a, &b, &c);

            ptr1= &a;

            ptr2= &b;

            ptr3= &c;

            printf("\nOrder before rotation: %d\t%d\t%d", *ptr1, *ptr2, *ptr3);

            ptr1= &c;

            ptr2= &a;

            ptr3= &b;

            printf("\nRotated values: %9d\t%d\t%d", *ptr1, *ptr2, *ptr3);

            getch();

}

#include<stdio.h>

#include<conio.h>

void main()

{

    int a[50], largest, i, n,*ptr;

    clrscr();

    printf("\nEnter the array size:");

    scanf("%d",&n);

    printf("\nEnter array elements\n");

    for(i=0;i<n;i++)

    scanf("%d",&a[i]);

    ptr=&a;

    largest=*ptr;

    ptr++;

    for(i=1;i<n;i++)

 

    {

if(largest<*ptr)

            largest= *ptr;

            ptr++;

    }

 

    printf("\nThe largest value of series is %d", largest);

    getch();

}

To Download this full program | Lab 9 | in pdf format you must have PDF reader as Adobe Acrobat Reader 5 and above in your computer,

click here to download now.

Search yourself for more such codes

Google

Home | Lab 1 | Lab 2 | Lab 3 | Lab 4 | Lab 5 | Lab 6 | Lab 7 | Lab 8 | Lab 9 | Lab 10 | Lab 11 | Lab 12

Protected under Creative Commons License.
Written by Ekendra Lamsal, www.ekendralamsal.com
for comments email at gmail address:
gearShifts@gmail.com