如何在c中的结构中实现变量?
How to implement variables in a structure in c?
这不是完整的程序
struct p
{
int a;
int b;
int c;
};
void main()
{
int op,i,j,to,from,a,b,c;
struct p pr[3];
clrscr();
for(i=1;i<4;i++)
{
pr[i].a=0;
pr[i].b=0;
pr[i].c=0;
printf("P%d %d %d %d\n",i,pr[i].a,pr[i].b,pr[i].c);
}
do{
printf("\nEnter the option you want to execute:\n1. Internal Event\n2. Send Message \n3. Exit \n\n");
scanf("%d",&op);
switch(op)
{
case 1:
printf("\nEnter the process for which the internal event takes place: ");
scanf("%d",&j);
j 的值将从 1 到 3 不等
我想在这里做的是:
如果j=1,
pr[1].a=pr[1].a+1;
我想自动更新 j 的值,而不是为 j 实现 switch case
pr[j]a/b/c
谁能帮忙解决一下。
PS: 我正在尝试为向量时钟实现一个 C 程序
如果struct不变,就相当于一个int数组。然后你可以使用联合并使用最漂亮的东西:
typedef union uBoth {
struct
{
int a;
int b;
int c;
} p;
int q[3];
};
void main()
{
int op,i,j,to,from,a,b,c;
union uBoth pr[3];
clrscr();
for(i=0; i<3; i++)
{
for (j=0; j<3; j++)
pr[i].q[j]= 0;
printf("P%d %d %d %d\n",i+1, pr[i].p.a, pr[i].p.b, pr[i].p.c);
}
do {
printf("\nEnter the option you want to execute:\n1. Internal Event\n2. Send Message \n3. Exit \n\n");
scanf("%d",&op);
switch(op)
{
case 1:
printf("\nEnter the process for which the internal event takes place: ");
scanf("%d",&j);
pr[j-1].p.a= 1;
}
} while(1);
}
这不是完整的程序
struct p
{
int a;
int b;
int c;
};
void main()
{
int op,i,j,to,from,a,b,c;
struct p pr[3];
clrscr();
for(i=1;i<4;i++)
{
pr[i].a=0;
pr[i].b=0;
pr[i].c=0;
printf("P%d %d %d %d\n",i,pr[i].a,pr[i].b,pr[i].c);
}
do{
printf("\nEnter the option you want to execute:\n1. Internal Event\n2. Send Message \n3. Exit \n\n");
scanf("%d",&op);
switch(op)
{
case 1:
printf("\nEnter the process for which the internal event takes place: ");
scanf("%d",&j);
j 的值将从 1 到 3 不等 我想在这里做的是: 如果j=1, pr[1].a=pr[1].a+1; 我想自动更新 j 的值,而不是为 j 实现 switch case pr[j]a/b/c
谁能帮忙解决一下。 PS: 我正在尝试为向量时钟实现一个 C 程序
如果struct不变,就相当于一个int数组。然后你可以使用联合并使用最漂亮的东西:
typedef union uBoth {
struct
{
int a;
int b;
int c;
} p;
int q[3];
};
void main()
{
int op,i,j,to,from,a,b,c;
union uBoth pr[3];
clrscr();
for(i=0; i<3; i++)
{
for (j=0; j<3; j++)
pr[i].q[j]= 0;
printf("P%d %d %d %d\n",i+1, pr[i].p.a, pr[i].p.b, pr[i].p.c);
}
do {
printf("\nEnter the option you want to execute:\n1. Internal Event\n2. Send Message \n3. Exit \n\n");
scanf("%d",&op);
switch(op)
{
case 1:
printf("\nEnter the process for which the internal event takes place: ");
scanf("%d",&j);
pr[j-1].p.a= 1;
}
} while(1);
}