The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
newbie string question
Discuss newbie string question in the C Programming forum on Dev Shed. newbie string question C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

April 10th, 2003, 08:37 PM
|
|
Junior Member
|
|
Join Date: Feb 2003
Posts: 11
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
newbie string question
Code:
class Account{
protected:
double acct_bal;
unsigned acct_num;
enum {SS_LEN = 12}; // creates class constant
char ss[SS_LEN];
public:
Account(){};
Account(unsigned n, double b, char* s){
acct_num = n;
acct_bal = b;
s = ss;
};
void deposit(double amt){
amt += acct_bal;
};
int withdrawl(double amt){
acct_bal -= amt;
return 1;
};
double balance(){
cout << acct_bal << endl;
return 1;
};
int transfer(double amt, Account& from, Account& to){
from.withdrawl(amt);
to.deposit(amt);
return 1;
};
};
i just was wondering, cuz i think its wrong.. am i passing the char* s to the ss array correctly?
|

April 10th, 2003, 11:10 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
I think you really meant to assign ss to s (instead of the other way around) in your constructor. Also, to copy strings, you should be using strcpy (or strncpy() or strlcpy() to be REALLY safe).
Code:
Account(unsigned n, double b, char* s){
acct_num = n;
acct_bal = b;
strncpy(ss, s, SS_LEN);
};
|

April 11th, 2003, 10:18 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
Quote: Originally posted by Scorpions4ever
I think you really meant to assign ss to s (instead of the other way around) in your constructor. Also, to copy strings, you should be using strcpy (or strncpy() or strlcpy() to be REALLY safe).
Code:
Account(unsigned n, double b, char* s){
acct_num = n;
acct_bal = b;
strncpy(ss, s, SS_LEN);
};
|
Or rather:
Code:
char ss[SS_LEN+1];
// ...
strncpy(ss, s, SS_LEN);
ss[SS_LEN] = '\0';
To make room for the null terminator. And then since strncpy does not automatically include the null terminator if s is longer than SS_LEN, I normally put it at the end of the array, just in case.
Plus, you misspoke. We think he really meant to assign s to ss.
Maskzilla, the reason we want to use strcpy or strncpy is because your code was assigning a pointer to a pointer, so that ss have ended up pointing to s, rather than having copied what s was pointing to into the array pointed to by ss. If s' string was created in a function, then it would go away once you exited that function, in which case attempts to use ss would have resulted either in a segmentation fault or in clobbering other data (possibly resulting in a crash if you clobber a function's return address) or in reading a bunch of garbage.
It caused me a bit of confusion when I started learning C, but you need to get straight in your mind the difference between a character pointer and the string of characters it's pointing to, plus you need to think about what you are actually doing when you work with strings.
Last edited by dwise1_aol : April 11th, 2003 at 10:29 AM.
|

April 11th, 2003, 10:41 AM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
>> To make room for the null terminator.
Yep, that's why I mentioned strlcpy() to be really safe.
>> Plus, you misspoke. We think he really meant to assign s to ss.
hehe yeah. I guess that's what happens when you're half-asleep and trying to type 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|