473,461 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Declaring an array within a structure

kd
Hi All,

I need to declare an array in a structure. The following gives an error at
private char[] chararr = new char[5];
------------------------------------------------------
struct SimpleStruct
{
private int xval;
private char[] chararr = new char[5];

public SimpleStruct(int intval, char[] chararrval)
{
xval = intval;
for(int i = 0; i < 5; i++)
{
charr[i] = chararrval[i];
}
}
}

Could anybody please post the correct declaration?

Thanks,
kd.
Nov 17 '05 #1
3 12583
You have to declare that property inside the constructor. Make it like so:

struct SimpleStruct
{
private int xval;
private char[] chararr;

public SimpleStruct(int intval, char[] chararrval)
{
chararr = new char[5];
xval = intval;

for(int i = 0; i < 5; i++)
{
charr[i] = chararrval[i];
}
}
}

Hi All,

I need to declare an array in a structure. The following gives an error at
private char[] chararr = new char[5];
------------------------------------------------------
struct SimpleStruct
{
private int xval;
private char[] chararr = new char[5];

public SimpleStruct(int intval, char[] chararrval)
{
xval = intval;
for(int i = 0; i < 5; i++)
{
charr[i] = chararrval[i];
}
}
}

Could anybody please post the correct declaration?

Thanks,
kd.

Nov 17 '05 #2
And on second examination, it looks like you're trying to do this...

struct SimpleStruct
{
private int charsLength;
private char[] chars;

public SimpleStruct(char[] input)
{
charsLength = input.Length;
chars = new char[charsLength];
Array.Copy( input, chars, charsLength );
}
}
Nov 17 '05 #3
Nick Baldwin <ra*****@gmail.com> wrote:
And on second examination, it looks like you're trying to do this...

struct SimpleStruct
{
private int charsLength;
private char[] chars;

public SimpleStruct(char[] input)
{
charsLength = input.Length;
chars = new char[charsLength];
Array.Copy( input, chars, charsLength );
}
}


Alternatively:

chars = (char[]) input.Clone();

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4

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

Similar topics

4
by: Excluded_Middle | last post by:
how to convert these two notation in pointer form: 1 - list = value; (explaination) - list is an array of structure of type M. - count is integer index - value is a structure of type M. 2 -...
5
by: Kelvin Moss | last post by:
Hi group, I have an array of structure. Say - typedef struct Person { int i; int j; int k; }person;
1
by: jimbo | last post by:
Here is my problem. I'm creating an Instrumentation class that will use previously created Performance Categories and Counters in order to time various processes (ie. query duration etc.). This...
2
by: Pas de Spam | last post by:
Hi, I have some problem in marshaling array of structure. I have to exchange a structure of data with a C++ program. This structure data contains a field which is an array of a custom type. I...
0
by: Vidya Bhagwath | last post by:
Hello Experts, I am porting the C++ code to the C#. In my C++ code, I have used structure of function pointers. Then I have created the array of that structure and intialized that array. Here...
0
by: protein | last post by:
Hi, This is my 2 structures which are used for dll's parameter: <StructLayout(LayoutKind.Sequential)> Structure TESTDATA <VBFixedArray(1, 128), MarshalAs(UnmanagedType.ByValArray,...
1
by: deme | last post by:
the problem is: do an array of structure to store ur personal info! and the other one is do a structure of array to store ur personal info! guys if it is possible can u pls tell me how to do this? am...
2
by: sunilvalli | last post by:
Hi All, I would like know how to allocate memory for an array of structure pointers..Like for example typedef struct List { char* Name; char* Address; BOOL is_present
2
by: Opteron64 | last post by:
Hi, I'm trying to create and initialise a dynamic array within a nested structure. The structure is defined as followed: (C++ code) typedef unsigned char uchar; typedef unsigned int uint; ...
2
by: Phoenix | last post by:
Hi Friends , Could anyone please help me to resolve the following issue : I pass an array of structures to a dll written in VC++ 6.0 whih fills it with data . The following works well for VB...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...
1
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.