# Thread: Help with Struct and pointers

1. ## 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. 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.

3. I am new to programming and have a struct for an array list.
O_o

Overshot.

Soma

4. Originally Posted by min2max
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. 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. Originally Posted by min2max
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.

7. Originally Posted by min2max
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
Originally Posted by min2max
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.

8. 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. MutantJohn, I do not see your point.

10. Originally Posted by min2max
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. 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.