473,395 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Convert structure object to pointer


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

Nov 22 '06 #1
2 4409

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 wrote:
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
Nov 22 '06 #2
"renu" <re*************@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
>
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.

Nov 22 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Thomas Matthews | last post by:
Hi, Given a structure of pointers: struct Example_Struct { unsigned char * ptr_buffer; unsigned int * ptr_numbers; }; And a function that will accept the structure:
5
by: Alfonso Morra | last post by:
Hi, I am writing a messaging library which will allow me to send a generic message structure with custom "payloads". In many cases, a message must store a non-linear data structure (i.e....
7
by: Prabhu | last post by:
Hi, I have to send a structure through TCPClient socket. we can send only byte array through the socket, So please any one can help me by telling How to convert a struct object into an byte...
23
by: Eric J.Hu | last post by:
Hi, I have following code, want do pointer convert. It always complain: vcnvt.c: In function `main': vcnvt.c:20: warning: dereferencing `void *' pointer vcnvt.c:20: request for member `key'...
8
by: SP | last post by:
The following code crashes after I add the two nested FOR loops at the end, I am starting to learn about pointers and would like to understand what I'm doing wrong. I think the problem is the way...
4
by: Neal Becker | last post by:
In an earlier post, I was interested in passing a pointer to a structure to fcntl.ioctl. This works: c = create_string_buffer (...) args = struct.pack("iP", len(c), cast (pointer (c),...
14
by: deepak | last post by:
Hi Experts, I'm getting this compilation error while trying to access a member in structure. at what time we will get this error message? Thanks, Deepak
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
5
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.