[英]C structure pointer
I'm trying to assign a structure to pointer by using another pointer 我正在尝试通过使用另一个指针将结构分配给指针
typedef struct cat Category;
Category{
///some stuff here
};
Category *categoryList;
Category *ap = &categoryList;
*ap = (Category *)malloc(sizeof(Category));
I get this: 我得到这个:
error: incompatible types when assigning to type 'Category' from type 'struct Category *'
What am I doing wrong? 我究竟做错了什么?
You are assigning pointer to pointer to structure Category to pointer to structure Category.. Yeah that's a lot of 'tos'. 您正在将指向结构类别的指针分配给结构类别的指针..是的,这有很多“ tos”。 So how to fix it?
那么如何解决呢?
typedef struct cat Category;
Category{
///some stuff here
};
// declare variable - pointer to category
Category *categoryList;
// store address of variable categoryList, which is pointer in variable ap,
// which needs to be of type Category ** since it is pointer to pointer!
Category **ap = &categoryList;
// assign pointer to mallocated address to categoryList whose address is
// accessed - dereferenced via *ap
*ap = malloc(sizeof(Category));
Also please note, that you shouldn't cast return value of malloc. 还请注意,您不应该转换malloc的返回值。 For explanation please refer to this answer
有关说明,请参阅此答案
A variable declared with a pointer data type (ie, Category*
) has a star counter, the number of Asterisks on the data type. 用指针的数据类型(即,声明的变量
Category*
)具有星形计数器的数目星号上的数据类型。
That's it, the variable x
: 就是这样,变量
x
:
typedef struct category_t {
// ...
} Category;
Category* x;
x
has a star counter = 1 because you have just one star. x
的星号计数器= 1,因为您只有一颗星。
Then remember this: 然后记住这一点:
&
operator increases the counter by one. &
运算符可使计数器增加一。 *
operator reduces the counter by one. *
运算符可将计数器减一。 Then, the expressions: 然后,这些表达式:
&x
has a star counter = 2, and &x
的星号计数器= 2,并且 *x
has a star counter = 0. *x
的星号计数器= 0。 You always need to match the datatype, including the star counter. 您始终需要匹配数据类型,包括星号计数器。
On your example you have two errors: 在您的示例中,您有两个错误:
Category *categoryList; // 1
Category *ap = &categoryList; // 2
*ap = (Category *)malloc(sizeof(Category)); // 3
On line 2 your variable ap
has a star counter = 1, yet the expression &categoryList
has a star counter = 2; 在第2行中,变量
ap
的星计数器为1,而表达式&categoryList
的星计数器为2; this is an invalid assigment. 这是无效的分配。
On line 3 your variable ap
has again star counter = 1, yet the expression *ap
has a star counter = 0 and you're assigning the result of malloc
which has a star counter = 1. 在第3行中,变量
ap
再次具有star计数器= 1,但是表达式*ap
的star计数器= 0,并且您要分配具有star counter的malloc
结果= 1。
#include <stdlib.h>
#include <stdio.h>
typedef struct {
// Some stuff here
} Category;
int main() {
// *categoryList doesn't initialize a struct, it only creates a pointer to one. Leave the * off.
Category categoryList;
// Assign the address of the categoryList struct to ap (a pointer)
Category *ap = &categoryList;
ap = malloc(sizeof(Category)); // Try to never cast malloc.
return 0;
}
Taking issues one at a time... 一次处理一个问题...
Category *categoryList;
The type of the variable categoryList
is Category *
( pointer to Category
). 变量
categoryList
为Category *
(指向Category
指针)。
Category *ap = &categoryList;
The type of the variable ap
is Category *
, but the type of the expression &categoryList
is Category **
, or "pointer to pointer to Category
". 变量
ap
的类型为Category *
,而表达式 &categoryList
为Category **
或“指向Category
指针”。 This is your first type mismatch; 这是您的第一种类型不匹配; the assignment should be written
作业应写成
Category *ap = categoryList;
Finally, 最后,
*ap = (Category *)malloc(sizeof(Category));
The expression *ap
has type Category
; 表达式
*ap
具有类型Category
; malloc
returns a void *
, which you are casting to Category *
. malloc
返回一个void *
,您将其转换为Category *
。 You cannot assign a pointer value to a non-pointer type, which is where your compiler error is coming from. 您不能将指针值分配给非指针类型,这是您的编译器错误出处。 That line should be written
那行应该写
ap = malloc( sizeof *ap );
The cast is unnecessary 1 , and since the expression *ap
has type Category
, sizeof *ap
will give the same result as sizeof (Category)
2 . 强制转换1是不必要的,并且由于表达式
*ap
类型为Category
,因此sizeof *ap
的结果与sizeof (Category)
2相同 。
sizeof
is an operator, not a function;
sizeof
是运算符,不是函数;
the only time parentheses are required is when the operand is a type name.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.