473,221 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 software developers and data experts.

Pointer to array of structs?

Still having a few problems with malloc and pointers.

I have made a struct. Now I would like to make a pointer an array with 4
pointers to this struct.

#include <stdlib.h>
#include <stdio.h>
typedef struct _tnode_t {
void *content;
struct _tnode_t *kids;
} tnode_t;
int main(void)
{
tnode_t *array;
array = malloc(sizeof(tnode_t)*4);
array[3] = NULL;
return 0;

}

As I understand "array" point to the first element of an array consisting of
space for 4 tnode_t structs. But why can't I initialize element at index 3
to NULL?
Feb 7 '06 #1
15 3784
Paminu wrote:
Still having a few problems with malloc and pointers.

I have made a struct. Now I would like to make a pointer an array with 4
pointers to this struct.

#include <stdlib.h>
#include <stdio.h>
typedef struct _tnode_t {
void *content;
struct _tnode_t *kids;
} tnode_t;
int main(void)
{
tnode_t *array;
array = malloc(sizeof(tnode_t)*4);
Better (and that may further illustrate the point below)

array = malloc(sizeof *array * 4);

showing you that you have allocated enough space to point to 4 of what
your variable `array' points to.
array[3] = NULL;
return 0;

}

As I understand "array" point to the first element of an array consisting of
space for 4 tnode_t structs. But why can't I initialize element at index 3
to NULL?


Because array[3] is a `tnode_t', not a pointer to a `tnode_t' -- and
NULL is a pointer constant.

If you want an array of *pointers* to tnode_t, you would write:

tnode_t **array = malloc(sizeof *array * 4);

and allocate enough space for a tnode_t for each element as needed.

HTH,
--ag
--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
Feb 7 '06 #2
Paminu wrote:
Still having a few problems with malloc and pointers.

I have made a struct. Now I would like to make a pointer an array with 4
pointers to this struct.

#include <stdlib.h>
#include <stdio.h>
typedef struct _tnode_t {
void *content;
struct _tnode_t *kids;
} tnode_t;
int main(void)
{
tnode_t *array;
array = malloc(sizeof(tnode_t)*4);
array[3] = NULL;
return 0;

}

As I understand "array" point to the first element of an array consisting of
space for 4 tnode_t structs. But why can't I initialize element at index 3
to NULL?


tnode_t *array; creates a pointer to a tnode_t structure. If you want
an array of 4 pointers to tnode_t structs you would use tnode_t
*array[4]. If you want want to create a dynamically allocated array of
pointers to tnode_t structures, you would want your pointer to be of
type "pointer to pointer to tnode_t":
tnode_t **array;

sizeof(tnode_t)*4 is the size required to store 4 tnode_t structures.
sizeof(tnode_t *)*4 will evaluate to the number of bytes required for 4
pointers to tnode_t.

Robert Gamble

Feb 7 '06 #3
Robert Gamble wrote:
Paminu wrote:
Still having a few problems with malloc and pointers.

I have made a struct. Now I would like to make a pointer an array with 4
pointers to this struct.

#include <stdlib.h>
#include <stdio.h>
typedef struct _tnode_t {
void *content;
struct _tnode_t *kids;
} tnode_t;
int main(void)
{
tnode_t *array;
array = malloc(sizeof(tnode_t)*4);
array[3] = NULL;
return 0;

}

As I understand "array" point to the first element of an array consisting
of space for 4 tnode_t structs. But why can't I initialize element at
index 3 to NULL?


tnode_t *array; creates a pointer to a tnode_t structure. If you want
an array of 4 pointers to tnode_t structs you would use tnode_t
*array[4].

It seems to work if I do:

int main(void)
{
tnode_t *array[4];
tnode_t *test;
test = malloc(sizeof(tnode_t));
test->content = "Test";
test->kids = NULL;

array[0] = test;
array[1] = NULL;
// etc.

printf("%s\n", array[0]->content);

return 0;

}
was that what you meant?
Feb 7 '06 #4
Robert Gamble wrote:
Paminu wrote:
Still having a few problems with malloc and pointers.

I have made a struct. Now I would like to make a pointer an array with 4
pointers to this struct.

#include <stdlib.h>
#include <stdio.h>
typedef struct _tnode_t {
void *content;
struct _tnode_t *kids;
} tnode_t;
int main(void)
{
tnode_t *array;
array = malloc(sizeof(tnode_t)*4);
array[3] = NULL;
return 0;

}

As I understand "array" point to the first element of an array consisting
of space for 4 tnode_t structs. But why can't I initialize element at
index 3 to NULL?


tnode_t *array; creates a pointer to a tnode_t structure.

Ok so it will only work if I set array[3] equal to a struct instead of a
pointer to a struct or NULL:

1)
tnode_t tt;
tt.content = "test";
tt.kids = NULL;
tnode_t *array;
array[0] = tt;
this seems to work and make sense. But if I make:

