c - 打印数组中的项目不会打印这些项目

标签 c

#include <stdio.h>
#include <string.h>
#define MAX_LENGTH 20

struct Item
{
int SKU;
char name[MAX_LENGTH+1];

};

int contains(void)
    {
    int compare;
    return 0;
    }

void displayItemWith(struct Item item[], int count)
    {
    int i;

    char alphabet;
    i = 0;
    for(i=0; i<count; i++){
            //char inventory[ ] = {item->SKU};
            printf("%d", item[i].SKU);
    }



int main(void)
{
int compare;
char count;

    //prints the title
    printf("=== TEST ===\n");

//硬编码库存 21 件元素 - 还可以容纳 0 件元素

    struct Item inventory[21] =
    {

    // price  sku  txd qty min  name

            {  .SKU =   275, "Royal Gala Apples"   },
            {  .SKU =   386, "Honeydew Melon"      },
            {  .SKU =   240, "Blueberries"         },
            {  .SKU =   916, "Seedless Grapes"     },
            {  .SKU =   385, "Pomegranate"         },
            {  .SKU =   495, "Banana"              },
            {  .SKU =   316, "Kiwifruit"           },
            {  .SKU =   355, "Chicken Alfredo"     },
            {  .SKU =   846, "Veal Parmigiana"     },
            {  .SKU =   359, "Beefsteak Pie"       },
            {  .SKU =   127, "Curry Chicken"       },
            {  .SKU =   238, "Tide Detergent"      },
            {  .SKU =   324, "Tide Liq. Pods"      },
            {  .SKU =   491, "Tide Powder Det."    },
            {  .SKU =   538, "Lays Chips S&V"      },
            {  .SKU =   649, "Joe Org Chips"       },
            {  .SKU =   731, "Allen's Apple Juice" },
            {  .SKU =   984, "Coke 12 Pack"        },
            {  .SKU =   350, "Nestea 12 Pack"      },
            {  .SKU =   835, "7up 12 Pack"         }
    };


    printf("*** NO MATCHES ***\n");
    printf("=== END ===\n");
    return 0;
}

您好,用户,我正在尝试打印项目列表 以这种格式输出; SKU: 275 - 皇家嘎拉苹果 SKU: 386 - 蜜瓜

但唯一会打印的是===TEST===,没有匹配,===END===。

有什么理由吗?

最佳答案

因为无论你做什么,都会显示“NO MATCHES”,因为你没有任何条件地打印它。另外,为了打印列表,您需要调用您创建的方法“displayItemWith(struct Item item[], int count)”,并且请首先声明该结构以获得更好的方法。示例:

  struct Books {
  char  title[50];
  char  author[50];
  char  subject[100];
  int   book_id;
  };

关于c - 打印数组中的项目不会打印这些项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40796744/

相关文章:

C,读取文本文件时字符串中的垃圾/垃圾值

c++ - 使用 Ubuntu 在 C/C++ 中的 UDP 套接字发送限制

c - fflush(stdin) 函数不起作用

c++ - CMake项目结构: How do I properly merge libraries together and include them in multiple executables

c - 变量获取错误值?

c - 如何在 Gtk+ 中使用 C 将按钮和文本框放入 GUI 窗口中?

c - 在C中: comparing arrays with both string and integer elements

c - PIC C编译器RS232传输代码

我们可以使用默认的 linux TCP/IP 堆栈对 TCP 发送/接收使用零拷贝吗?

动态分配和填充 2 个矩阵、验证较小的一个是否是另一个矩阵的子集并检查条件的 C 程序