为每个用户制作不同的指针,如何? [英] Make different pointer for every user,How?
本文介绍了为每个用户制作不同的指针,如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嘿所有
我面临着为每个用户创建不同结构指针的问题
Hey All
I am facing a problem for creating the struct pointer different for every user
#include<conio.h>
struct mine_struct
{
int a;
};
int main()
{
for(int i=0;i<=10;i++)
{
int num;
printf("Hey user please enter the no\n");
scanf("%d",&num);
if(num==1)
{
//create a struct pointer for every user dynamically
//access the a variable
//if i do like this
struct mine_struct *p =NULL;
p->a=21;
//Then p i allocated for every 10 runs i want to create the different pointer for different user and also destroy them
}
else
{
printf("you have not entered the valid no\n");
}
}
return 0;
}
推荐答案
如果你有固定数量的用户(比如10
)然后声明
If you have a fixed number of users (say10
) then declare
#define USERS 10
struct mine_struct *p[USERS];
然后,在运行时,创建并填写正确项目的详细信息,例如
and then, at runtime, create and fill the details of proper item, e.g.
if (user == 5)
{
p[user] =(struct mine_struct *) malloc(sizeof(struct mine_struct));
if (p[user])
p[user]->a = 21;
}
然后,当你不再需要它时
Then, when you don't need anymore it
if ( p[user] )
{
free(p[user]);
p[user] == NULL;
}
你不能这样做:你专门将指针的值设置为null,然后尝试通过它访问数据 - 在任何情况下都不会起作用。
我建议您查看 malloc [ ^ ]和免费 [ ^ ]但我无法帮助你,因为你对你的想法所做的描述在用你的代码查看时没有任何意义......
You can't do that: you are specifically setting the value of the pointer to null and then trying to access data via it - that won't work under any circumstances.
I would suggest that you need to look at malloc[^] and free[^] but I can't help you much further as your description of what you want to do makes no sense when viewed with your code...
这篇关于为每个用户制作不同的指针,如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文