Thread: **ptr and ptr or *ptr and &ptr?

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    21

    **ptr and ptr or *ptr and &ptr?

    Ehlo.

    I came back to programming in C after more than a few years and there's a piece of code I'm not sure I understand correctly. It's a couple of lines of code regarding libpcap. What it does it runs a pcap function to get the list of available devices:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <pcap.h>
    
    int main(void) {
    
            char ebuf[PCAP_ERRBUF_SIZE];
            pcap_if_t *devs;
    
            pcap_findalldevs(&devs, ebuf);
    
            exit(EXIT_SUCCESS);
    }
    This is the way it's supposed to be done. However, when I first saw this function's prototype:

    int pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf);

    I assumed that this code would work as well:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <pcap.h>
    
    int main(void) {
    
            char ebuf[PCAP_ERRBUF_SIZE];
            pcap_if_t **devs;
    
            pcap_findalldevs(devs, ebuf);
    
            exit(EXIT_SUCCESS);
    }
    It does compile but segfaults on execution. I think I know why but I don't want to suggest any particular point of view so, if you know the answer then....well, answer. :)

    Cheers.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Try
    pcap_if_t *devs;
    pcap_findalldevs(&devs, ebuf);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    21
    Yup. That's the way it's supposed to be and that's the first piece of code I quoted. I just wonder why the second piece of code seqfaults.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Because the function expects to WRITE a pointer to the address you supply.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    21
    So what happens memory-wise when you supply it with a dereferenced pointer to a pointer? I mean, doesn't you supply it with a memory address anyway? When it comes to a program's command line arguments you can use either **argv or *argv[]. Why can't you use **ptr (and then dereference it) instead of *ptr (and then $ptr)?

    Code:
    /usr/local/include/pcap/pcap.h:495:14: note: expected ‘pcap_if_t ** {aka struct pcap_if **}’
    so I give it a **ptr, right?

    I know that there is a difference between
    Code:
    char *ptr = "hello!";
    and
    Code:
    ptr[7] = { h, e, l, l, o, !, \0 };
    in that you can't access individual characters of "hello!" using *ptr = "string".

    What's the case here?

    Cheers.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Do you understand this?
    Code:
    void getUniversalAnswer ( int *a ) {
        *a = 42;
    }
    int main ( ) {
       int foo;
       getUniversalAnswer(&foo);
    }
    Do you further understand that this is the same thing.
    Code:
    void getUniversalAnswer ( int *a ) {
        *a = 42;
    }
    int main ( ) {
       int foo;
       int *ptr = &foo;
       getUniversalAnswer(ptr);
    }
    Do you then understand why this is bad?
    Code:
    void getUniversalAnswer ( int *a ) {
        *a = 42;
    }
    int main ( ) {
       // null makes it fail, leaving it uninitialised for randomness
       int *ptr = NULL;
       getUniversalAnswer(ptr);
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    21
    Yup. That's what I suspected happened. I just wanted a confirmation. Thanks.

Popular pages Recent additions subscribe to a feed

Tags for this Thread