C 是否将其视为全局变量?结构和链表
我搜索了一下,但找不到我问题的严格答案。 我必须使用数据的动态结构来编写项目,但是我不能使用全局变量。我想使用链表的链表。我在论坛()上找到了这段代码,它对我来说非常有用,但我不确定这部分是否被视为全局变量C 是否将其视为全局变量?结构和链表,c,struct,linked-list,global-variables,C,Struct,Linked List,Global Variables,我搜索了一下,但找不到我问题的严格答案。 我必须使用数据的动态结构来编写项目,但是我不能使用全局变量。我想使用链表的链表。我在论坛()上找到了这段代码,它对我来说非常有用,但我不确定这部分是否被视为全局变量 typedef struct sToy { char name[50]; struct sToy *next; }tToy; typedef struct sChild { char name[50]; tToy *firstToy; struct
typedef struct sToy {
char name[50];
struct sToy *next;
}tToy;
typedef struct sChild {
char name[50];
tToy *firstToy;
struct sChild *next;
} tChild;
我指的是“tToy”和“tChild”的事。库斯·海德已经被正式宣布
谢谢你的帮助 正如@WhozCraig所说,这些是结构类型别名,而不是全局变量。下面是一个示例代码,演示如何将这些结构用作全局变量或局部变量:
typedef struct sToy {
char name[50];
struct sToy *next;
}tToy;
typedef struct sChild {
char name[50];
tToy *firstToy;
struct sChild *next;
} tChild;
tToy Gtoy;//global
tChild Gchild;//global
int main() {
tToy Ltoy;//local
tChild Lchild;//local
}
您还可以省略“sToy”和“sChild”,只留下“typedef struct{…}alias;”就像@WhozCraig所说的,这些是结构类型别名,而不是全局变量。下面是一个示例代码,演示如何将这些结构用作全局变量或局部变量:
typedef struct sToy {
char name[50];
struct sToy *next;
}tToy;
typedef struct sChild {
char name[50];
tToy *firstToy;
struct sChild *next;
} tChild;
tToy Gtoy;//global
tChild Gchild;//global
int main() {
tToy Ltoy;//local
tChild Lchild;//local
}
您还可以省略“sToy”和“sChild”,只留下“typedef struct{…}alias;”您只需要提供一个局部变量-指向子列表头的指针和一些对列表进行操作的函数 这里有两个示例函数-一个附加子对象,另一个将玩具附加到子对象的玩具列表中。当然,您需要更多,但这只是一个开始:
tChild *appendChild(tChild **head, const char *name)
{
tChild *result = NULL;
tChild *current = *head;
while (current != NULL && current ->next != NULL) current = current->next;
if ((result = malloc(sizeof(tChild))) != NULL)
{
if (*head == NULL)
{
*head = result;
}
else
{
current->next = result;
if (name != NULL) strncpy(result->name, name, sizeof((tChild) {0}.name));
result->name[sizeof((tChild) { 0 }.name) - 1] = 0; //just in case
result->firstToy = NULL;
result->next = NULL;
}
}
return result;
}
tToy *appendToy(tChild *child, const char *name)
{
tToy *result = NULL;
tToy *current;
if (child != NULL)
{
current = child->firstToy;
while (current != NULL && current->next != NULL) current = current->next;
if ((result = malloc(sizeof(tToy))) != NULL)
{
if (child->firstToy == NULL)
{
child ->firstToy = result;
}
else
{
current->next = result;
if (name != NULL) strncpy(result->name, name, sizeof((tToy) { 0 }.name));
result->name[sizeof((tToy) { 0 }.name) - 1] = 0; //just in case
result->next = NULL;
}
}
}
return result;
}
以及如何从总体上开始
int main(int argc, char **argv)
{
tChild *head = NULL;
if (appendChild(&head, "John") == NULL)
printf("Error - appendingh the child failed\n");
if (appendChild(&head, "Mark") == NULL)
printf("Error - appendingh the child failed\n");
.....
您只需要提供一个局部变量-指向子列表头的指针和一些对列表进行操作的函数 这里有两个示例函数-一个附加子对象,另一个将玩具附加到子对象的玩具列表中。当然,您需要更多,但这只是一个开始:
tChild *appendChild(tChild **head, const char *name)
{
tChild *result = NULL;
tChild *current = *head;
while (current != NULL && current ->next != NULL) current = current->next;
if ((result = malloc(sizeof(tChild))) != NULL)
{
if (*head == NULL)
{
*head = result;
}
else
{
current->next = result;
if (name != NULL) strncpy(result->name, name, sizeof((tChild) {0}.name));
result->name[sizeof((tChild) { 0 }.name) - 1] = 0; //just in case
result->firstToy = NULL;
result->next = NULL;
}
}
return result;
}
tToy *appendToy(tChild *child, const char *name)
{
tToy *result = NULL;
tToy *current;
if (child != NULL)
{
current = child->firstToy;
while (current != NULL && current->next != NULL) current = current->next;
if ((result = malloc(sizeof(tToy))) != NULL)
{
if (child->firstToy == NULL)
{
child ->firstToy = result;
}
else
{
current->next = result;
if (name != NULL) strncpy(result->name, name, sizeof((tToy) { 0 }.name));
result->name[sizeof((tToy) { 0 }.name) - 1] = 0; //just in case
result->next = NULL;
}
}
}
return result;
}
以及如何从总体上开始
int main(int argc, char **argv)
{
tChild *head = NULL;
if (appendChild(&head, "John") == NULL)
printf("Error - appendingh the child failed\n");
if (appendChild(&head, "Mark") == NULL)
printf("Error - appendingh the child failed\n");
.....
这些是类型别名;不是全局变量。这里没有变量。这些是类型别名;不是全局变量。这里没有变量。