这是初始化指向struct的指针的正确方法吗?非常感谢。
如何以正确的方式创建指向struct的指针这是初始化指向struct的指针的正确方法吗?非常感谢。,c,pointers,C,Pointers,如何以正确的方式创建指向struct的指针 typedef struct account{ char name[80]; int acct_year; }account; int main { account *accountant;// is this the proper way? } 通常的方法是使用malloc()系统调用来分配内存块 系统堆上的结构类型的大小为零。您需要检查一下 如果你记性不好的话,那就真的发生了 account * accountant;
typedef struct account{
char name[80];
int acct_year;
}account;
int main
{
account *accountant;// is this the proper way?
}
通常的方法是使用malloc()系统调用来分配内存块 系统堆上的结构类型的大小为零。您需要检查一下 如果你记性不好的话,那就真的发生了
account * accountant;
if ((accountant = malloc ( sizeof(struct account))) == NULL)
{
errno = ENOMEM;
return some error token.
}
...continue knowing the pointer has been initialised.
完成后,确保将块释放回系统
free (accountant);
我在代码中写了注释来解释事情。 如果仍然困惑,请告诉我
#包括
//这是strncpy所需要的
#包括
//这是malloc所需要的
#包括
结构帐户{
字符名[80];
国际会计年度;
};
int main()
{
//Account类型变量的正常声明
结构帐户用户帐户;
//使用strncpy比使用strcpy更安全。了解溢出缓冲区攻击。
char*userName=“Alex”;
strncpy(userAccount.name,userName,sizeof(userName));
userAccount.acct_year=4;
printf(“用户帐户:%s%d\n”,userAccount.name,userAccount.acct\u year);
//创建一个指针来访问我们在上面声明和初始化的结构;
结构帐户*pUserAccount;
//将上面变量的地址传递到指针内部
pUserAccount=&userAccount;
//我们可以从指针替换userAccount中的值
会计科目->会计年度=5;
//比较以查看我们使用指针更改了userAccount中的值
printf(“指针用户帐户:%s%d\n”,pUserAccount->name,pUserAccount->act\u year);
printf(“用户帐户:%s%d\n”,userAccount.name,userAccount.acct\u year);
//让我们创建一个指针,并从堆内存中给它一些空间
结构帐户*pAdminAccount;
pAdminAccount=malloc(sizeof(struct Account));
//我们总是检查,因为我们可能会耗尽内存或出现故障
if(pAdminAccount!=NULL){
//由于我的懒惰,我正在从pAdminAccount中的userAccount复制名称
strncpy(pAdminAccount->name,userAccount.name,sizeof(userAccount.name));
pAdminAccount->act_year=6;
printf(“指针堆管理员帐户:%s%d\n”,pAdminAccount->name,pAdminAccount->act\u year);
//不使用内存时,请始终释放内存。
免费(pAdminAccount);
}
返回0;
}
注意:我已经很久没有做C了,所以请仔细检查我在这里解释的内容
说明:
因此,有不同的方法可以使用结构
第一种使用的方法与任何其他类型一样,如int、char、bool等
如果我做了inta;a=4代码>,堆栈中提供的内存将在声明它的块内可用。因此,如果您在函数中创建它,它将一直可用,直到您退出该函数为止,即使您使用&
返回该变量的地址。不要这样做
所以,struct-Account-userAccount
将创建一个可以立即使用的变量:userAccount.acct\u year=4代码>
如果希望使用指针间接访问,则需要创建指针struct Account*pUserAccount代码>。在这个指针中,我们以这种方式保存userAccount
的地址:pUserAccount=&userAccount
。注意,我没有使用*
,因为我们没有访问
使用此方法的优点是编译器知道我们所指内容的大小。稍后,当您熟悉这一点时,可以使用结构数组以及如何使用指针访问它们
无论如何,继续解释
例如,如果您希望有一个创建帐户的函数并在函数外部使用该帐户,则必须使用malloc
malloc
从堆中提取内存,并返回该内存位置的地址
您必须告诉malloc
您希望保留多少内存:sizeof(struct Account)
如前所述,malloc(sizeof(struct Account))代码>将返回一个地址。
现在,您可以从函数返回该地址或立即使用它
检查NULL
很重要,因为malloc
可能无法获取保留内存(即内存不足)
此外,你自己打扫卫生也很重要。释放堆中占用的内存,以便其他程序也可以使用它。这将防止许多问题,如内存分割等
您可以将其声明为struct Account userAccount代码>和自动内存将在块的整个生命周期内分配。如果声明了,您有两个选项可以选择如何初始化声明的指针。以你为例:
typedef struct account {
char name[80];
int acct_year;
} account;
int main (void)
{
account *accountant;
}
上面,accountary
是一个未初始化的指针,它将不确定(可以是任何内容)的地址作为其值。要初始化指针,必须(1)为其分配现有类型帐户的地址,例如:
int main (void)
{
account account1 = { "Client A", 2019 }; /* object of type account */
account *accountant = &account1; /* address assigned to pointer */
/* use accountant as needed */
printf ("\naccountant: %s (%d)\n", accountant->name, accountant->acct_year);
}
或者,(2)使用malloc
、calloc
或realloc
为accountary
动态分配存储,例如:
int main (void)
{
account *accountant = malloc (sizeof *accountant); /* allocate for 1 account */
if (accountant == NULL) { /* validate every allocation */
perror ("malloc-accountant");
return 1;
}
strcpy (accountant->name, "Client A");
accountant->acct_year = 2019;
/* use accountant as needed */
printf ("\naccountant: %s (%d)\n", accountant->name, accountant->acct_year);
free (accountant); /* free what you allocate */
}
两种方式都可以。指针只是一个普通变量,它保存了存储在内存中的其他内容作为其值的地址。换句话说,指针指向可以找到其他内容的地址。您必须确保指针的值指向包含该类型对象的有效内存地址时所包含的地址。如果您在使用它之前总是问“我的指针指向哪里?”,您将永远不会再遇到指针问题。这将创建一个适当类型的变量,但它不会创建结构的实例,也不会初始化指针的值。但实际上,对于这类问题,您最好跟随教程,而不是试图询问堆栈溢出中出现的每个细节