Hey guys, Im having problems using a pointer as a character array... Im using "scanf" but it will only display the first word. Help! Any suggestions would be appretiated.
Hey guys, Im having problems using a pointer as a character array... Im using "scanf" but it will only display the first word. Help! Any suggestions would be appretiated.
Using my great mind reading powers, I've pinpointed your problem. Now you can use yours to read my mind and find the answer.
If you don't possess astounding powers like I have, you'll need to post your code so we can see what your problem is. While you're at it, read the top thread in this board to see how to correctly post code.
On a side note, I actually do know what your problem is. It has to do with the way you're trying to read strings with scanf. You are using %s, which stops at the first whitespace. I told you I'm a mind reader.
Quzah.
Hope is the first step on the road to disappointment.
Sorry, but I'm on a PDA and cant write code... although your sarcasm was greatly appreciated, the answer to my question would be more benificial. Yes I am using %s with scanf.... and your right, it does stop at the first space. What possible solution could there be?
Of course you can write code. Code is text. You can write text can't you? Sure you can.Originally posted by BubbleBoy
Sorry, but I'm on a PDA and cant write code... although your sarcasm was greatly appreciated, the answer to my question would be more benificial. Yes I am using %s with scanf.... and your right, it does stop at the first space. What possible solution could there be?
There are multiple solutions.
1) Use the correct format specifier.
2) Use fgets instead.
Can your PDA click links? Read up on the format specifiers for printf. If that doesn't cover it, read up on some of the more rarely used specifiers, such as the bracket specifiers. That's what you'll have to use if you use scanf.
Personally, I'd just use fgets.
And to conclude today's lesson in how to post... read the FAQ, read the sticky note.
Quzah.
Hope is the first step on the road to disappointment.
you could read the whole strings by using the concept of end of file(eof) character.the character tha ends all char arrays. try this out:
char *str;
*str=getch();
while ( *str++ != eof)
*str=getch();
Doing this will alow you to read multiline strings until you hit Ctrl+z
This won't work too well. To start with getch() returns an int not a char (most probably), and will cause problems with EOF. You do no bounds checking, so an overrun becomes an easy exploit/bug. eof is actually EOF. You shouldn't control your loop in the manner shown, you'll end up putting EOF into the array, which is not a desired situation. The char array doesn't get nul terminated, so you can't use it as a string.Originally posted by Priyank
you could read the whole strings by using the concept of end of file(eof) character.the character tha ends all char arrays. try this out:
char *str;
*str=getch();
while ( *str++ != eof)
*str=getch();
Doing this will alow you to read multiline strings until you hit Ctrl+z
Like Quzah said, use fgets() as shown here.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
getch() returns whatever user enters ...i suppose .....and the code which i have used requires no bound checking at all it allows u to enter strings till the time u want to and then hit Ctrl+z to get out of it
U see the whole point of this construction is that........whatever be the size of ur input it doesnt matter since what it does is it reads a singel char at a time and stores it in *str and then increments str so it goes on and on until end of file is reached
But you didn't allocate space for str. You always need to allocate space in order to write something to memory. Otherwise you end up overwriting memory of other variables or applications and you get Access Violations, core dumps, etc.Originally posted by Priyank
U see the whole point of this construction is that........whatever be the size of ur input it doesnt matter since what it does is it reads a singel char at a time and stores it in *str and then increments str so it goes on and on until end of file is reached