Chapter: 6 Pointer

5/5 - (1 vote)

6.1    Introduce the pointers, its features and its applications

A pointer is a variable whose value is the address of another variable i.e., the direct

address of the memory location. Similar to any variable or constant, you must declare

a pointer before you can use it to store any variable address.

The general form of a pointer declaration is:

type *var-name;

                Example:

                                int *sa;

double *as;

                Code:

                                using System;

namespace UnsafeCodeApplication

{

    class Program

    {

        static unsafe void Main(string[] args)

        {

            int var = 20;

            int* p = &var;

            Console.WriteLine(“Data is: {0} “, var);

            Console.WriteLine(“Address is: {0}”, (int)p);

            Console.ReadKey();

        }

    }

}

Output:

                Data is: 20

Address is: 99215364

Features of Pointer

  • Pointers save memory space.
  • Execution time with pointers is faster because data are manipulated with the address, that is, direct access to memory location.
  • Memory is accessed efficiently with the pointers. The pointer assigns and releases the memory as well.
  • Pointers are used with data structures. They are useful for representing two-dimensional and multi-dimensional arrays.
  • Pointers are used for file handling.
  • Pointers are used to allocate memory dynamically.1

Application of Pointer

  • To pass arguments by reference
  • For accessing array elements
  • To return multiple values
  • Dynamic memory allocation
  • To implement data structures

6.2    Differentiate between advantages and disadvantages of pointers

                Advantages:

  • Pointers provide direct access to memory
    • Pointers provide a way to return more than one value to the functions
    • Reduces the storage space and complexity of the program
    • Reduces the execution time of the program
    • Provides an alternate way to access array elements
    • Pointers allows us to perform dynamic memory allocation and deallocation.

Disadvantages:

  • Uninitialized pointers might cause segmentation fault.
  • Dynamically allocated block needs to be freed explicitly.  Otherwise, it would lead to memory leak.
  • Pointers are slower than normal variables.
  • If pointers are updated with incorrect values, it might lead to memory corruption.

6.3   Demonstrate the access of data value using pointer

Code:

                                using System;

namespace UnsafeCodeApplication

{

    class Program

    {

        public static void Main()

        {

            unsafe

            // {

                int var = 20;

                int* p = &var;

                Console.WriteLine(“Data is: {0} “, var);

                Console.WriteLine(“Data is: {0} “, p->ToString());

                Console.WriteLine(“Address is: {0} “, (int)p);

            }

            Console.ReadKey();

        }

    }

}

                Output:

                                Data is: 20

Data is: 20

Address is: 77128984

6.4    Illustrate the passing of pointers as parameters to methods

                Code:

                                using System;

namespace UnsafeCodeApplication

{

    class TestPointer

    {

        public unsafe void swap(int* p, int* q)

        {

            int temp = *p;

            *p = *q;

            *q = temp;

        }

        public unsafe static void Main()

        {

            TestPointer p = new TestPointer();

            int var1 = 10;

            int var2 = 20;

            int* x = &var1;

            int* y = &var2;

            Console.WriteLine(“Before Swap: var1:{0}, var2: {1}”, var1, var2);

            p.swap(x, y);

            Console.WriteLine(“After Swap: var1:{0}, var2: {1}”, var1, var2);

            Console.ReadKey();

        }

    }

}

                Output:

                                Before Swap: var1: 10, var2: 20

After Swap: var1: 20, var2: 10

6.5   Demonstrate the access of array elements using a pointer

                Code:

using System;

namespace UnsafeCodeApplication

{

    class TestPointer

    {

        public unsafe static void Main()

        {

            int[] list = { 10, 100, 200 };

            fixed (int* ptr = list)

                /* let us have array address in pointer */

                for (int i = 0; i < 3; i++)

                {

                    Console.WriteLine(“Address of list[{0}]={1}”, i, (int)(ptr + i));

                    Console.WriteLine(“Value of list[{0}]={1}”, i, *(ptr + i));

                }

            Console.ReadKey();

        }

    }

}

                Output:

                                Address of list[0] = 31627168

Value of list[0] = 10

Address of list[1] = 31627172

Value of list[1] = 100

Address of list[2] = 31627176

Value of list[2] = 200

Sharing Is Caring:

Desktop and Android Developer

2 thoughts on “Chapter: 6 Pointer”

  1. In this grand design of things you’ll get an A+ just for effort. Exactly where you confused me was on all the specifics. You know, people say, details make or break the argument.. And it couldn’t be more accurate right here. Having said that, allow me reveal to you precisely what did work. The authoring is certainly rather engaging and that is probably the reason why I am taking the effort in order to comment. I do not make it a regular habit of doing that. 2nd, even though I can easily notice a jumps in reason you make, I am not convinced of exactly how you appear to unite the points which inturn help to make the actual final result. For now I will, no doubt subscribe to your position but trust in the foreseeable future you actually connect your dots much better.

    Reply

Leave a Comment

Slide to prove you're not a bot/spammer *