What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » C - vectors

C - vectors
Author: Message:
Chancer
Senior Member
****

Avatar

Posts: 648
Reputation: 7
34 / Male / Flag
Joined: May 2005
Status: Away
O.P. C - vectors
Quick question.

If I create a vector and read from the keyboard:

code:
char word[20];
scanf ("%s", word);

How can I know/discover the number of elements added into the vector?
06-15-2008 01:53 AM
Profile E-Mail PM Find Quote Report
Mnjul
forum super mod
******

Avatar
plz wub me

Posts: 5396
Reputation: 58
– / Other / Flag
Joined: Nov 2002
Status: Away
RE: C - vectors
Use strlen() ? Include <string.h> .

Anyway, it's an array, not vector; because there's a C++ STL type called vector :p
06-15-2008 11:12 AM
Profile PM Web Find Quote Report
Chancer
Senior Member
****

Avatar

Posts: 648
Reputation: 7
34 / Male / Flag
Joined: May 2005
Status: Away
O.P. RE: C - vectors
quote:
Originally posted by Mnjul
Use strlen() ? Include <string.h> .

Anyway, it's an array, not vector; because there's a C++ STL type called vector :p
Nice. That's what I was looking for.

My teachers call it 'vector'. In Portuguese, obviously. So it may be a translation issue or something.

06-15-2008 08:34 PM
Profile E-Mail PM Find Quote Report
TheSteve
Full Member
***

Avatar
The Man from Japan

Posts: 179
Reputation: 23
40 / Male / Flag
Joined: Aug 2005
RE: C - vectors
Keep in mind that strlen() will only work if your string has a null character at the end.  In the case of scanf, it should be null terminated as long as the user doesn't input more than 19 characters.   (more than that and the program could crash or just act strangely of course.)
quote:
Originally posted by RaceProUK
quote:
Originally posted by Chancer
code:
char word[20];
scanf ("%s", word);

Bye-bye program :P
Not always.  Depending on how much data is inputed in to the program, and how much other data is on the stack, it could just overwrite the other variables in the function. Even if the data size does exceed the current stack frame, there's always going to be a chance that the return address is going to be overwritten by a random, but valid memory address, which would cause a very interesting situation to debug. :P

This post was edited on 06-16-2008 at 05:29 AM by TheSteve.
06-16-2008 05:28 AM
Profile PM Web Find Quote Report
Chancer
Senior Member
****

Avatar

Posts: 648
Reputation: 7
34 / Male / Flag
Joined: May 2005
Status: Away
O.P. RE: C - vectors
quote:
Originally posted by TheSteve
Keep in mind that strlen() will only work if your string has a null character at the end.  In the case of scanf, it should be null terminated as long as the user doesn't input more than 19 characters.   (more than that and the program could crash or just act strangely of course.)
Yes, of course.
If I was taugth right, the last thing that '%s' adds to the sequence is '\0'. So this is the 'null character' you mentioned.
06-19-2008 01:57 AM
Profile E-Mail PM Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On