2)
int *p;
p = malloc(sizeof(int)*4);
p[0] = NULL;
this only give the warning: "warning: assignment makes integer from pointer
without a cast".

In 1) if I did: array[0] = NULL; I would get the error:

"error: incompatible types in assignment".

Why the different rules?

Feb 8 '06 #5
Paminu wrote:
Robert Gamble wrote:
Paminu wrote:
Still having a few problems with malloc and pointers.

I have made a struct. Now I would like to make a pointer an array with 4
pointers to this struct.

#include <stdlib.h>
#include <stdio.h>
typedef struct _tnode_t {
void *content;
struct _tnode_t *kids;
} tnode_t;
int main(void)
{
tnode_t *array;
array = malloc(sizeof(tnode_t)*4);
array[3] = NULL;
return 0;

}

As I understand "array" point to the first element of an array consisting
of space for 4 tnode_t structs. But why can't I initialize element at
index 3 to NULL?
tnode_t *array; creates a pointer to a tnode_t structure.

Ok so it will only work if I set array[3] equal to a struct instead of a
pointer to a struct or NULL:

1)
tnode_t tt;
tt.content = "test";
tt.kids = NULL;
tnode_t *array;
array[0] = tt;
this seems to work and make sense. But if I make:

2)
int *p;
p = malloc(sizeof(int)*4);
p[0] = NULL;
this only give the warning: "warning: assignment makes integer from pointer
without a cast".


You still seem to be confusing pointers to types with the types they
point. In the second code snippet you posted you declare a pointer to
integer and then allocate memory for four integers and assign the value
returned by malloc to your pointer. Now you have a dynamic array of
four integers that can be accessed as p[0], p[1], [2] and p[3] or any
equivalent way.

In your next statement you assign a null pointer constant (NULL) to
p[0]. This can be done and is perfectly legal, but it is implementation
defined. Pointers can be implicitly or explicitly converted to integers
and back in an implementation defined manner. The compiler is just
being nice telling you that this is something strange.

In 1) if I did: array[0] = NULL; I would get the error:

"error: incompatible types in assignment".

Why the different rules?


Here the scenario is totally different. array is of type pointer to
tnode_t. array[0] is of type tnode_t which is a struct. Now you're
trying to assign a null pointer constant to a struct and this cannot be
done. There's no implicit conversion between pointers and structs and
if you think of it for a while you'll realize that there is no such
conversion because it simply doesn't make sense.

HTH

Feb 8 '06 #6
Paminu wrote:
Still having a few problems with malloc and pointers.

I have made a struct. Now I would like to make a pointer an array with 4
pointers to this struct.

#include <stdlib.h>
#include <stdio.h>
typedef struct _tnode_t {
void *content;
struct _tnode_t *kids;
} tnode_t;
int main(void)
{
tnode_t *array;
array = malloc(sizeof(tnode_t)*4);
array[3] = NULL;
return 0;

}

As I understand "array" point to the first element of an array consisting of
space for 4 tnode_t structs. But why can't I initialize element at index 3
to NULL?


Because NULL is a pointer value. index 3 stores a tnode_t , not a
pointer to a tnode_t.

You cannot store a pointer in a place that cannot hold a pointer.

Feb 8 '06 #7

"Paminu" <sd**@asd.com> wrote in message
news:ds*********@news.net.uni-c.dk...
Robert Gamble wrote:
Paminu wrote:
Still having a few problems with malloc and pointers.

I have made a struct. Now I would like to make a pointer an array with 4 pointers to this struct.

#include <stdlib.h>
#include <stdio.h>
typedef struct _tnode_t {
void *content;
struct _tnode_t *kids;
} tnode_t;
int main(void)
{
tnode_t *array;
array = malloc(sizeof(tnode_t)*4);
array[3] = NULL;
return 0;

}

As I understand "array" point to the first element of an array consisting of space for 4 tnode_t structs. But why can't I initialize element at
index 3 to NULL?
tnode_t *array; creates a pointer to a tnode_t structure.

Ok so it will only work if I set array[3] equal to a struct instead of a
pointer to a struct or NULL:

1)
tnode_t tt;
tt.content = "test";
tt.kids = NULL;
tnode_t *array;
array[0] = tt;
this seems to work and make sense. But if I make:

