指向结构数组的指针? [英] Pointer to array of structs?
问题描述
还有一些关于malloc和指针的问题。
我做了一个结构。现在我想把一个指针指向一个带有4
指向这个结构的数组。
#include< stdlib.h>
#include< stdio.h>
typedef struct _tnode_t {
void * content;
struct _tnode_t * kids;
} tnode_t;
int main(无效)
{
tnode_t * array;
array = malloc(sizeof(tnode_t)* 4);
array [3] = NULL;
返回0;
}
据我所知数组指向由4个tnode_t结构组成的
空间的数组的第一个元素。但为什么我不能将索引3处的元素初始化为
为NULL?
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?
推荐答案
Paminu写道:
还有一些关于malloc和指针的问题。
我做了一个结构。现在我想把一个指针指向一个带有4个指向这个结构的指针的数组。
#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 = malloc(sizeof * array * 4);
显示你已经分配了足够的空间来指向你的变量`array'指向的
。
array [3 ] = NULL;
返回0;
}
据我所知数组指向由4个tnode_t结构组成的
空间组成的数组的第一个元素。但是为什么我不能将索引3处的元素初始化为NULL?
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?
因为array [3]是`tnode_t'',而不是指针到'tnode_t'' - 和
NULL是一个指针常量。
如果你想要一个*指针*的数组到tnode_t,你会写:
tnode_t ** array = malloc(sizeof * array * 4);
并为每个元素分配足够的空间用于tnode_t需要。
HTH,
- g
-
Artie Gold - 奥斯汀,德克萨斯州
http://goldsays.blogspot.com
< a rel =nofollowhref =http://www.cafepress.com/goldsaystarget =_ blank> http://www.cafepress.com/goldsays
如果你没有什么可隐瞒的,你就没有尝试!
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!"
Paminu写道:
仍有一些问题malloc和指针。
我做了结构。现在我想把一个指针指向一个带有4个指向这个结构的指针的数组。
#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;
返回0;
}
据我了解数组指向由4个tnode_t结构组成的
空间组成的数组的第一个元素。但是为什么我不能将索引3处的元素初始化为NULL?
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?
* array [4]。如果你想要创建一个动态分配的指向tnode_t结构的
指针数组,你会希望你的指针是
类型指向指向tnode_t的指针:
tnode_t **数组;
sizeof(tnode_t)* 4是存储4个tnode_t结构所需的大小。
sizeof (tnode_t *)* 4将评估为4
指向tnode_t所需的字节数。
Robert Gamble
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
Robert Gamble写道:
Robert Gamble wrote:
Paminu写道:
还有一些关于malloc和指针的问题。 />
我做了一个结构。现在我想把一个指针指向一个带有4个指向这个结构的指针的数组。
#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;
返回0;
}
据我了解数组指向包含4个tnode_t结构的空间的数组的第一个元素。但为什么我不能将
索引3处的元素初始化为NULL?
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;创建一个指向tnode_t结构的指针。如果你想要一个指向tnode_t结构的4个指针的数组,你可以使用tnode_t
* array [4]。
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].
如果我这样做似乎有效:
int main(无效)
{
tnode_t * array [4];
tnode_t *测试;
test = malloc(sizeof(tnode_t));
test-> content =" test";
test-> ; kids = NULL;
array [0] = test;
array [1] = NULL;
//等。
printf("%s \ n",array [0] - > content);
返回0; < br $>
}
是你的意思吗?
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?
这篇关于指向结构数组的指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!