Total Pageviews

Friday, 25 May 2012

JNTU Anantapur MBA/MCA PreviousQuestion Papers

For MBA I semester Supply Previous Papers Click Here 


For MBA  II Sem Regular Paper Click Here 

For MBA  II Sem Supple Paper Click Here 



For MBA  III Sem Regular Paper Click Here 

MCA 

For MCA  I Sem Regularv  Paper Click Here 

For MCA  I Sem Supply  Paper Click Here  

For MCA  II Sem Supply  Paper Click Here   

For MCA  II Sem Supply  Paper 2011 Click Here    


For MCA  III Sem Supply  Paper Click Here 

For MCA  V Sem   Paper Click Here 

  

 

Saturday, 5 May 2012

Pointers and Arrays


Pointers and Arrays
Pointers and arrays are very closely linked in C.

Hint: think of array elements arranged in consecutive memory locations.
Consider the following:
           
               int a[10], x;
               int *pa;

               pa = &a[0];  /* pa pointer to address of a[0] */

               x = *pa;    /* x = contents of pa (a[0] in this
                              case) */



                        0   1 …………                     9


 
                    a

                        pa    ++pa                                pa+i
Fig. 9.3 Arrays and Pointers

To get somewhere in the array (Fig. 9.3) using a pointer we could do:
pa + i º a[i]

WARNING: There is no bound checking of arrays and pointers so you can easily go beyond array memory and overwrite other things.

C however is much more subtle in its link between arrays and pointers.


For example we can just type
pa = a;
instead of pa = &a[0]
and
a[i] can be written as *(a + i).
i.e. &a[i] º a + i.

We also express pointer addressing like this:
pa[i] º *(pa + i).

However pointers and arrays are different:
  • A pointer is a variable. We can do
    pa = a and pa++.
  • An Array is not a variable. a = pa and a++ ARE ILLEGAL.

This stuff is very important. Make sure you understand it. We will see a lot more of this.

We can now understand how arrays are passed to functions.
When an array is passed to a function what is actually passed is its initial elements location in memory.

So: strlen(s) º strlen(&s[0])

This is why we declare the function:
int strlen(char s[]);

An equivalent declaration is : int strlen(char *s);
since char s[]
ºchar *s.

strlen() is a standard library function (Chapter 18) that returns the length of a string. Let's look at how we may write a function:


   int strlen(char *s)
                 { char *p = s;
                     while (*p != `\0);
                        p++;
                     return p-s;
                 }

Now lets write a function to copy a string to another string. strcpy() is a standard library function that does this.

   void strcpy(char *s, char *t)
                 {  while ( (*s++ = *t++) != `\0);}

This uses pointers and assignment by value.
Very Neat!! NOTE: Uses of Null statements with while.

Pointer and Functions


Pointer and Functions
Let us now examine the close relationship between pointers and C's other major parts. We will start with functions.

When C passes arguments to functions it passes them by value.

There are many cases when we may want to alter a passed argument in the function and receive the new value back once to function has finished. Other languages do this (e.g. var parameters in PASCAL). C uses pointers explicitly to do this. Other languages mask the fact that pointers also underpin the implementation of this.

The best way to study this is to look at an example where we must be able to receive changed parameters.

Let us try and write a function to swap variables around?
The usual function call:

swap(a, b) WON'T WORK.

Pointers provide the solution: Pass the address of the variables to the functions and access address of function.

Thus our function call in our program would look like this:
swap(&a, &b)

The Code to swap is fairly straightforward:

    void swap(int *px, int *py)

                  { int temp;
                     temp = *px; /* contents of pointer */
                     *px = *py;
                     *py = temp;
                   }

We can return pointer from functions. A common example is when passing back structures. e.g.:

typedef struct {float x,y,z;} COORD;
        main()
         {  COORD p1, *coord_fn();  /* declare fn to                           
                                      return ptr of COORD type */
            ....
          
            p1 = *coord_fn(...);    /* assign contents of
                                        address returned */
            ....
         }

   COORD *coord_fn(...)

                {  COORD p;
                   .....
                   p = ....;    /* assign structure values */
                   return &p;   /* return address of p */
                }

Here we return a pointer whose contents are immediately unwrapped into a variable. We must do this straight away as the variable we pointed to was local to a function that has now finished. This means that the address space is free and can be overwritten. It will not have been overwritten straight after the function ha squit though so this is perfectly safe.

Pointers


Pointers
Pointer are a fundamental part of C. If you cannot use pointers properly then you have basically lost all the power and flexibility that C allows. The secret to C is in its use of pointers.

C uses pointers a lot. Why?:
  • It is the only way to express some computations.
  • It produces compact and efficient code.
  • It provides a very powerful tool.

C uses pointers explicitly with:
  • Arrays,
  • Structures,
  • Functions.
NOTE: Pointers are perhaps the most difficult part of C to understand. C's implementation is slightly different DIFFERENT from other languages.
 
What is a Pointer?
A pointer is a variable which contains the address in memory of another variable. We can have a pointer to any variable type.

The unary or monadic operator & gives the ``address of a variable''.

The indirection or dereference operator * gives the ``contents of an object pointed to by a pointer''.

To declare a pointer to a variable do:
int *pointer;

NOTE: We must associate a pointer to a particular type: You can't assign the address of a short int to a long int, for instance.


Consider the effect of the following code:

       int x = 1, y = 2;
       int *ip;
       ip = &x;
       y = *ip;
       x = ip;
       *ip = 3;

It is worth considering what is going on at the machine level in memory to fully understand how pointer work. Consider Fig. 9.1. Assume for the sake of this discussion that variable x resides at memory location 100, y at 200 and ip at 1000.

Note A pointer is a variable and thus its values need to be stored somewhere. It is the nature of the pointers value that is new.