Zero-sized array in class/struct?

L

Lyle Avery

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.
 
D

David Wilkinson

Lyle said:
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
 
L

Lyle Avery

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" <[email protected]> 写入消æ¯
Lyle said:
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
 
B

Bo Persson

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" <[email protected]> skrev i meddelandet 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...
 
D

Doug Harrison [MVP]

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.
 
B

Ben Voigt

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.
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.
 

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

Top