#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    11
    Rep 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?
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    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);
    	};
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    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 11:29 AM.
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    >> 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 :)

IMN logo majestic logo threadwatch logo seochat tools logo