Thread: Need help. printf() and scanf().

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    6

    Need help. printf() and scanf().

    Just thought of taking a no. having digits greater than or equal to 10, as user input and simply print it. But this code doesnot work. Please help..


    Code:
    #include <stdio.h>
    #include <alloc.h>
    
    int main(void)
    {
       void *p=malloc(10);
        scanf("%p",&p);
        printf("%p",p);
        return 0;
    }
    Last edited by HBK; 07-17-2011 at 02:19 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So C is going to be your third language then? Because English sure doesn't seem to be your first.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    6
    Quote Originally Posted by quzah View Post
    So C is going to be your third language then? Because English sure doesn't seem to be your first.


    Quzah.

    Actually english was my 4th Lang.!!! Anyways, I have Edited it.....now, l would like u talk "in C".....!!!

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    You cannot do anything with a void pointer before you cast it to a type.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    #include <alloc.h>
    You may as well just use stdlib.h, since alloc.h is rather old fashioned.
    Code:
        scanf("%p",&p);
        printf("%p",p);
    What is that format specifier supposed to be for? Also, since you have a pointer, you don't need the address of there. You also don't free what you allocate.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by AndrewHunter View Post
    You cannot do anything with a void pointer before you cast it to a type.
    You can assign a value to it.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by quzah View Post
    You can assign a value to it.
    Quzah.
    Haha...thank your quzah for keeping me honest.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You can also print them: %p actually expects a void pointer.

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    %p
    Reads in a pointer to be stored in a void*. The format of this pointer should be the same as that which is outputted with printf() and the "%p" format specifier.
    Fair enough, I assumed there was an underlying question....I guess I was wrong.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    Registered User
    Join Date
    Jul 2011
    Posts
    6
    That format specifier (%p) reads a pointer. And I think u need the address of operator there because in the the format specifier i have given %p. This code does what it is suppossed to do but for 8digit nos. only. It only prints the first 8digits when i enter a 10digit no.

    Can't realy understand what's going on..........

  11. #11
    Registered User
    Join Date
    Jul 2011
    Posts
    6
    That format specifier (%p) reads a pointer. And I think u need the address of operator there because in the the format specifier i have given %p. This code does what it is suppossed to do but for 8digit nos. only. It only prints the first 8digits when i enter a 10digit no.

    And in Pelles C it only prints the last 8digits(ignoring the 1st two digits) when i enter a 10digit no.

    Can't realy understand what's going on..........

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You and me both don't really understand what's going on.

    You allocate p with malloc(), only to later leak that memory by scanning for a value for p. Moreover, the value for p is a memory address. Scanning some random address from the user isn't going to necessarily give p a useful address. This idea is frought with problems.

    And in Pelles C it only prints the last 8digits(ignoring the 1st two digits) when i enter a 10digit no.
    To be absolutely clear with you, pointers have nothing to do with numbers. Use an int.

  13. #13
    Registered User
    Join Date
    Jul 2011
    Posts
    6
    But nither 'int' nor 'long int' can hold a no. with 10 digits (like, 90387569860)!!!

    I said what I want to do.....and I'm clear i guess......Let someone post a working code doing what I want to do.......

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by HBK View Post
    That format specifier (%p) reads a pointer. And I think u need the address of operator there because in the the format specifier i have given %p.
    Just what is it you think you are going to be putting there? What can you possibly be typing there that would be useful input? I'm genuinely curious.
    Quote Originally Posted by HBK View Post
    Let someone post a working code doing what I want to do.......
    Do you even know what you want to do?


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Jul 2011
    Posts
    6
    Quote Originally Posted by quzah View Post
    Do you even know what you want to do?


    Quzah.
    I want to take a no.(conataining 10-15digits) from user and simply print it then!!!

    I guess, I'm clear now. I failed doing it....i tried many ways(using long int and all...) that i could think of, but............

    Share ur way of doing it.....waiting....eagerly..................

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with printf and scanf
    By Teiji in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 11:27 PM
  2. printf() and scanf()
    By homeyg in forum C Programming
    Replies: 1
    Last Post: 02-06-2006, 10:43 PM
  3. scanf and printf
    By gmanUK in forum C Programming
    Replies: 5
    Last Post: 11-25-2005, 03:03 PM
  4. printf and scanf
    By studentc in forum C Programming
    Replies: 3
    Last Post: 06-11-2004, 03:07 PM
  5. something about printf (or maybe scanf)
    By netboy in forum C Programming
    Replies: 4
    Last Post: 06-11-2002, 10:26 PM