2)
int *p;
p = malloc(sizeof(int)*4);
p[0] = NULL;
this only give the warning: "warning: assignment makes integer from

pointer without a cast".

In 1) if I did: array[0] = NULL; I would get the error:

"error: incompatible types in assignment".

Why the different rules?
Different errors.

The pointer to an int, p, can be assigned NULL:
p=NULL;

Or, you can assign the int that p[0] references to zero:
p[0]=0;

Or, you can assign the int that p[1] references to zero:
p[1]=0;

etc...

When you write: p[0] = NULL;

You are attempting to convert a pointer (NULL) to an int.
Rod Pemberton
Feb 8 '06 #8
"Nils O. Selåsdal" wrote:
Paminu wrote:
Still having a few problems with malloc and pointers.

I have made a struct. Now I would like to make a pointer an array with 4
pointers to this struct.

#include <stdlib.h>
#include <stdio.h>
typedef struct _tnode_t {
void *content;
struct _tnode_t *kids;
} tnode_t;
int main(void)
{
tnode_t *array;
array = malloc(sizeof(tnode_t)*4);
array[3] = NULL;
return 0;

}

As I understand "array" point to the first element of an array consisting
of space for 4 tnode_t structs. But why can't I initialize element at
index 3 to NULL?


Because NULL is a pointer value. index 3 stores a tnode_t , not a
pointer to a tnode_t.

You cannot store a pointer in a place that cannot hold a pointer.

Ok I guess that this is correct and identical:

typedef struct _tnode_t {
void *content;
struct _tnode_t *kids;
} tnode_t;

int main(void)
{
tnode_t *array;
array[0].kids = NULL;
array->kids = NULL;
return 0;

}

when I do this: array[i] I get a struct. When I do this: array->kids I get a
pointer to a struct.
Feb 9 '06 #9
Paminu wrote:

....snip...
Ok I guess that this is correct and identical:

typedef struct _tnode_t {
void *content;
struct _tnode_t *kids;
} tnode_t;

int main(void)
{
tnode_t *array;
array[0].kids = NULL;
array->kids = NULL;
return 0;

}

when I do this: array[i] I get a struct. When I do this: array->kids I get a
pointer to a struct.


There's still an issue, you've declared a pointer to a struct, but you
have not initialized it to a valid value (through a call to
malloc/calloc). Your program is dereferencing a pointer that doesn't
point to a valid block of memory and so invokes undefined behaviour.

Feb 9 '06 #10
On Wed, 08 Feb 2006 09:34:30 +0100, Paminu <sd**@asd.com> wrote:
Robert Gamble wrote:
Paminu wrote:
Still having a few problems with malloc and pointers.

I have made a struct. Now I would like to make a pointer an array with 4
pointers to this struct.

#include <stdlib.h>
#include <stdio.h>
typedef struct _tnode_t {
void *content;
struct _tnode_t *kids;
} tnode_t;
int main(void)
{
tnode_t *array;
array = malloc(sizeof(tnode_t)*4);
array[3] = NULL;
return 0;

}

As I understand "array" point to the first element of an array consisting
of space for 4 tnode_t structs. But why can't I initialize element at
index 3 to NULL?
tnode_t *array; creates a pointer to a tnode_t structure.

Ok so it will only work if I set array[3] equal to a struct instead of a
pointer to a struct or NULL:

1)
tnode_t tt;
tt.content = "test";
tt.kids = NULL;
tnode_t *array;
array[0] = tt;


tt is a tnode_t. array is a pointer to a tnode_t. array is not
initialized to point anywhere. array[0] would be the first tnode_t it
pointed if it did point anywhere. That is why your program is
syntactically correct. Some compilers warn you if you attempt to use
an uninitialized variable but even without the warning it is still
undefined behavior. If you added a call to malloc before using
array[0], array would then point to one or more structures and the
assignment would be fine.


this seems to work and make sense. But if I make:
Seeming to work is just leading down the path of murphy's law.
2)
int *p;
p = malloc(sizeof(int)*4);
p[0] = NULL;
this only give the warning: "warning: assignment makes integer from pointer
without a cast".
p is a pointer to int. p[0] is the first uninitialized int it points
to. NULL is not a suitable value for int. Hence the warning. Do not
think of it as "only". Your program is doing something much different
than you expect.

In 1) if I did: array[0] = NULL; I would get the error:

"error: incompatible types in assignment".
There is an implementation defined conversion from void* (the type of
NULL) to int. There is no such conversion from int* to struct.

Why the different rules?


