Simple Array Question

Started by
4 comments, last by Zahlman 17 years, 4 months ago
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.
Advertisement
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().
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].
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.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
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
Ring3 Circus - Diary of a programmer, journal of a hacker.
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.

This topic is closed to new replies.

Advertisement