Thread: explanation code

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    31

    explanation code

    Code:
    main()
    {
          int *j;
          void fun(int **);
          void(&j);
    }
    
    void fun(int **k)
    {
         int a = 10;
         *k = &a;
    }
    Can anyone explain me how does the address of a gets stored in j.
    I don't understand what's the use of "pointer to pointer to an int" in the "fun(int **) function.
    Can't it just be fun(int *) instead of fun(int **)?

  2. #2
    Registered User dinjas's Avatar
    Join Date
    Feb 2005
    Location
    Vancouver, Washington
    Posts
    40
    yes, it could just be an int* if the code actually
    did anything. There arent any functions being called
    in your code, only function declarations, and those are
    incomplete.
    straight off the heap

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    31

    correction in the code

    Code:
    main()
    {
          int *j;
          void fun(int **);
          fun(&j);
    }
    
    void fun(int **k)
    {
         int a = 10;
         *k = &a;
    }
    Can anyone explain me how does the address of a gets stored in j.
    I don't understand what's the use of "pointer to pointer to an int" in the "fun(int **) function.
    Can't it just be fun(int *) instead of fun(int **)?
    Now I have called function fun(&j) in "main" function.

  4. #4
    Registered User dinjas's Avatar
    Join Date
    Feb 2005
    Location
    Vancouver, Washington
    Posts
    40
    Code:
    #include <...>
    void fun(int *myInt);
    
    int main(void)
    {
         int j;
         fun(&j);
    }
    
    void fun(int *j)
    {
         int a = 10;
         j = &a;
    }
    yea, there's no reason it can't be something like the above, all you are doing is
    changing the integer value pointed at by j.
    Last edited by dinjas; 04-19-2008 at 11:15 AM. Reason: typo
    straight off the heap

  5. #5
    Registered User dinjas's Avatar
    Join Date
    Feb 2005
    Location
    Vancouver, Washington
    Posts
    40
    oh wait, I'm an idiot.
    I think you need the double pointer because of memory allocation.
    If you did what I just told you too, this is what i think the problem would
    be:
    Code:
    #include <...>
    void fun(int *myInt);  // declare your function
    
    int main(void)
    {
         int j;   // declare an integer and allocate space for it
         fun(&j);  // pass the address of that integer to your function
    }
    
    void fun(int *k)
    {
         int a = 10;  // declare another integer and allocate space for it on the stack
         k = &a;       // point your pointer at that space. essentially *j = 10;
    }  // except now the function ends and the space allocated for a goes away
       // leaving j pointing at something that may change unexpectedly
    Last edited by dinjas; 04-19-2008 at 11:25 AM. Reason: yet another typo :P
    straight off the heap

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    First: Implicit main = bad --> http://cpwiki.sourceforge.net/Implicit_main
    Second, function prototypes should be global, in a header or at the beginning of the source file.
    Third, many pointer questions are answered here: http://cpwiki.sourceforge.net/A_pointer_on_pointers

    Your code is broken, too. You don't get warnings?
    Basically a pointer to pointer is just a pointer to a variable that is of pointer type. If you want that pointer to retain a value when set inside a function, then you simply get a pointer to pointer. See link for more information.

    One example might be:
    Code:
    int main(void)
    {
         int* j;
         fun(&j);
    }
    
    void fun(int** j)
    {
         *j = malloc(sizeof(int));
    }
    Now j in main will hold a pointer to an allocated buffer allocated using malloc that was allocated inside fun.
    Last edited by Elysia; 04-19-2008 at 11:27 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    31
    Quote Originally Posted by dinjas View Post
    Code:
    #include <...>
    void fun(int *myInt);
    
    int main(void)
    {
         int j;
         fun(&j);
    }
    
    void fun(int *j)
    {
         int a = 10;
         j = &a;
    }
    yea, there's no reason it can't be something like the above, all you are doing is
    changing the integer value pointed at by j.
    I want my code to be explained not another code. I don't understand the use of "pointer to pointer" in the function argument.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Code Explanation
    By dmkanz07 in forum C Programming
    Replies: 1
    Last Post: 03-27-2007, 08:24 PM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM