C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 10th, 2003, 08:37 PM
maskzilla maskzilla is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 11 maskzilla User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #2  
Old April 10th, 2003, 11:10 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,406 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 9 h 35 m 45 sec
Reputation Power: 4080
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);
	};

Reply With Quote
  #3  
Old April 11th, 2003, 10:18 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,252 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 5 Days 19 h 26 m 40 sec
Reputation Power: 1985
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.

Reply With Quote
  #4  
Old April 11th, 2003, 10:41 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,406 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 9 h 35 m 45 sec
Reputation Power: 4080
>> 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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > newbie string question

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap