A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

C# program to access array elements using the pointer
Q:

C# program to access array elements using the pointer

0

C# program to access array elements using the pointer

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

The source code to access array elements using pointers is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to access array elements using the pointer.

using System;

class UnsafeEx
{
    static unsafe void Main(string[] args)
    {
        int loop = 0;
        int[] intArr = { 10, 20, 30, 40, 50 };

        Console.WriteLine("Array elements are:");
        fixed (int* ptr = intArr)
        for (loop = 0; loop < intArr.Length; loop++)
        {
            Console.Write(*(ptr + loop)+" ");
        }
        Console.WriteLine();
    }
}

Output:

Array elements are:
10 20 30 40 50
Press any key to continue . . .

Explanation:

In the above program, we created class UnsafeEx that contains the Main() method, here we used the unsafe keyword with the Main() method to define the unsafe method that can use pointers.

In the Main() method, we created an array of integer elements then we assign the address of the array to the pointer and then print array elements using the pointer on the console screen.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now