Thread: Help with Struct and pointers

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    17

    Lightbulb Help with Struct and pointers

    I am new to programming and have a struct for an array list. I am having a hard time visualizing what this struct actually looks like if I were to map it out with pen and paper...If anyone could help me with this that would be great and I would have a better understanding of what Im actually doing with this struct...Thanks so much!

    Code:
    typedef struct ArrayList
    {
            char **array;
            int size;
            int capacity;
    }ArrayList;

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    First check this simple example on structs.

    New to programming and you have a list of arrays..? Hmm, anyway, the char** array, is actually a 2D array, if this can help.

    Welcome to the forum.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I am new to programming and have a struct for an array list.
    O_o

    Overshot.

    Start with a simple structure and build on that foundation.

    Soma

  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by min2max View Post
    I am new to programming and have a struct for an array list. I am having a hard time visualizing what this struct actually looks like if I were to map it out with pen and paper...If anyone could help me with this that would be great and I would have a better understanding of what Im actually doing with this struct...Thanks so much!
    So, the way I visualize a data structure is, I draw a circle lol. When I took CS in school, my professor called them 'nodes' and so when we had to build a linked list, I drew circle connected by lines in a row.

    And I think it's a decent analogy. A structure in C is really just a random collection of variables and by random, I really mean that they can be anything, ints, arrays, chars, you name it. If you can declare it, you can declare it in a structure.

    So each structure is a self-contained circle that contains whatever you want. If you contain a pointer to the same structure within your structure, you can link them!

    It'd be like,

    Code:
    struct node {
    
        int value;
        struct node *next;
    };
    The 'next' pointer allows you to link your structures and if you had two pointers to structs, A and B, you'd go A->next = B and voila, your data is linked!

    So then you could draw circle or squares or any shape you want just so long as it's closed and the links would be lines connecting them together. Does any of this help or just make it all worse?

  5. #5
    Registered User
    Join Date
    May 2013
    Posts
    17
    Yes this helped ! Thanks. When I said I was new to programming I meant I haven't done it in awhile so I needed to refresh on some things lol sorry if I didn't clarify that correctly.

    So I guess I can just look at the char **array as a 2d array inside of the struct??

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by min2max View Post
    Yes this helped ! Thanks. When I said I was new to programming I meant I haven't done it in awhile so I needed to refresh on some things lol sorry if I didn't clarify that correctly.

    So I guess I can just look at the char **array as a 2d array inside of the struct??
    You need two mental models.

    We can have

    Code:
    struct employee
    {
      char name[32];
      int id;
      float salary;
    };
    
    /or */
    
    struct employee2
    {
      char *name;
      int id;
      float salary;
    };
    For a lot of purposes, the two methods are exactly the same. printf("%s %d %6.2f\n"employee.name, employee.id, employee.salary); will give you the same result.
    But the way the data is stored in memory is different. In the first case we have a fixed field

    32 bytes [name]
    4 bytes [id]
    4 bytes [salary]

    in the second case we have a pointer field

    4 or 8 bytes [name pointer]
    4 bytes [id]
    4 bytes [salary]

    pointers will be 4 bytes on a 32 bit system, 8 bytes if we have 64 bits.
    The pointer will point to memory somewhere else in the computer. Typically this will be allocated with malloc() so the structure will "own" the memory. But it could be a big list of names somewhere.

    You've got to toggle between thinking of name as a field containing the employee's name, and a pointer to some memory somewhere, depending on what you are doing.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by min2max View Post
    Yes this helped ! Thanks. When I said I was new to programming I meant I haven't done it in awhile so I needed to refresh on some things lol sorry if I didn't clarify that correctly.
    No, you did not state it correctly. Now you did
    Quote Originally Posted by min2max View Post
    So I guess I can just look at the char **array as a 2d array inside of the struct??
    I can not see why McLean is using another struct, other than the one you got. Then answer is yes. It is a 2D array, but you have to allocate memory for it dynamically, like in this example. Combine this with the other example in my first post, in order to access properly the 2D array.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    So wait std10093, what else do people use for debugging? 'Cause I'm with you. I think I come up with the best debugging messages ever. Although some of them slip and my TA in school saw all my phallus flags.

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    MutantJohn, I do not see your point.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  10. #10
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by min2max View Post
    So I guess I can just look at the char **array as a 2d array inside of the struct??
    From the declaration, all you know is that char **array means that "array" is a pointer to a pointer to a character. It's possible that "array" is a pointer to one of many pointers in an array of pointers. Normally "array" would point to the first pointer of an array of pointers, but it could point to a pointer in the middle or the end of an array of pointers.

  11. #11
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Lol std, such grumpy cat. I know people will use gdb for debugging but I don't know any others because I don't debug with gdb or anything else, I use printf's too. From your signature I realized that I wasn't alone in using printf's to find exactly where my code was crashing in lieu of something like gdb. Granted, I use valgrind but Idt that's a debugger. Does the -Wall compile flag count either?

    But I'm sorry, I thought it was a funny/neat sig but these things are hard to convey over the internet without sounding sarcastic. Oh, and a lot of my debugging printf's are, um, inappropriate and once I sent a TA of mine some code that had one of those. She laughed though, so it was okay. Maybe the C board isn't the best for story time but how dry would this board be otherwise? We are all human, you know.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers and struct
    By rrlangly in forum C Programming
    Replies: 5
    Last Post: 06-07-2011, 02:19 AM
  2. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  3. Want help using struct pointers
    By burkoJames in forum C Programming
    Replies: 2
    Last Post: 05-13-2010, 10:59 PM
  4. pointers to struct - help please
    By seminolas in forum C Programming
    Replies: 2
    Last Post: 12-22-2008, 01:03 AM
  5. Assigning pointers from pointers in struct.
    By Brian in forum C Programming
    Replies: 2
    Last Post: 10-18-2003, 04:30 AM

Tags for this Thread