Doubt in pointer to structure

This is a discussion on Doubt in pointer to structure within the C Programming forums, part of the General Programming Boards category; Code: static int *p; void main() { point_update(&p); printf("%d",*p); } point_update(int **q) { int x = 5; *q = &x; ...

  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
    21,636
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    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%.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    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
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    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
    2,709
    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,675
    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, 02:52 AM
  4. doubt regarding C structure in linux
    By Bargi in forum C Programming
    Replies: 2
    Last Post: 01-23-2007, 05:18 AM
  5. structure doubt...
    By incognito54 in forum C Programming
    Replies: 6
    Last Post: 04-21-2004, 11:54 AM

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