Thread: double astrix pointer question..

  1. #76
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Quote Originally Posted by MK27
    which I imagine is not the same thing, but then (*p)->next causes a segfault.
    You know the mantra
    Post the smallest and simplest compilable program that demonstrates the problem.

    Quote Originally Posted by transgalactic2
    so you agree with me that if we define
    Node **p;
    then we have two pointers in this definition
    in which
    p points *p

    correct?
    No, there is only one pointer, namely, p. p does not point to *p yet because p is left uninitialised.
    Last edited by laserlight; 01-18-2009 at 01:43 PM.
    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

  2. #77
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    I must add, once again, that it is only a VARIABLE. It creates ONE VARIABLE, with a certain type. That is all. It is uninitialized, filled with random garbage just like ANY OTHER VARIABLE.
    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.

  3. #78
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    can you give me an example with function calls of actual addresses to understand Node**p
    for every address i could see to what node/pointer it corresponds


    because i am really confused you said that
    *p does point to a node
    but to a pointer node

    but when i am saying that p points to *p
    you say its wrong
    Last edited by transgalactic2; 01-18-2009 at 01:55 PM.

  4. #79
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by laserlight View Post
    You know the mantra
    Post the smallest and simplest compilable program that demonstrates the problem.
    Hopefully that will be this:
    Code:
    #include <stdlib.h>
    
    typedef struct _node {
    	struct _node *next;
    } node;
    
    int main () {
    	node *test=malloc(sizeof(node)), **p;
    	*p=test;
    	return 0;
    }
    gcc -Wall test.c
    ./a.out
    Segmentation fault
    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

  5. #80
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    Try compiling this code, please.
    Compile ALL the code you post before you ask.
    Go ahead and do it right now. We can wait.
    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.

  6. #81
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    Quote Originally Posted by transgalactic2 View Post
    can you give me an example with actual addreses to understand Node**p

    because i am really confused you said that
    *p does point to a node
    but to a pointer node

    but when i am saying that p points to *p
    you say its wrong
    Code:
    #include <stdio.h>
    
    int main()
    {
    	int Temp = 50;
    	int* p1 = &Temp;
    	int** p_to_p = &p1;
    
    	printf("Integer:\t%i\n", Temp);
    	printf("p1:\t\t%p\n", p1);
    	printf("Integer:\t%i\n", *p1);
    	printf("p-to-p:\t\t%p\n", p_to_p);
    	printf("p1:\t\t%p\n", *p_to_p);
    	printf("Integer:\t%i\n", **p_to_p);
    }
    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. #82
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Quote Originally Posted by MK27
    Hopefully that will be this:
    Eh, but that improves on nothing from what transgalactic2 posted and does not use the (*p)->next syntax. p is still left uninitialised. I am talking about something like this:
    Code:
    #include <stdlib.h>
    #include <stddef.h>
    #include <stdio.h>
    
    typedef struct node
    {
        struct node *next;
    } node;
    
    int main()
    {
        node node_obj;
        node *node_ptr = &node_obj;
        node **p = malloc(sizeof(*p));
        node_obj.next = NULL;
        *p = node_ptr;
        (*p)->next = malloc(sizeof(node));
        printf("%p\n", (void*)node_obj.next);
        free(p);
        free(node_obj.next);
        return 0;
    }
    Note that I am assuming that malloc() succeeds in allocating memory.
    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. #83
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Looks like I was wrong about "The other one":
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    typedef struct _node {
    	char content[64];
    	struct _node *next;
    } node;
    
    int main () {
    	node *test=malloc(sizeof(node)), **p;
    	strcpy(test->content,"hello world");
    	p=&test;
    	printf("%s\n",(*p)->content);	
    	return 0;
    }
    Works fine, sorry...
    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

  9. #84
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    Yeah, and don't forget to free.
    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.

  10. #85
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Ah, so with:
    Code:
    *p=node_ptr;
    **p must first be allocated space since *p is what the address in **p would point to?
    Whereas:
    Code:
    p=&node_ptr;
    writes the address of node_ptr into **p?
    By using the dereference (*p), aren't we just compensating for the fact that p is a pointer to a pointer? Meaning why not use a pointer in the first place?
    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

  11. #86
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Quote Originally Posted by MK27
    By using the dereference (*p), aren't we just compensating for the fact that p is a pointer to a pointer? Meaning why not use a pointer in the first place?
    Because you might want to change the pointer itself, rather than merely change what the pointer points to.
    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

  12. #87
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    you posted many codes and argued regarding them
    which one do i need to compile?

  13. #88
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    You need to compile your own code that post. The last one you posted, which you edited out would have worked horribly wrong.
    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.

  14. #89
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    okkk

  15. #90
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i tried to run the function like this
    its not running
    Code:
    #include <stdio.h>
    void what1(Node ** p,int num);
     
    typedef struct node {
        int value;
        struct node *next;
    }Node;
    
    int main()
    {
    	Node **ptr;
    	ptr=(**Node)malloc(sizeof(Node));
        void what1(ptr,5);
        return 0;
    }
    
    void what1(Node ** p,int num){
       Node *elt;
       elt=(Node*)malloc(sizeof(Node));
       elt->next=*p;
       elt->value=num;
       *p=elt;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about pointer arithmetic and types
    By brooksbp in forum C Programming
    Replies: 4
    Last Post: 08-22-2008, 01:53 PM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM