Thread: passing pointers to structures

  1. #1
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95

    passing pointers to structures

    The following code seems to work. I appreciate any comments you can make on how to improve it please. The tough part for me was treating the pointers to the list slightly differently from the pointers to each node. My thinking was that they are both of type struct entry. I think where things started to break was when I had to use each type to reference what they pointed to. That is the next pointer of each node vs. listPtr, and other pointers outside of given node. I think I need to do more of these problems to get used to how to access each type. Here is the code:

    Code:
    #include <stdio.h>
    struct entry {
        int value;
        struct entry *next;
    };
    struct entry *findEntry ( struct entry *listPtr, int match)
      {
        while ( listPtr != (struct entry *) 0)
          {
          printf ("Here is the current value%i\n", listPtr->value);
          if ( listPtr->value == match )
            return (listPtr);
          else
            listPtr = listPtr->next;
          }
        return (struct entry *) 0;
      }
    void insertEntry ( struct entry *newNode, struct entry *targetPtr)
    // This function inserts a node after the targe Node
    {
       // set the pointer inside of new object to point to what target node is pointting to right now
       newNode->next = targetPtr->next;
       printf("Here is what is in new node value%i\n Here is what is in targetNode value %i\n",newNode->next, targetPtr->next);
       // Now move the pointer inside of target node, and point to new object
       // keep in mind that targetPtr is a pointer pointing to the new node. It is not a node; rather a pointer to a node
       targetPtr->next = newNode;
    }
    struct entry newNodef ()
      {
        struct entry node, *nodePtr;
        int newValue;
        printf("Please tell me what value you want to store in the new node\n");
        scanf("%i", &newValue);
        node.value = newValue;
        return node;
      }
    
    int main (void)
    {
     struct entry *findEntry ( struct entry *listPtr, int match);
     void insertEntry ( struct entry *newNode, struct entry *targetPtr);
     struct entry newNodef ();
     struct entry n1, n2, n3;
     struct entry *listPtr, *targetPtr, *listStart = &n1;
     int search;
     n1.value = 100;
     n1.next = &n2;
     n2.value = 200;
     n2.next = &n3;
     n3.value =300;
     n3.next = (struct entry *) 0;
     struct entry tempNode = newNodef();
     listPtr = &tempNode;
     targetPtr = &n2;
     insertEntry ( listPtr, targetPtr);
     printf("Here is what the new node is pointing to now %i. \n", tempNode.next->value );
     printf ("Enter value to locate: ");
     scanf ("%i", &search);
     listPtr = findEntry (listStart, search);
     if ( listPtr != (struct entry *) 0)
       printf ("Found %i.\n", listPtr->value);
     else
       printf ("Not found. \n");
     return 0;
    }

  2. #2
    Banned
    Join Date
    Oct 2014
    Location
    Home
    Posts
    135
    I'd suggest trying to keep it simple. I usually don't use struct or pointers since they are the trickiest parts of programming in my opinion. For me it is my biggest learning hurdle. For instance if you are doing files I write out each part separated from the other types instead of a struct that includes all the different types conveniently for you. Instead of pointers I would use char type!

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    Quote Originally Posted by Tien Nguyen View Post
    I'd suggest trying to keep it simple. I usually don't use struct or pointers since they are the trickiest parts of programming in my opinion. For me it is my biggest learning hurdle. For instance if you are doing files I write out each part separated from the other types instead of a struct that includes all the different types conveniently for you. Instead of pointers I would use char type!
    You either have too much free time and an obssesive love for trolling or you really are oblivious, either way . You need help

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should not use (struct entry *) 0. Either be explicit with NULL or be implicit, i.e.,
    Code:
    while (listPtr != NULL)
    or:
    Code:
    while (listPtr)
    You don't need to forward declare the functions in main since their definitions already serve as declarations. In fact, this is not a proper forward declaration because it declares a function named newNodef that returns a struct entry and takes an unknown number of arguments:
    Code:
    struct entry newNodef ();
    For a proper forward declaration, you should explicitly declare that the function takes no arguments:
    Code:
    struct entry newNodef(void);
    Personally, I am wary of the fact that you construct a node in two stages, i.e., with newNodef followed by insertEntry. This means that some point in between, the new node has a data value, but its next pointer is invalid. newNodef should initialise the new node's next pointer to be a null pointer.
    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

  5. #5
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95
    Thank you so much. Nodef () was my own attempt at devoting a function to creation of new nodes, while the rest of the code had the node creation in it. I'll remove the redundant code.

  6. #6
    Registered User
    Join Date
    Dec 2011
    Location
    Namib desert
    Posts
    94
    To make the notation of date types a bit more simple you can typedef pointers to a certain struct.
    For example:
    Code:
    typedef struct ENTRY
    {
        int value;
         struct ENTRY *next;
    } ENTRY,  *PTR_ENTRY;
    
    /*
    For example:
    void myFunc()
    {
        ENTRY anEntry;
        PTR_ENTRY ptr2Entry;
    
         ptr2Entry  =  &anEntry;
         ----
         ----
    }
    
    Btw, your code becomes more readable if you write your own datatypes (typedefs) in uppercase.
    */

  7. #7
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95
    This is a great point Dutch. Thanks.

  8. #8
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95
    really appreciate the comments laser.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ddutch
    To make the notation of date types a bit more simple you can typedef pointers to a certain struct.
    No, don't do that unless the pointer is an opaque pointer (or is meant to be an implementation of an iterator type as in C++), which in this case it isn't. Such typedefs can obscure the fact that the type is a pointer type, or if it does "encode" the pointer type in the typename (e.g., PTR_ENTRY), then rather than having made the types more simple, you have substituted the * syntax with PTR_.

    Also, fully uppercase names are conventionally reserved for macro names (sometimes including names of non-macro named constants), so an ENTRY typedef is a bad idea. Yes, I know FILE is a counterexample, but it has historical baggage.
    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

  10. #10
    Registered User
    Join Date
    Dec 2011
    Location
    Namib desert
    Posts
    94
    Yes, uppercase names are by convention used for macros and other defines.
    Some conventions promote the use of an uppercase for the first character of a typedeffed type name, others use a _t at the end of a typedeffed type name.
    But, especially if you work with a small programmers team or work just by yourself on the development and maintenance of a project, you make your own conventions. And if you think it is a good convention which makes your code more readable, you are always right.

    The same holds for typedeffing pointers to structs.

  11. #11
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95
    Thanks. The last comments prompted me to look into what opaque pointers were; so they were much appreciated. Just to reword, it seems using CAPS for the name of new structure may overlap with other conventions. Ok; I'll proceed with caution.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ddutch
    Some conventions promote the use of an uppercase for the first character of a typedeffed type name, others use a _t at the end of a typedeffed type name.
    Standard C does not reserve such names, but you should be aware that POSIX reserves names with a _t suffix, so if POSIX is relevant to you, it may be wiser not to typedef names with a _t suffix unless you are specifically mimicking POSIX names.

    Quote Originally Posted by ddutch
    But, especially if you work with a small programmers team or work just by yourself on the development and maintenance of a project, you make your own conventions. (...) The same holds for typedeffing pointers to structs.
    Certainly, you are free to adopt your own conventions. However, the typedef of pointers to structs that are not opaque pointers or otherwise have strong conventions for such a typedef is a bad idea for the reasons that I outlined earlier, so if you choose to adopt such a convention, you are simply adopting a bad idea.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of structures and passing pointers to structs
    By Vendrus in forum C Programming
    Replies: 9
    Last Post: 02-24-2013, 04:18 PM
  2. Replies: 6
    Last Post: 08-05-2012, 10:16 PM
  3. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  4. passing structures??
    By justin69enoch in forum C Programming
    Replies: 6
    Last Post: 03-08-2003, 03:33 AM
  5. Replies: 5
    Last Post: 04-11-2002, 11:29 AM

Tags for this Thread