-
array of struct pointers
hi there,
i am trying to create an array of struct pointers, set each element in the array to null, and (later) then assign struct values to certain positions within the array... and i am getting confused with accessing the
elements in the array ...
if i have an array of char pointers...
Code:
char *charPtr = malloc(sizeof(char*)*5);
and go
Code:
charPtr[2] = 'b';
printf(" the char pointer is :%c: \n",charPtr[2]);
i get
Code:
the char pointer is :b:
but if i go...
Code:
typedef struct indexType
{
nodePtr head;
nodePtr tail;
int count;
} IndexType;
typedef IndexType * IndexTypePtr;
int main()
{
IndexTypePtr tester = NULL;
tester = malloc(sizeof(IndexTypePtr)*10);
tester[2] = NULL;
return 0;
}
I get an assignment type mismatch ?
Code:
assignment type mismatch:
struct indexType {pointer to struct node {..} head, pointer to struct node {..} tail,
int count} "=" int
is this because i am trying to assign null to something that has elements like head, tail
etc...
but if that is true then how does
Code:
IndexTypePtr tester = NULL;
work ??
thanks
-
Quote:
Code:
char *charPtr = malloc(sizeof(char*)*5);
OK, there's a problem here. You say you're making an array of char pointers (and we'll pretend that a pointer is an array for the sake of this discussion). But charPtr is, in that case, an array of char, not an array of pointers. As such, you should not be using sizeof(char *) to size the elements, but sizeof(char). Of course, sizeof(char) is 1 so it's redundant to multiply by it, but it's OK for illustration.
What you really want for an array of char pointers is something like:
Code:
char **a = malloc(sizeof *a * 5);
You can use sizeof(char *) there if you want, but I prefer not scattering type names around. Now you have an array of char pointers, and you can do this sort of thing:
Code:
a[0] = NULL;
a[1] = "string";
/* a[1][0] is 's' now */
The same holds for your array of struct pointers. The way you have it is an array of structs, and so tester[2] is a struct, not a pointer. Again, you want a pointer to a pointer:
Code:
struct foo **a = malloc(sizeof *a * 5); /* Typedefing a pointer makes things unclear, in my opinion */
a[0] = NULL; /* perfectly OK now */
The thing with this, though, is that for each element you have to point it somewhere useful. That could be via a malloc():
Code:
a[0] = malloc(sizeof *a[0]);
a[0]->head = whatever;
-
sorry to interrupt but whats a node?
-
It comes from the word "knot" in Latin. In this case, I'd say that it is a "knot" or "junction" in a linked list structure (or similar).
Linked list - Wikipedia, the free encyclopedia
--
Mats
-
thanks cas, very helpful response :)
i got confused there for a while.... ;p
notation is exactly what i was trying to do and now it works great :)
very cool i can now add any sort of data structure ,bst,linked lists etc... from this
(index / array not really sure what to call it ) ... which i will create a hash for the relative positions ... exciting ..
and yeah node here is part of a bst or list..
thanks again to all of the response