473,396 Members | 1,832 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,396 software developers and data experts.

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 12579
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...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.