Like Tree1Likes
  • 1 Post By whiteflags

Pointers vs. Pointers to pointers

This is a discussion on Pointers vs. Pointers to pointers within the C Programming forums, part of the General Programming Boards category; I keep seeing code that uses the the same function twice, but one copy of the function will use 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
    2,804
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,740
    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,308
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,740
    Eww.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,740
    >> 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.
    phantomotap likes this.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    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


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