Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将struct中的所有变量设置为0/1_C - Fatal编程技术网

将struct中的所有变量设置为0/1

将struct中的所有变量设置为0/1,c,C,我想知道是否有可能将结构中存在的所有变量(在循环中)初始化为0并返回到1 下面是一个演示程序: #include <stdio.h> #define LEDS_LENGTH 7 void inititate_to_zero(void); void inititate_to_one(void); struct pins { unsigned char state : 1; unsigned char LED0 : 1; unsigned char LED

我想知道是否有可能将结构中存在的所有变量(在循环中)初始化为
0
并返回到
1

下面是一个演示程序:

#include <stdio.h>

#define LEDS_LENGTH 7

void inititate_to_zero(void);
void inititate_to_one(void);

struct pins
{
    unsigned char state : 1;
    unsigned char LED0  : 1;
    unsigned char LED1  : 1;
    unsigned char LED2  : 1;
    unsigned char LED3  : 1;
    unsigned char LED4  : 1;
    unsigned char LED5  : 1;
}pins;

int main(void)
{
    inititate_to_zero( );
    inititate_to_one( );
}

void inititate_to_zero(void)
{
    for(unsigned char i = 0; i < LEDS_LENGTH; i++)
    {
        /// set all variable in struct pins to 0;
        pins.VARIABLE[SOME_HOW] ^= ( 1 << 0 );
    }
}

void inititate_to_one(void)
{
    for (unsigned char i = 0; i < LEDS_LENGTH; i++)
    {
        /// set all variable in struct pins to 1;
        pins.VARIABLE[SOME_HOW] ^= ( 1 << 0 );
    }
}
拥有

要将all设置为0:
memset(&pins,0,sizeof(pins))

要将全部设置为1:
memset(&pins,-1,sizeof(pins))

可能会对memset进行优化,只设置一个字节


要帮助John Doe:

#include <stdio.h>
#include <string.h>

struct pins
{
    volatile unsigned char state    : 1;
    unsigned char LED0              : 1;
    unsigned char LED1              : 1;
    unsigned char LED2              : 1;
    unsigned char LED3              : 1;
    unsigned char LED4              : 1;
    unsigned char LED5              : 1;
} pins;

void pr()
{
  printf("%u %u %u %u %u %u %u\n",
     pins.state, pins.LED0, pins.LED1, pins.LED2, pins.LED3, pins.LED4, pins.LED5);
}

int main()
{
  /* pins is already to 0 because static */
  memset(&pins, -1, sizeof(pins));
  pr();
  memset(&pins, 0, sizeof(pins));
  pr();
  memset(&pins, -1, sizeof(pins));
  pr();

  return 0;
}

也许你真正想要的是一个数组?或者可能使用单个
无符号字符
作为位字段?C结构的初始化为0(或任何零ish/null值)非常简单。稍后设置为0或1或初始化为1是不同的。因此,这实际上取决于你问的是什么问题。制作一个LED阵列[i]而不是led0 led1……你有
memset
可用,对吗?(例如
memset(&pins,1,sizeof(struct pins));
)只需使用
0
将每个字节归零。@DavidC.Rankin此memset将不起作用。您需要的不是1,而是0xF和值255,它从何而来?@Dominique 255的所有位都是1,0xff是十六进制的,11111111是二进制的。如果一个字节有
8
位:那么
0
意味着
0000
,我需要一个字节,所有变量的
0000 0001
。想想看:
uint8\u t led=0:
这意味着
0000
如果我这样做
led^=(1)我需要将all设置为
0000 0000
0000 0001
。因为我使用位字段,我只有on位,所以这个位应该是
0
1
@JohnDoe:您还没有理解这个答案;通过将结构占用的所有字节设置为所有字节,单个位字段将隐式变成
1
becau请注意,我建议使用
~0
而不是
-1
,但两者都可以。
struct pins
{
    volatile unsigned char state    : 1;
    unsigned char LED0              : 1;
    unsigned char LED1              : 1;
    unsigned char LED2              : 1;
    unsigned char LED3              : 1;
    unsigned char LED4              : 1;
    unsigned char LED5              : 1;
}pins;
#include <stdio.h>
#include <string.h>

struct pins
{
    volatile unsigned char state    : 1;
    unsigned char LED0              : 1;
    unsigned char LED1              : 1;
    unsigned char LED2              : 1;
    unsigned char LED3              : 1;
    unsigned char LED4              : 1;
    unsigned char LED5              : 1;
} pins;

void pr()
{
  printf("%u %u %u %u %u %u %u\n",
     pins.state, pins.LED0, pins.LED1, pins.LED2, pins.LED3, pins.LED4, pins.LED5);
}

int main()
{
  /* pins is already to 0 because static */
  memset(&pins, -1, sizeof(pins));
  pr();
  memset(&pins, 0, sizeof(pins));
  pr();
  memset(&pins, -1, sizeof(pins));
  pr();

  return 0;
}
1 1 1 1 1 1 1
0 0 0 0 0 0 0
1 1 1 1 1 1 1