471,056 Members | 1,514 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Zero-sized array in class/struct?

Hello guys,

Look at this in c++ file:
class T
{
public:
char c;
char ca[];
};

The size of this class sizeof(T) is 1, and this is error when assign a string to char[] e.g. T t; t.ca="Hello World!"; : error C2440: '=' : cannot convert from 'const char [13]' to 'char []'
Who can tell me why... why not???

If i want to assign a string to member array in struct/union, how to do it?
Help me, thanks.
Oct 8 '06 #1
5 4570
Lyle Avery wrote:
Hello guys,

Look at this in c++ file:
class T
{
public:
char c;
char ca[];
};

The size of this class sizeof(T) is 1, and this is error when assign a string to char[] e.g. T t; t.ca="Hello World!"; : error C2440: '=' : cannot convert from 'const char [13]' to 'char []'
Who can tell me why... why not???

If i want to assign a string to member array in struct/union, how to do it?
Help me, thanks.

Lyle:

You must specify the size of the array

class T
{
public:
char c;
char ca[16];
};

T t;
strcpy(t.ca, "Hello World!");

Or, better, use std::string

class T
{
public:
char c;
std::string ca;
};

T t;
t.ca = "Hello World!";

And better also use a constuctor for your class and make the data
private. You say you are using C++, not C.

David Wilkinson

Oct 8 '06 #2
Hi David,

Thanks a lot. I use strcpy(t.ca,"Hello World!")/strcpy_s(t.ca,13,"Hello World!"), both of them are ok for copy const string to char[](unfix-sized array in struct/union). But I want to know why t.ca="<const string>" could not to assign a string to an array but copy method strcpy/strcpy_s can do it. Is there any difference between them? Since I'm researching the unfix-sized array in class/struct/union (compiler accept it)and it's object's size. e.g.(No matter how long the string is, the size of type T is still 1 byte)...

class T
{
public:
char c;
char ca[];//must be the last member in structure
};

T t;
strcpy(t.ca, "Hello World!");!!strcpy_s(t.ca, 13, "Hello World!"); //both of them are ok
sizeof(t)/sizeof(T)==1(byte) always...

"David Wilkinson" <no******@effisols.com写入消息 news:uy****************@TK2MSFTNGP03.phx.gbl...
Lyle Avery wrote:
Hello guys,

Look at this in c++ file:
class T
{
public:
char c;
char ca[];
};

The size of this class sizeof(T) is 1, and this is error when assign a string to char[] e.g. T t; t.ca="Hello World!"; : error C2440: '=' : cannot convert from 'const char [13]' to 'char []'
Who can tell me why... why not???

If i want to assign a string to member array in struct/union, how to do it?
Help me, thanks.
Lyle:

You must specify the size of the array

class T
{
public:
char c;
char ca[16];
};

T t;
strcpy(t.ca, "Hello World!");

Or, better, use std::string

class T
{
public:
char c;
std::string ca;
};

T t;
t.ca = "Hello World!";

And better also use a constuctor for your class and make the data
private. You say you are using C++, not C.

David Wilkinson

Oct 8 '06 #3

The char ca[] construct is *not* ok. It is a compiler specific extension that allows you to use that.

The intention is that you (or the system) somehow will allocate a larger chunk of memory, and use that to store the string. The class definition will then allow you to access all the bytes, *if* you know how many there are. Using more than actually allocated is a severe error!

Your strcpy code does *not* work, as it will overwrite whatever happens to reside after your t variable. Don't do that!

If you write C++, use std::string instead. It will take care of all the low level trix for you.
Bo Persson
"Lyle Avery" <g.******@gmail.comskrev i meddelandet news:%2****************@TK2MSFTNGP05.phx.gbl...
Hi David,

Thanks a lot. I use strcpy(t.ca,"Hello World!")/strcpy_s(t.ca,13,"Hello World!"), both of them are ok for copy const string to char[](unfix-sized array in struct/union). But I want to know why t.ca="<const string>" could not to assign a string to an array but copy method strcpy/strcpy_s can do it. Is there any difference between them? Since I'm researching the unfix-sized array in class/struct/union (compiler accept it)and it's object's size. e.g.(No matter how long the string is, the size of type T is still 1 byte)...

class T
{
public:
char c;
char ca[];//must be the last member in structure
};

T t;
strcpy(t.ca, "Hello World!");!!strcpy_s(t.ca, 13, "Hello World!"); //both of them are ok
sizeof(t)/sizeof(T)==1(byte) always...

Oct 8 '06 #4
On Sun, 8 Oct 2006 19:59:58 +0800, "Lyle Avery" <g.******@gmail.comwrote:
>Hi David,

Thanks a lot. I use strcpy(t.ca,"Hello World!")/strcpy_s(t.ca,13,"Hello World!"), both of them are ok for copy const string to char[](unfix-sized array in struct/union). But I want to know why t.ca="<const string>" could not to assign a string to an array but copy method strcpy/strcpy_s can do it. Is there any difference between them? Since I'm researching the unfix-sized array in class/struct/union (compiler accept it)and it's object's size. e.g.(No matter how long the string is, the size of type T is still 1 byte)...

class T
{
public:
char c;
char ca[];//must be the last member in structure
};

T t;
strcpy(t.ca, "Hello World!");!!strcpy_s(t.ca, 13, "Hello World!"); //both of them are ok
sizeof(t)/sizeof(T)==1(byte) always...
I would hope you would not be able to create a T that way. The only valid
way to create a T goes something like this:

// Use malloc/free if you prefer.

T* p = (T*) operator new(sizeof(T)+N);
...
operator delete(p);

where N is the number of bytes you want to be the size of ca. The compiler
does not know what N is (which explains your sizeof observation), so you
have to remember it in your future use of the created T.

--
Doug Harrison
Visual C++ MVP
Oct 8 '06 #5
Your problem is not at all related to being a member array. You can't assign any array variable with =. The variable name is treated as a const pointer to the storage space, and I think you want to move data into that storage space, not change the pointer. If you did want to change the pointer, use a pointer variable instead. If you want to copy the contents (the usual case), use memcpy, memmove, or strncpy.
"Lyle Avery" <g.******@gmail.comwrote in message news:%2****************@TK2MSFTNGP05.phx.gbl...
Hello guys,

Look at this in c++ file:
class T
{
public:
char c;
char ca[];
};

The size of this class sizeof(T) is 1, and this is error when assign a string to char[] e.g. T t; t.ca="Hello World!"; : error C2440: '=' : cannot convert from 'const char [13]' to 'char []'
Who can tell me why... why not???

If i want to assign a string to member array in struct/union, how to do it?
Help me, thanks.
Oct 10 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by matthias_k | last post: by
28 posts views Thread by Andreas Prilop | last post: by
25 posts views Thread by Mantorok Redgormor | last post: by
23 posts views Thread by Hallvard B Furuseth | last post: by
4 posts views Thread by lawrence k | last post: by
5 posts views Thread by DBC User | last post: by
reply views Thread by leo001 | last post: by

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.