求问:结构
先说题目吧这是题目范例,就是要最终得到的效果
我现在可以得到分部的部分就是输一个数,得到一个输出
#include <stdio.h>
struct bits
{
unsigned char bit0 : 1;
unsigned char bit1 : 1;
unsigned char bit2 : 1;
unsigned char bit3 : 1;
unsigned char bit4 : 1;
unsigned char bit5 : 1;
unsigned char bit6 : 1;
unsigned char bit7 : 1;
};
union w { struct bits a; unsigned char t; }m;
void f0(unsigned char bit) { printf("The Interrupt Service Routine isr0 is called!\n"); }
void f1(unsigned char bit) { printf("The Interrupt Service Routine isr1 is called!\n"); }
void f2(unsigned char bit) { printf("The Interrupt Service Routine isr2 is called!\n"); }
void f3(unsigned char bit) { printf("The Interrupt Service Routine isr3 is called!\n"); }
void f4(unsigned char bit) { printf("The Interrupt Service Routine isr4 is called!\n"); }
void f5(unsigned char bit) { printf("The Interrupt Service Routine isr5 is called!\n"); }
void f6(unsigned char bit) { printf("The Interrupt Service Routine isr6 is called!\n"); }
void f7(unsigned char bit) { printf("The Interrupt Service Routine isr7 is called!\n"); }
int main(int argc, char const *argv[])
{ unsigned int n;
void (*p_fun[8])(unsigned char b);
scanf("%d", &n);
m.t = n;
p_fun[0] = f0;
p_fun[1] = f1;
p_fun[2] = f2;
p_fun[3] = f3;
p_fun[4] = f4;
p_fun[5] = f5;
p_fun[6] = f6;
p_fun[7] = f7;
if(m.a.bit0) p_fun[0](m.a.bit0);
if(m.a.bit1) p_fun[1](m.a.bit1);
if(m.a.bit2) p_fun[2](m.a.bit2);
if(m.a.bit3) p_fun[3](m.a.bit3);
if(m.a.bit4) p_fun[4](m.a.bit4);
if(m.a.bit5) p_fun[5](m.a.bit5);
if(m.a.bit6) p_fun[6](m.a.bit6);
if(m.a.bit7) p_fun[7](m.a.bit7);
return 0;
}
但是在到输入全部的值出错了,直接显示无法运行,求教,这是我改的一部分代码,其他没改
int main(int argc, char const *argv[])
{
void (*p_fun[8])(unsigned char b);
int p,q,n[10];
scanf("%d", &q);
for(p=1;p<=q;p++)
{scanf("%d", n[p]);}
for(p=1;p<=q;p++)
{
m.t = n[p];
想知道错在哪里,怎么改正
这是原代码
int main(int argc, char const *argv[])
{ unsigned int n;
void (*p_fun[8])(unsigned char b);
scanf("%d", &n);
m.t = n;