Thread: How to print everything you type till you hit enter using POINTERS.

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    33

    How to print everything you type till you hit enter using POINTERS.

    I think I need to use

    Code:
    while(input[0]!='\n'){   
    scanf("%s", input)
    printf("%s", pointerhere);
    }
    basically we were asked to make a program that will print everything before the user hits the enter button. we cannot use fgets and we have to use pointers here.

    not sure what exactly to do. Can you please help me?
    We were told not to use fgets cause it's so easy and we need to incorporate pointers in this project.. all help would be appreciated.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    input[0] isn't a pointer, and neither is "input". "input" HAS an address, and can be CLOSE to a constant char pointer - but it just isn't a char pointer.

    What about using a char *ptr, and working with that? I believe that's what your instructor wants you to do, in this case. Sound right?

    Give that a try!

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Sounds like an interesting assignment. I'm glad to hear: "We were told not to use fgets cause it's so easy and we need to incorporate pointers in this project" because it means "fgets()" is being taught.

    Two questions:
    1. Have you been taught about memory allocation?
    2. Have you learned about memory allocation?

    (Yes, those are distinct questions.)

    [pedantic]
    Unless you're talking a cash register drawer, or cultivating crops, it's "until" - the colloquial being "'til."
    [/pedantic]

    Give that a try!
    Yes, do! Review your notes, give it your best shot, and let us know what you've come up with!

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    33
    I still am not sure what to do here though.. any ideas?

    Thanks adak, I declared a ptr to the array I'm using atm. Not sure what to do after that though.

    May I ask where is the data after the space in scanf is stored?

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Pointer Arithmetic

    I'm guessing you probably don't want to use scanf, you want to use a character-by-character approach, like getchar(): Using Getchar

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    33
    How do I use getchar and pointers at the same time?

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You need to spend some time working through the tutorial linked with the button, at the top of this page. Unless it's the first week of your first C class or chapter in your self-study book, you should know where the data is stored.

    You'll make FAR faster progress in C, if you run through that tutorial. Forums aren't designed for that kind of tutoring - even this forum which is wickedly fast with responses - is too slow for tutoring.

  8. #8
    Registered User
    Join Date
    Feb 2013
    Posts
    33
    Code:
    int main()
    {
        char array[100];
        char *ptr;
        ptr = &array[0];
        int i, j;
        fgets(ptr, 100, stdin);
        printf("%s", array);
        getch();
        return 0;
    }
    Here's my code and it basically does all that my prof asked us to do.. I used fgets here though. Is there any way I can use scanf for this?

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    basically we were asked to make a program that will print everything before the user hits the enter button.
    "scanf()" will not work for this purpose (without some trickery) because it will stop reading at the first white space. Also, if you aren't supposed to use "fgets()", I imagine that "scanf()" would also not be allowed.

    I'll ask one more time: Have you covered dynamic memory allocation?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. to take input till enter is pressed
    By shruthi in forum C Programming
    Replies: 10
    Last Post: 08-25-2012, 12:34 PM
  2. Replies: 4
    Last Post: 10-30-2009, 06:01 PM
  3. it won't print the 0 if i enter 102
    By maybabier in forum C Programming
    Replies: 1
    Last Post: 04-17-2009, 02:13 AM
  4. Print pointers
    By donkeyass in forum C++ Programming
    Replies: 8
    Last Post: 10-18-2007, 12:45 AM
  5. print astrik when enter password
    By ck12 in forum C++ Programming
    Replies: 0
    Last Post: 10-18-2001, 10:16 PM