Thread: Doubt in pointer to structure

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    53

    Doubt in pointer to structure

    Code:
    static int *p;
    
    void main()
    {
        point_update(&p);
        printf("%d",*p);
    }
    
    point_update(int **q)
    {
       int x = 5;
       *q = &x;
    }
    In the above pgm, why *p is giving some junk value?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    When control returns from the point_update function, the local variable x no longer exists, hence dereferencing p in main leads to undefined behaviour. Also, to print a pointer, you should be using the %p format specifier, not %d, and you should cast the pointer argument to void*. Oh, and void main should be int main, and you should declare point_update before using it.
    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

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    In addition to all that laserlight has said, specify the return type of point_update() as well.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by grumpy View Post
    In addition to all that laserlight has said, specify the return type of point_update() as well.
    The default return type of a function is int,but it is better to declare it by yourself.
    In this case,you should declare it void of course

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    In addition to allthat grumpy and laserlight and std10093 said, you need to have a prototype declared before you call a function that takes a variable number of parameters. In this case, the prototype for the function printf() is in the standard header <stdio.h>
    Code:
    #include <stdio.h>
    /* ... */

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Quote Originally Posted by std10093 View Post
    The default return type of a function is int,but it is better to declare it by yourself.
    In this case,you should declare it void of course
    Actually that is only true with the outdated C standards, like C89-90. The current standard doesn't allow default return types.

    Jim

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by qny View Post
    In addition to allthat grumpy and laserlight and std10093 said, you need to have a prototype declared before you call a function that takes a variable number of parameters. In this case, the prototype for the function printf() is in the standard header <stdio.h>
    Code:
    #include <stdio.h>
    /* ... */
    Or instead of a prototype,you could define your function before you use it (before main in other words)
    definition
    Code:
    int foo(void)
    {
        printf("inside foo\n");
    }
    declaration
    Code:
    int foo(void);
    The way that gny is more elegant and it is the one i suggest

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doubt if this is bad pointer
    By gusth in forum C Programming
    Replies: 2
    Last Post: 09-08-2011, 12:24 AM
  2. Basic doubt in C structure
    By cbalu in forum Linux Programming
    Replies: 2
    Last Post: 08-17-2009, 11:03 AM
  3. Doubt in structure
    By karthik537 in forum C Programming
    Replies: 15
    Last Post: 01-21-2009, 03:52 AM
  4. doubt regarding C structure in linux
    By Bargi in forum C Programming
    Replies: 2
    Last Post: 01-23-2007, 06:18 AM
  5. structure doubt...
    By incognito54 in forum C Programming
    Replies: 6
    Last Post: 04-21-2004, 11:54 AM