Describe how to pass a structure variable to a procedure and also how to store a structure variable in an array. Provide an example of each to demonstrate your understanding. Please provide with a clear answer and explanation.
Expert Answer
struct automobile //structure declaration
{
public string name;
public int price;
}
class Program
{
static void Main(string[] args)
{
private static void structfn(ref s struc)//function to receive struc as parameter
{
Console.WriteLine(“car name = ” + struc.name);//print the car name
Console.WriteLine(“car price = ” + struc.price);//print the car price
}
car[] c=new car[4];//declaring car arraysize as 4 so that we can enter 4 car details
for(int i=0;i<4;i++)//looping through 4 times to read the car names
{
//requesting for userinput
Console.WriteLine(“Please enter Car Name, Car Price you know”);
// assigning values to structure variables
c[i].name = Console.ReadLine();
c[i].price = Int32.Parse(Console.ReadLine());
// use ref to pass the structure as parameter using reference:
structfn(ref automobile);
}
}
}