Thread: pointer issue

  1. #1
    Registered User
    Join Date
    May 2014
    Posts
    15

    Question pointer issue

    hi, i have a really kind of silly question. i see the problem i just don't know how to solve it. i am trying to fill in a linked list of info on lighting. i will post the structures relevant in the linked list and the code snippet im having troubles with. i just want to assign some allocated memory to a pointer to the next available light structure. but what im doing right now is im just pointing a pointer that points to the next member of the list to the allocated memory. i want to point the pointer that the first pointer points to to the memory. sorry its kinda hard to say (:P) but the code should help you see the problem.
    Code:
    typedef struct DirectionalLight
    {
        int id;
        struct Type4D colorlight;
        struct Type3D lightdirection;    
        float intensity;
        struct DirectionalLight *next;    
    } Directional;
    
    typedef struct LightStruct
    {
        int id;
        struct AmbientLight *ambient;
        struct DirectionalLight *directional;
        struct PointLight *point;
        
    } Lights;

    and the loading code:
    Code:
    if (lighttype == DIRECTIONAL_LIGHT) //1
        {
            struct DirectionalLight *dirlight = lights->directional;
            
            while(dirlight != NULL)
                dirlight = dirlight->next;
            dirlight = (struct DirectionalLight *)malloc(sizeof(struct DirectionalLight));

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by alansandbucket
    i just want to assign some allocated memory to a pointer to the next available light structure. but what im doing right now is im just pointing a pointer that points to the next member of the list to the allocated memory. i want to point the pointer that the first pointer points to to the memory. sorry its kinda hard to say (:P) but the code should help you see the problem.
    Are you trying to append a Directional node to the lights->directional linked list?
    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

  3. #3
    Registered User
    Join Date
    May 2014
    Posts
    15
    yes i am trying to append a node to the lights->directional linked list. if you look at the bottom code i called the loading code you might be able to figure out my problem. first i cycle through the nodes until i hit the first empty node. but once i have "dirlight" pointing directly at the NULL/(first empty) node i dont know of a way to elegantly assign the memory needed for the structure to the node that "dirlight" directly points to. i hope that is clearer.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by alansandbucket
    yes i am trying to append a node to the lights->directional linked list.
    Great. Keep this in mind next time: "The most important single aspect of software development is to be clear about what you are trying to build." -- Bjarne Stroustrup

    Quote Originally Posted by alansandbucket
    if you look at the bottom code i called the loading code you might be able to figure out my problem. first i cycle through the nodes until i hit the first empty node. but once i have "dirlight" pointing directly at the NULL/(first empty) node i dont know of a way to elegantly assign the memory needed for the structure to the node that "dirlight" directly points to. i hope that is clearer.
    I suggest that you create a struct type for these linked lists. Objects of this struct type will have two member pointers: a pointer to the head of the linked list and a pointer to the tail of the linked list. This way, you can efficiently append items to the linked list instead of traversing the entire linked list each time to append an item. Your Lights struct can then store a pointer to an object of this linked list type.

    Furthermore, write functions that do one thing and do it well. For example, you could write a function that has a pointer to an object this linked list struct as a parameter, and a pointer to a Directional object as another parameter, thus allowing you to append a Directional object to the linked list by calling the function with the appropriate arguments.

    If you really want to keep traversing the linked list each time to append an item, then you need to check for dirlight->next != NULL instead of dirlight != NULL, yet you should keep in mind that dirlight could be NULL (i.e., you also need to check for that before you start the loop).
    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. Pointer (*p)[3] Issue
    By Bob Kang in forum C Programming
    Replies: 7
    Last Post: 07-04-2015, 10:53 PM
  2. Issue with Pointer
    By YannB in forum C Programming
    Replies: 1
    Last Post: 02-27-2014, 02:33 AM
  3. Pointer to Pointer manipulation issue
    By workisnotfun in forum C Programming
    Replies: 5
    Last Post: 10-16-2012, 11:31 AM
  4. pointer issue
    By Nuggnugg in forum C Programming
    Replies: 15
    Last Post: 03-31-2011, 01:52 PM
  5. not a pointer issue
    By ~Kyo~ in forum C++ Programming
    Replies: 45
    Last Post: 04-21-2010, 05:51 PM

Tags for this Thread