Convert structure object to pointer

  • Thread starter Thread starter renu
  • Start date Start date
R

renu

hello,
I have one structure named CHAINPOINT,like
CHAINPOINT
{
int x,y;
byte dir
}
I am creating object of CHAINPOINT like
CHAINPOINT* arrayPoint;
arrayPoint = new
CHAINPOINT[arrayChainCode.count];
where arrayChainCode is object of ArrayList.
But its giving error like Cannot implicitly convert type CHAINPOINT[]
to CHAINPOINT*.
If I type cast it 'CHAINPOINT*' then also its giving error.
What can do to solve this problem?
Please suggest me.
Thanking you in advance

Renu
 
Renu,

Usage of 'unsafe' and 'fixed' is requried to handle this scenario.

The fixed statement sets a pointer to a managed variable and "pins"
that variable during the execution of statement. Without fixed,
pointers to movable managed variables would be of little use since
garbage collection could relocate the variables unpredictably. The C#
compiler only lets you assign a pointer to a managed variable in a
fixed statement.

Added a small example to help you understand, hope this helps

public class Chainpoint
{
struct Point
{
int _x;
int _y;

public Point(int p,int q)
{
_x = p;
_y = q;
}

public override string ToString()
{
return " x -> " + _x.ToString() + " : y -> " +
_y.ToString();
}

}

public static void Main()
{

char[] initials = new char[] { 'a', 'c', 'e' };
unsafe
{
/// Example1
fixed (char* p = &initials[0])
{
Console.WriteLine(" First value is ... " +
(char)*p);
};

/// Example2

Point[] coordinates = new Point[]{ new Point(2,2),new
Point(1,1) };

fixed (Point* ptr = &coordinates[1])
{
Console.WriteLine(ptr->ToString());
}

}
}
}


Thanks
Shyam
 
renu said:
hello,
I have one structure named CHAINPOINT,like
CHAINPOINT
{
int x,y;
byte dir
}
I am creating object of CHAINPOINT like
CHAINPOINT* arrayPoint;
arrayPoint = new
CHAINPOINT[arrayChainCode.count];
where arrayChainCode is object of ArrayList.
But its giving error like Cannot implicitly convert type CHAINPOINT[]
to CHAINPOINT*.
If I type cast it 'CHAINPOINT*' then also its giving error.
What can do to solve this problem?
Please suggest me.
Thanking you in advance

Renu


Why do you declare the arrayPoint as a pointer variable in the first place? Just declare it
as an array like 'CHAINPOINT[] will solve all of your issues.

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top