# Simple Array Question

This topic is 4255 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

If I define an array within a structure:
struct test
{
char[20] chName;
};

How do I assign a string to name... e.g. the following will not compile:
test mytest;
myTest.chName = "MyName";

Can anyone offer any help? Many thanks.

##### Share on other sites
In c, strcpy(), but be careful to ensure you have enough space in the array.
test mytest;strcpy(myTest.chName,"MyName");

In c++, std::string.

#include <string>struct test{    std::string chName;};test mytest;myTest.chName = "MyName";

That shouldnt work. You cannot assign to an array. Also you are leaking the memory after the malloc().

##### Share on other sites
struct test{    char chName[20];};test mytest;strcpy(myTest.chName, "MyName");

But, since you did not typedef your struct, you must be using C++. In which case you sghould use the string class:

struct test{    std::string name;}test mytest;mytest.name = "MyName";

Much easier and safer to boot [smile].

##### Share on other sites
struct test
{
char[20] chName;
};

Firstly, that should be

char chName[20];

-- I'm surprised that even compiles!

Second, you need to copy the string in using strcpy... but that's a bit dodgy since you need to check that the string you are copying is 19 characters long or less (you need to allow 1 char for the null terminator). strncpy is safer but might not be standard.

If you are programming in C++ (I suspect you might be since you have no typedef in your struct declaration), you should really use std::string before learning about arrays of char, since you are only just starting off. That's the easier way.

This code...

test mytest;

char *buffer = (char*)malloc(20);
buffer = "Arial";

mytest.chName = buffer;

... is very wrong... you are doing a malloc and then throwing away the allocated memory... so how can you free it? You can't assign a char* to a char array either without strcpy, since myTest.chName has a constant address... so there is something wrong with that as well.

##### Share on other sites
It's a little irresponsible to propagate the use of the deprecated strcpy function.

jpbb, I strongly suggest you use std::string for your purposes. It is safer, easier and laden with wonderful features.

If that's not possible, then you should use strncpy. It's not without its problems, but it can help to avoid buffer overruns and hence exploitable code.

Regards

##### Share on other sites
Quote:
 Original post by TheAdmiralIt's a little irresponsible to propagate the use of the deprecated strcpy function.jpbb, I strongly suggest you use std::string for your purposes. It is safer, easier and laden with wonderful features.If that's not possible, then you should use strncpy. It's not without its problems, but it can help to avoid buffer overruns and hence exploitable code.RegardsAdmiral

Indeed.

Please also note that strncpy is standard, and strcpy_s is not; it's instead the usual Microsoft propagation of competing standards for little to no good reason. But that shouldn't matter to you, because std::string behaves exactly like you expect a real string type to, because it is a real string type. Character arrays are not.

But if you really feel the need to learn what's going on, read this.

1. 1
2. 2
3. 3
Rutin
22
4. 4
JoeJ
17
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631774
• Total Posts
3002295
×