将指针数组传递给函数

泰迪熊
  1. 我有一个名为menu_item的结构,如下所示:

    struct menu_item
    {
        char name[ITEM_NAME_LEN+1];
    };
    
  2. 在main中,我声明了一个指向struct的指针数组(我对这部分是正确的吗?):

    struct menu_item * menu_items[NUM_MENU_ITEMS];
    
  3. 另外,主要是我想打电话给:

    init_menu(&menu_items[NUM_MENU_ITEMS]);
    
  4. init_menu函数如下所示:

    void menu_init(struct menu_item * menu_items[NUM_MENU_ITEMS])
    {
        /* allocate memory for each element in the array */
        menu_items[NUM_MENU_ITEMS] = (struct menu_item *) malloc(sizeof(struct menu_item));
    }
    

但是我遇到了细分错误,我在做什么错?提前致谢。

萨马拉斯

仔细看看您的功能。

void menu_init(struct menu_item * menu_items[NUM_MENU_ITEMS])
{
    /* allocate memory for each element in the array */
    menu_items[NUM_MENU_ITEMS] = (struct menu_item *) malloc(sizeof(struct menu_item));
}

您需要在函数的第二个参数中携带数组的大小。但是,NUM_MENU_ITEMS似乎是一个全局变量#define,因此您无需携带第二个参数。

然后,您正在访问一个超出范围的单元格menu_items[NUM_MENU_ITEMS]我假设您知道索引从0开始,在结束NUM_MENU_ITEMS-1

在函数中,您需要在循环内分配内存。而且,您不需要强制转换malloc返回的内容。

因此,例如,您可以执行以下操作:

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

#define ITEM_NAME_LEN 15
#define NUM_MENU_ITEMS 3

// Define the struct before main
struct menu_item {
  char name[ITEM_NAME_LEN + 1];
};

// Give a synonym. Now struct menu_item is the same with menu_item_t.
// Notice the _t extension, which implies that this is a typedef.
typedef struct menu_item menu_item_t;

/**
 * Given a pointer 'p' to an array of pointers
 * (of type menu_item_t), allocate memory for
 * every cell of the array.
 */
void init_menu(menu_item_t* p[]) {
  int i;
  for(i = 0; i < NUM_MENU_ITEMS; ++i) {
    // for every cell of our array, allocate memory
    p[i] = malloc(sizeof(menu_item_t));

    // check that allocation for the i-th cell is OK
    if(!p[i]) {
      printf("Error in allocating %d item!\n\n", i);
      return;
    }
  }
}

/**
 * Given a pointer 'p' to an array of pointers
 * (of type menu_item_t), de-allocate memory for
 * every cell of the array.
 */
void delete_menu(menu_item_t* p[]) {
  int i;
  for(i = 0; i < NUM_MENU_ITEMS; ++i) {
    // free the memory we had allocated for the i-th cell
    free(p[i]);

    // set the pointer to NULL
    p[i] = NULL;
  }
}

void fill(menu_item_t* p[]) {
  int i;
  for(i = 0; i < NUM_MENU_ITEMS; ++i) {
    strcpy(p[i]->name, "myitem");
  }
}

void print(menu_item_t* p[]) {
  int i;
  for(i = 0; i < NUM_MENU_ITEMS; ++i) {
    printf("%s\n", p[i]->name);
  }
}

int main(void) {
  // Declare an array of pointers of menu_items_t.
  // The size of the array is NUM_MENU_ITEMS
  menu_item_t *menu_items[NUM_MENU_ITEMS];

  init_menu(menu_items);

  fill(menu_items);

  print(menu_items);

  delete_menu(menu_items);

  return 0;
}

当我处理结构时,我总是想到这个例子。

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

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

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

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

来自分类Dev

将“指针数组”传递给函数

来自分类Dev

将char指针数组传递给函数

来自分类Dev

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

来自分类Dev

将char指针数组传递给函数

来自分类Dev

将指针或数组传递给函数

来自分类Dev

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

来自分类Dev

将指针数组传递给函数

来自分类Dev

C ++将指针数组传递给函数

来自分类Dev

将指针数组传递给函数

来自分类Dev

将指针传递给函数

来自分类Dev

将指针传递给函数

来自分类Dev

将指针传递给函数

来自分类Dev

将指针传递给函数

来自分类Dev

将指针传递给函数

来自分类Dev

将数组传递给函数-指针与引用(C ++与C)

来自分类Dev

将字符指针数组传递给C函数

来自分类Dev

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

来自分类Dev

如何通过BLAS函数将指针传递给子数组?

来自分类Dev

将数组的指针传递给对数字进行排序的函数

来自分类Dev

将数组的指针传递给C中的函数

来自分类Dev

将指向数组的指针传递给函数有什么好处?

来自分类Dev

C将void指针数组传递给函数

来自分类Dev

C指针,了解如何将数组传递给函数

来自分类Dev

将结构数组传递给函数作为指针(C)

来自分类Dev

将指针传递给构造函数中的数组

来自分类Dev

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

来自分类Dev

如何使用指针将字符数组传递给函数?

来自分类Dev

将指针传递给需要数组的函数是否合法?