Another question about c-strings

Pages: 12
@Lynx876

char empName[21] = {0};

and

char empName[21] = {'\0'};

are equivalent because '\0' == 0 (the null character has ascii value zero).
Ahh right, I didn't know that. I've always explicitly set it as '\0'.
Thanks! (:
closed account (DSLq5Di1)
Assuming you've swapped the parameters in strcpy as shacktar pointed out, the only other issue(s) I see is..
1
2
3
public:
    Employee( int initId=0, char initName[]=0, ... // Bad! initName is a null pointer.
    Employee( int initId=0, char initName[]="", ... // Good, initName is an empty string. 


char getName() {return name[21];}.

You may be trying to return the name here, though it won't work like that. You are actually returning a character at the 21st position of name.. and since name is an array of 21 characters with an index ranging from 0-20, this will cause a run time error.

I think what you want is,
const char* getName() {return name;}

Although if you want a copy of name returned, you'll need to fiddle with dynamic memory.
sloppy9, thanks that helps a lot.

Now I just have to figure out why I can't do this: cout << info[0].getName();
closed account (DSLq5Di1)
Why? What happens..?

I've tested your code on ideone with no apparent issues, see:- http://ideone.com/BwfpN.

ps. Added const to your char array parameters, as the compiler was complaining, and rightly so. I'd also recommend you read about arrays and pointers:-

http://www.cs.bu.edu/teaching/cpp/string/array-vs-ptr/
http://www.lysator.liu.se/c/c-faq/c-2.html
Topic archived. No new replies allowed.
Pages: 12