将char数组传递给函数内的struct数组

安德鲁

如标题中所述,我不知道如何将char数组传递给函数内的struct数组。

任务是编写一个小的仓库系统,您可以在其中添加/删除/搜索项目。但是为了添加/删除项目,应检查该项目是否已经存在。

我必须使用一个结构和该结构的数组。我目前的方法确实有问题,我尝试了几个小时来解决此问题,但目前我什至不知道要解决什么。

重要提示:我不允许使用任何其他库比stdio.h中和string.h中,我不准使用别的比scanf函数输入和printf输出我必须将两个字符串都与strcmp进行比较,并且我不认为可以使用strncpy

也许我的整个方法是错误的。

这是我的代码

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

#define LENGTHITEM 100
#define NUMBEROFITEMS 100

typedef struct WareHouse_ {
    char item[LENGTHITEM];
    int number;
} WareHouse;

void newItem(char*, WareHouse*, int);

int main() {

    int end = 0; int inputNumber = 0;
    char decision; char inputItem[NUMBEROFITEMS];
    WareHouse wHouse[NUMBEROFITEMS];

    printf("\nWelcome!");
    printf("\n\nYou can choose the following:");

    while (end != 1) {
        printf("\n\nNew Item (H) / Delete Item (E) | Search Item (S) | Table (A) | End (B): ");
        scanf("%c", &decision);

        switch(decision) {
            case 'h':
            case 'H': printf("\nName: ");
                      scanf("%s, inputItem);
                      printf("Number: "); 
                      scanf("%i", &inputNumber); <-- Here it says: scanf used to convert a string to an integer value (CLion)
                      newItem(inputItem, wHouse, inputNumber);
                      break;
            case 'e':
            case 'E': printf("\nTest E");
                      break;
            case 's':
            case 'S': printf("\nTest S");
                      break;
            case 'a':
            case 'A': printf("\nTest A");
                      break;
            case 'b':
            case 'B': printf("\nGood bye!");
                      end++;
                      break;
        }
    }
    return 0;
}

void newItem(char *inputItem, WareHouse *wHouse , int inputNumber) {

    for (int i = 0; i < NUMBEROFITEMS; i++) {
        if (strcmp(inputItem, wHouse[i].item) == 0) {
            printf("\nItem already exists, number increased %i", inputNumber);
            wHouse[i].number+= inputNumber;

        } else if (strcmp(inputItem, wHouse[i].item) < 0 || strcmp(inputItem, wHouse[i].item) > 0) {
            wHouse[i].number+= inputNumber;

            <-- Here I want to pass the "inputItem" into the "wHouse" array but I don't know how.. -->
        }
    }
}


Program:

    Welcome

    You can choose the following:

    New Item (H) / Delete Item (E) | Search Item (S) | Table (A) | End (B): H <-- My input

    Name and number (Name Number): fish 10 <-- My input


    New Item (H) / Delete Item (E) | Search Item (S) | Table (A) | End (B): <-- BUG IT APPEARS TWO TIMES

    New Item (H) / Delete Item (E) | Search Item (S) | Table (A) | End (B): H

    Name and number (Name Number): fish 10 <-- My input

    <-- Here it should output: "Item already exists, number increased 10" but it does nothing -->


    New Item (H) / Delete Item (E) | Search Item (S) | Table (A) | End (B): <-- BUG IT APPEARS TWO TIMES

    New Item (H) / Delete Item (E) | Search Item (S) | Table (A) | End (B): B

    Good bye

迈克猫

要将字符串放入没有的另一个数组中strncpy(),可以使用strcpy()

strcpy(wHouse[i].item, inputItem);

另一种方法是计算通过strlen()复制和通过复制的长度memcpy()

size_t len = strlen(inputItem);
memcpy(wHouse[i].item, inputItem, len + 1); /* +1 for terminating null character */

如果这两种方式均不允许,则可以自己复制每个字符。

for (size_t p = 0; ; p++) {
    wHouse[i].item[p] = inputItem[p];
    /* break here (not via loop condition) instead of loop condition for copying terminating null character */
    if (inputItem[p] == '\0') break;
}

而且我在您的代码中发现了另外三个问题。

首先,使用数组时wHouse无需初始化,因此您使用不确定的值进行计算并调用未定义的行为

初始化可以这样进行:

WareHouse wHouse[NUMBEROFITEMS] = {{"", 0}};

您可以只为第一个元素指定值,剩下的元素将自动初始化为“零”。

其次,该选择显示两次,因为通过会读取换行符%c为避免这种情况,您可以在添加空格字符之前%c告诉scanf忽略空白字符。

scanf(" %c", &decision);

第三,如果newItem()像您说的那样函数中添加“ passing”,则将修改所有元素,因为对strcmp()返回的所有值都修改了元素相反,我猜您想在找不到指定的项目时修改第一个空元素。

为此,您可以这样编写:

void newItem(char *inputItem, WareHouse *wHouse , int inputNumber) {

    int itemFound = 0; /* set to 1 when specified item is found */
    int firstEmptyItem = -1; /* set to the index of the first empty item */

    for (int i = 0; i < NUMBEROFITEMS; i++) {
        if (strcmp(inputItem, wHouse[i].item) == 0) {
            printf("\nItem already exists, number increased %i", inputNumber);
            wHouse[i].number+= inputNumber;

            itemFound = 1; /* the item is found! */

        } else if (strcmp("", wHouse[i].item) == 0) { /* check if this item is empty */
            /* if empty and index is not written yet, write the index */
            if (firstEmptyItem < 0) firstEmptyItem = i;

        }
    }
    /* if the item is not found, and a room to add new item exists */
    if (!itemFound && firstEmptyItem >= 0) {
        int i = firstEmptyItem; /* set index (or you can use firstEmptyItem directly in the following code) */

        wHouse[i].number+= inputNumber;

        /* Here you should pass the "inputItem" into the "wHouse" array. */
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将char数组的数组传递给函数

来自分类Dev

将char数组的数组传递给函数

来自分类Dev

将数组“ typedef struct”传递给函数

来自分类Dev

将数组“ typedef struct”传递给函数

来自分类Dev

将struct数组传递给函数

来自分类Dev

无法将char数组传递给函数

来自分类Dev

将char指针数组传递给函数

来自分类Dev

将char数组的指针传递给函数

来自分类Dev

将char指针数组传递给函数

来自分类Dev

将char数组传递给函数C ++

来自分类Dev

将char数组传递给函数

来自分类Dev

将char指针/数组传递给函数

来自分类Dev

C编程,将char数组传递给函数

来自分类Dev

C18将char数组传递给函数

来自分类Dev

将指针传递给char数组作为函数的参数-C

来自分类Dev

将数组传递给函数

来自分类Dev

将数组传递给函数

来自分类Dev

将整数传递给函数内的数组(无指针)

来自分类Dev

将数组传递给函数或确保它在范围内?

来自分类Dev

将char数组传递给CUDA内核

来自分类Dev

将数组存储在变量中还是将数组直接传递给循环内的函数更好?

来自分类Dev

将数组存储在变量中还是将数组直接传递给循环内的函数更好?

来自分类Dev

将char *传递给C中的函数后,如何指向预期的char数组?

来自分类Dev

无法将char * [2]类型的数组的地址传递给采用char ***的函数

来自分类Dev

将char *传递给C中的函数后,如何指向预期的char数组?

来自分类Dev

将指向数组的指针传递给函数

来自分类Dev

将结构数组传递给函数?

来自分类Dev

C ++直接将数组传递给函数

来自分类Dev

将数组传递给bash函数