【问题标题】:How can i print an interior supply of structure on c?如何在 c 上打印内部结构供应?
【发布时间】:2021-07-18 10:03:52
【问题描述】:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdbool.h>

struct game{
  bool is_computer_game;
  bool  is_tabletop_game;
  bool is_for_children;
  bool is_collective;
};

int main(){
  int n,a,b,c,d;
  printf("How much games do you want?:");
  scanf("%d",&n);
  struct game games[n];
  for(int i=0;i<n;i++){
    a=rand()%2;
    b=1-a;
    c=rand()%2;
    d=rand()%2;
      if(a==0){
            games[i].is_computer_game=false;
      }
      if(a==1){
            games[i].is_computer_game=true;
      }
      if(b==0){
            games[i].is_tabletop_game=false;
      }
      if(b==1){
            games[i].is_tabletop_game=true;
      }
      if(c==0){
            games[i].is_for_children=false;
      }
      if(c==1){
            games[i].is_for_children=true;
      }
      if(d==0){
            games[i].is_collective=false;
      }
      if(d==1){
            games[i].is_collective=true;
      }

  }
  printf("\nHere is the information about all games we have:\n\n ");
  for(int i=0;i<n;i++){
    printf("is the game a computer one: %d\n",games[i].is_computer_game);
    printf("is the game a tabletop one: %d\n",games[i].is_tabletop_game);
    printf("is the game for children: %d\n",games[i].is_for_children);
    printf("is the game a collective one: %d\n\n",games[i].is_collective);
  }
  return 0;
}

好吧,我有这段代码可以创建一个结构并用一些元素填充它并打印所有这些元素。另一件事是该程序还必须打印它的内部供应。长话短说,我需要那个

struct game games[n]

被打印为代码和二进制数字字符串,但我不知道我该怎么做。你能帮帮我吗?

【问题讨论】:

  • 您的问题有点不清楚,但如果您只想将 games 数组的原始内容打印为十六进制值,您可以执行以下操作:char *ptr = (char*)games; for (int i = 0; i &lt; sizeof(games); ++i) printf(" %02X",ptr[i]);
  • 顺便说一句,由于 C 中的布尔值只是 0 和 1,因此您的 if 语句不是必需的,您可以直接进行赋值,例如 games[i].is_computer_game = a;
  • SGeorgiades,这个结构只包含零和一,你的建议是真正正确的。谢谢你的帮助

标签: c struct printf


【解决方案1】:

将代码中的行更改如下:

int n,a,b,c,d,i;

然后在main()的return 0;之前添加以下块:

char *ptr = (char*)games;
for (i = 0; i < sizeof(games); ++i) printf(" %02X",ptr[i]);   /* Option 1 */
printf("\n");

for (i = 0; i < sizeof(games); i+=4) 
printf("%1X%1X%1X%1X",ptr[i],ptr[i+1],ptr[i+2],ptr[i+3]);     /* Option 2 */

运行您的代码,为 n 输入 3。您应该会看到类似于以下输出的内容:

How much games do you want?:3
Here is the information about all games we have:

 is the game a computer one: 1
is the game a tabletop one: 0
is the game for children: 0
is the game a collective one: 1

is the game a computer one: 1
is the game a tabletop one: 0
is the game for children: 1
is the game a collective one: 1

is the game a computer one: 0
is the game a tabletop one: 1
is the game for children: 0
is the game a collective one: 1

 01 00 00 01 01 00 01 01 00 01 00 01           /* Option 1 */
 1001 1011 0101                                /* Option 2 */

您可以确定您更喜欢选项 1 还是选项 2。

【讨论】:

    猜你喜欢
    • 2021-07-12
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-18
    • 2018-01-04
    相关资源
    最近更新 更多