Sign in to follow this  
jpbb

Simple Array Question

Recommended Posts

jpbb    122
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 this post


Link to post
Share on other sites
rip-off    10976
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";


[in response to your edit]

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

Share this post


Link to post
Share on other sites
Simian Man    1022
To answer your question:
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 this post


Link to post
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 this post


Link to post
Share on other sites
TheAdmiral    1122
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
Admiral

Share this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Original post by TheAdmiral
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
Admiral


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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this