c - 将指针数组传递给函数

标签 c

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

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

    struct menu_item * menu_items[NUM_MENU_ITEMS];
    
  3. 而且在 main 中,我正在尝试调用:

    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;
}

当我处理结构时,我总是有 this脑海中的例子。

关于c - 将指针数组传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26063002/

相关文章:

c - sscanf 在从 c 中的内存指针扫描 short int 时返回 -1?

当我尝试使用命令行参数编译 C 源文件时,WSL 出现编译错误

c - 如何获取和比较 libcurl 版本?

c - C 代码混淆教程

c - STM32L476 flash 页删除无效

C- For 循环 gcc 中的 int 第一个声明

c - 使用 valgrind 在 strstr() 中读取大小 1 无效

c - 为什么下面的代码不打印整数的二进制表示形式?

c - 如何使用 inotify-rs 生成的 WatchDescriptor 结构将事件与生成它们的文件匹配?

c - 为什么 "scanf"有效但 fgets 在 C 中不起作用?