Different types.
Remove del for email
Feb 10 '06 #11
Hi,
I have to say another problem that do not forget to free() them when
you allocated a pointer. Maybe there are no errors ,but that will cause
memory leak . ( Maybe your OS is stronger than mine :)

Feb 10 '06 #12
Ro*****@gmail.com wrote:
Hi,
I have to say another problem that do not forget to free() them when
you allocated a pointer. Maybe there are no errors ,but that will cause
memory leak . ( Maybe your OS is stronger than mine :)


What are you talking about? Who, or what are "them"? Etc...

Please quote what and who you're replying to. Read this:

"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>

--
BR, Vladimir

My OS can beat the hell out of yOurS.

Feb 10 '06 #13
Thanks for mentioning.
I am sorry about that, I am new here.
And I will pay attention to that.

Vladimir S. Oka wrote:
Ro*****@gmail.com wrote:
Hi,
I have to say another problem that do not forget to free() them when
you allocated a pointer. Maybe there are no errors ,but that will cause
memory leak . ( Maybe your OS is stronger than mine :)


What are you talking about? Who, or what are "them"? Etc...

Please quote what and who you're replying to. Read this:

"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>

--
BR, Vladimir

My OS can beat the hell out of yOurS.


Feb 10 '06 #14
Ro*****@gmail.com wrote:
Thanks for mentioning.
I am sorry about that, I am new here.
And I will pay attention to that.


Good, thank you. Also, don't top-post.

--
BR, Vladimir

43rd Law of Computing:
Anything that can go wr
fortune: Segmentation violation -- Core dumped

Feb 10 '06 #15
On Thu, 09 Feb 2006 08:18:53 +0100, Paminu <sd**@asd.com> wrote:
<snip>
Ok I guess that this is correct and identical:

typedef struct _tnode_t {
void *content;
struct _tnode_t *kids;
} tnode_t;
Most identifiers beginning with underscore are reserved to the
implementation and you shouldn't use them. This reservation is not
always enforced, indeed rarely is, but you are just asking for trouble
sometime in the future when it will be (maximally) incovenient. (There
are a few categories that are allowed, but your case here is not one
of them, and rather than remember the exact rules it's easier to avoid
all of them.)

Note it is perfectly legal to use the same identifier for the struct
tag and the typedef-name; they are in separate namespaces. If you
don't wish to, and do want to have both, the usual practice is to put
a suffix on one, either like typedef struct tnode { ... } tnode_t;
or like typedef struct tnode_s { ... } tnode;
int main(void)
{
tnode_t *array;
array[0].kids = NULL;
array->kids = NULL;
Yes. Given that the type is defined correctly (and as another reply
points out the pointer is initialized validly), then:
p -> e is defined as (*p) . e
p[0] is the same as *p
p[0] . e is the same as (*p) . e
return 0;

}

when I do this: array[i] I get a struct. When I do this: array->kids I get a
pointer to a struct.


But note that if array is actually an array, as the name implies,
array[i] allows you to access elements other than array[0], and
array[i].kids gives you the pointer in the i'th element. array->kids
gives you always and only array[0].kids.
- David.Thompson1 at worldnet.att.net
Feb 20 '06 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: sathyashrayan | last post by:
The standard confirms that the following initialization of a struct struct node { --- --- } struct node var = {NULL};
10
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr;...
1
by: mrhicks | last post by:
Hello all, I need some advice/help on a particular problem I am having. I have a basic struct called "indv_rpt_rply" that holds information for a particular device in our system which I will...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
13
by: aegis | last post by:
The following was mentioned by Eric Sosman from http://groups.google.com/group/comp.lang.c/msg/b696b28f59b9dac4?dmode=source "The alignment requirement for any type T must be a divisor of...
7
by: Ray Dillinger | last post by:
Hi. I'm having a problem and I really want to understand it. Here's the situation: I have an array of pointers, and each pointer is the head of a linked list of structs. The structs are...
156
by: Lame Duck | last post by:
Hi Group! I have a vector<floatvariable that I need to pass to a function, but the function takes a float * arguement. That's OK, I can convert by doing &MyVector.front(), but when I get back a...
14
by: Szabolcs Borsanyi | last post by:
Deal all, The type typedef double ***tmp_tensor3; is meant to represent a three-dimensional array. For some reasons the standard array-of-array-of-array will not work in my case. Can I...
0
by: mjaaland | last post by:
Hi! I've been working with DLLimports passing structs and various other parameters to unmanaged code. I had problems earlier sending pointer to structs to the unmanaged code, and this forum solved...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.