Using a pointer as a character array

This is a discussion on Using a pointer as a character array within the C Programming forums, part of the General Programming Boards category; Hey guys, Im having problems using a pointer as a character array... Im using "scanf" but it will only display ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    5

    Using a pointer as a character array

    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.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    5

    Ah sarcasm, how I have missed you

    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?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: Ah sarcasm, how I have missed you

    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?
    Of course you can write code. Code is text. You can write text can't you? Sure you can.

    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.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    7
    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

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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
    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.

    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]

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    7
    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

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    7
    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

  9. #9
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    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
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with character array pointer and strcmp
    By axe in forum C Programming
    Replies: 2
    Last Post: 11-13-2007, 07:47 PM
  2. Pros pls help, Pointer to array
    By kokopo2 in forum C Programming
    Replies: 7
    Last Post: 08-17-2005, 11:07 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. mygets
    By Dave_Sinkula in forum C Programming
    Replies: 6
    Last Post: 03-23-2003, 06:23 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21