Thread: printing dereferenced pointer.

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    5

    printing dereferenced pointer.

    Code:
      1 #include <stdio.h>
      2
      3 main()
      4 {
      5     int* x;
      6     x = (int*) malloc(sizeof(int));
      7     scanf("*x: %d", x);
      8     printf("*x: %d\n", *x);
      9 }
    After I compile:

    Code:
    [oadams@lister] pointers [1:119] a.out
    4
    *x: 134613344
    Why is this? If I change the *x to an x in the printf statement I get exactly the same output.

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Much to think about here. Your most immediate problem is this line:
    Code:
    scanf("*x: %d", x);
    The bit in red should not be there. The '4' that you entered never got into the variable x, and so the output you got is just garbage that happened to be in that area of memory.

    You should be thinking of a couple of other issues as well. First, main returns an int.
    Code:
    int main(void)
    {
    
        ...
     
       return 0;
    }
    Also, you cast the return value of malloc:
    Code:
    x = (int*) malloc(sizeof(int));
    One question - why? The cboard FAQ has a decent entry on that subject here. There is also some good reading about the matter in the comp.lang.c FAQ:
    7.6 -- Why am I getting ``warning: assignment of pointer from integer lacks a cast'' for calls to malloc?
    7.7 -- Why does some code carefully cast the values returned by malloc to the pointer type being allocated?
    7.7b -- What's wrong with casting malloc's return value?
    Last edited by kermit; 10-18-2009 at 06:43 AM.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    First:

    • the return type of main is int
    • malloc is part of stdlib.h*
    • you do not cast malloc calls in C


    But your problem is you are asking scanf to read this as input:
    *x: [a number]
    Try typing that and you will notice the program does work. But what you wanted was this:
    Code:
    printf("*x: ");
    scanf("%d", x);
    *it may "work anyway" (or it may not, how are you to tell?)
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    5
    Wow, thanks for the good responses!

    I've been reading "The C Programming Language" By K&R.

    In it they often just write the main function without a type declaration or a void argument. I use this and it works. Do I really have to specify the return type,argument type and then go 'return 0;' if all goes well?

    EDIT: Oh so casting malloc is totally unnecessary?
    Last edited by olliepa; 10-18-2009 at 07:25 AM.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by olliepa View Post
    I've been reading "The C Programming Language" By K&R.

    In it they often just write the main function without a type declaration or a void argument. I use this and it works. Do I really have to specify the return type,argument type and then go 'return 0;' if all goes well?
    Those two goofs I guess we set a higher standard here.

    Well, you can do whatever you like, but it is supposed to int main. The return value is for the operating system.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by olliepa View Post
    Wow, thanks for the good responses!

    I've been reading "The C Programming Language" By K&R.

    In it they often just write the main function without a type declaration or a void argument. I use this and it works. Do I really have to specify the return type,argument type and then go 'return 0;' if all goes well?
    The C language has changed a bit since K&R wrote that book. A (small) bit of historical perspective from the board FAQ might be helpful for you - read about it here.

    Then you may as well have at look at the following sections of the comp.lang.c FAQ (Keeping in mind that the following are more general in nature, and not specifically dealing with your questions; I posted them so that you might have a broad view of some of the issues surrounding the declaration of main. That they have found their way into the FAQ is indicative of the widespread dissemination of false information regarding this matter):

    1.25b

    11.12a
    11.12b
    11.13
    11.14a
    11.14b
    11.15
    11.16

    Quote Originally Posted by olliepa View Post
    EDIT: Oh so casting malloc is totally unnecessary?
    Unless you are using an ancient compiler, then yes, it is completely, absolutely, totally not necessary.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by kermit
    Unless you are using an ancient compiler, then yes, it is completely, absolutely, totally not necessary.
    Or if you intend your code to be compilable as C++ as well.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    5
    Quote Originally Posted by MK27 View Post
    *it may "work anyway" (or it may not, how are you to tell?)
    It does 'work anyway'. But I don't know how that could possibly be the case if I havn't imported the library that has the function in it. How can it be?

    Thanks a lot for the links, kermit. Much appreciated.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by olliepa View Post
    It does 'work anyway'. But I don't know how that could possibly be the case if I havn't imported the library that has the function in it. How can it be?

    Thanks a lot for the links, kermit. Much appreciated.
    You have "imported" the library -- it's the same library that allows you to print stuff to the screen, and, well, do anything at all. The C runtime (which includes all the standard functions) is linked automatically whenever you create an executable file. What the header file does is allows the compiler to know what the function should be -- if you don't have it, everything will be turned into ints whether that's what you want or not.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. printing the contents the pointer points to
    By cblix in forum C Programming
    Replies: 8
    Last Post: 01-27-2006, 10:10 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM