Thread: Pointer and Structures..

  1. #1
    Registered User
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    6

    Pointer and Structures..

    I'm working on a maze solving program right now... But the problem is, I dont know how to access a pointer to pointer to struct data.

    Something like this

    struct path *solution_path in function A
    it points to struct tile *tile_list in another struct which points to:

    Code:
    struct tile {
        /*position of the tile on the maze*/
        int x;
        int y;
        
        /*current value of the tile*/
        int value;
    };
    I want to store values on 'value'... so how do I access value?
    I've been looking over the internet on how to do it... but I can't find any...

    doing something like solution_path->tile_list[].value doesn't seem to work...

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > doing something like solution_path->tile_list[].value doesn't seem to work...
    Something like seems?
    How is this accurate information?

    Did you try
    solution_path->tile_list->value
    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
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CurlyBrace
    struct path *solution_path in function A
    it points to struct tile *tile_list in another struct which points to:
    You are probably just saying things wrong. If solution_path is a pointer to struct path, then it does not make sense for it to point to tile_list when tile_list is a pointer to struct tile.

    My guess is that you have something like this:
    Code:
    struct tile {
        /*position of the tile on the maze*/
        int x;
        int y;
         
        /*current value of the tile*/
        int value;
    };
    
    struct path {
        struct tile *tile_list;
    };
    Now, given:
    Code:
    struct path *solution_path;
    You want to access: solution_path->tile_list->value or solution_path->tile_list[i].value, where i is some valid index.
    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

  4. #4
    Registered User
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    6
    After trying that out, it does work but it crashes midway while assigning values to solution_path->tile_list[counter].value not sure what I'm doing wrong
    Code:
    struct path *solution_path;
    xy = width*height;
        
    solution_path = malloc(sizeof(int));
    solution_path->tile_list = malloc(sizeof(int)*xy);
    
    
    counter =0;    
    for(i=0;i<height;i++)    {
          for(j=0;j<width;j++)
          {
            solvemap[j][i] = (maze_data.grid+counter)->value; 
            counter++;
          }
        }  
    
    
        counter = 0;
        for(i=0;i<height;i++)
         {
             for(j=0;j<width;j++)
             {
               solution_path->tile_list[counter].value = solvemap[j][i];
               printf("%d", solution_path->tile_list[counter].value);
               counter++;
             }  
           printf("\n");
    
         }
    Last edited by CurlyBrace; 10-31-2012 at 10:45 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is wrong:
    Code:
    solution_path = malloc(sizeof(int));
    solution_path is a pointer to struct path, therefore the space that should be allocated for what it points to should be sizeof(struct path), at least. We can do this by making use of the pointer itself:
    Code:
    solution_path = malloc(sizeof(*solution_path));
    Likewise, this is wrong:
    Code:
    solution_path->tile_list = malloc(sizeof(int)*xy);
    It should be:
    Code:
    solution_path->tile_list = malloc(sizeof(solution_path->tile_list[0]) * xy);
    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. Structures and Pointer
    By sjmp in forum C Programming
    Replies: 11
    Last Post: 10-27-2012, 05:23 AM
  2. Some help needed with structures and pointer
    By Engineria in forum C Programming
    Replies: 11
    Last Post: 08-14-2012, 04:43 AM
  3. pointer to array of structures
    By strokebow in forum C Programming
    Replies: 14
    Last Post: 12-09-2006, 02:14 PM
  4. Question about pointer to structures
    By KidMan in forum C Programming
    Replies: 4
    Last Post: 08-23-2006, 07:11 PM
  5. accesing pointer structures
    By xenodvs1 in forum C Programming
    Replies: 3
    Last Post: 04-02-2003, 08:02 PM

Tags for this Thread