Thread: Pointers vs. Pointers to pointers

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    4

    Pointers vs. Pointers to pointers

    I keep seeing code that uses the the same function twice, but one copy of the function will use pointers to an array, and another will use pointers to pointers to an array.

    Is there a difference in execution between these two methods, or is it just preference?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Please show some examples to illustrate your question.

    Jim

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    4
    Code:
          int compA(const void* item1, const void* item2) {
          const Emp* emp1 = (const Emp*) item1;
          const Emp* emp2 = (const Emp*) item2;
          
          unsigned char buff1[BuffSize + 1];
          unsigned char buff2[BuffSize + 1];
        
          strcpy(buff1, emp1->lname);
          strcat(buff1, emp1->fname);
          strcpy(buff2, emp2->lname);
          strcat(buff2, emp2->fname);
       
          return strcmp(buff1, buff2);
        }
    and

    Code:
        int compB(const void* item1, const void* item2) {
          const Emp** emp1 = (const Emp**) item1;
          const Emp** emp2 = (const Emp**) item2;
        
       unsigned char buff1[BuffSize + 1];
       unsigned char buff2[BuffSize + 1];
       
          strcpy(buff1, (*emp1)->lname);
          strcat(buff1, (*emp1)->fname);
          strcpy(buff2, (*emp2)->lname);
          strcat(buff2, (*emp2)->fname);
        
          return strcmp(buff1, buff2);
       }
    Or

    Code:
     void dump_emps1(Emp a[ ], unsigned n) {
       102   int i;
            char buffer[BuffSize + 1];
            for (i = 0; i < n; i++) {
            to_string(&a[i], buffer);
            printf("%s\n", buffer);
          }  
        }
    and

    Code:
           void dump_emps2(Emp* a[ ], unsigned n) {
           int i;
           char buffer[BuffSize + 1];
           for (i = 0; i < n; i++) {
           to_string(a[i], buffer);
           printf("%s\n", buffer);
          }
       }

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    In the examples it looks like one set of functions is for an array of Emp pointers. The other set of functions is for an array of Emp. Since the data structures are different--an array of things is not the same as an array of pointers to things--the correct function to use is not just a preference, or a matter of performance. What matters is how the Emp structures are allocated. If the Emp structures are allocated separately from the array data structure so that each array element points to a structure, then only compB and dump_emps2 will work. If you just allocate an array of structures, then only compA and dump_emps1 will work.
    Last edited by whiteflags; 07-03-2012 at 04:03 PM.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    In this instance I would probably replace compB with:
    Code:
    int compB(const void* item1, const void* item2)
    {
        return compA(*(const void**)item1, *(const void**)item2);
    }
    etc
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Eww.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by whiteflags View Post
    Eww.
    Was that in response to my comment?

    Do you not feel "Eww" about the original code duplication?
    How was that not the ideal solution, care to elaborate?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    >> Do you not feel "Eww" about the original code duplication?
    >> How was that not the ideal solution, care to elaborate?

    I'd rather just decide on one way to allocate Emp structures, to be honest. Other than that, nothing is wrong.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Oh right, gotcha.
    Yes I would question the reasons for doing it as well.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  2. Storing function pointers in generic pointers
    By Boxknife in forum C Programming
    Replies: 6
    Last Post: 08-01-2009, 01:33 PM
  3. Variable pointers and function pointers
    By Luciferek in forum C++ Programming
    Replies: 11
    Last Post: 08-02-2008, 02:04 AM
  4. Pointers to objects -- passing and returning pointers
    By 1veedo in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2008, 11:42 AM
  5. weak pointers and use_count smart pointers
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 07-29-2006, 07:54 AM

Tags for this Thread