2014-09-26 73 views
1
  1. 我有一个结构称为MENU_ITEM,看起来像:传递指针数组到功能

    struct menu_item 
    { 
        char name[ITEM_NAME_LEN+1]; 
    }; 
    
  2. 而在主本人声明指针数组的结构(我说的对这个部分?):

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

但是我得到一个分割错误,我究竟做错了什么?提前致谢。

+0

请参阅[C乱码英文](http://cdecl.org)并输入'struct menu_item * menu_items [3];'以获得一些指导。 – chux 2014-09-26 15:28:44

+0

'init_menu(&menu_items [NUM_MENU_ITEMS]);'将指针传递给数组末尾的不存在的元素。只是缺乏一些基本的语法知识,在这里。 – 2014-09-26 15:29:09

回答

2

仔细看看你的功能。

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例子。

+1

+1用于处理分配问题(注意:最好使用'free(p [i]); p [i] = NULL;'delete_menu()'_may_不是'menu_items'的最终用法。 – chux 2014-09-26 15:53:07

1

要调用你的函数作为

init_menu(&menu_items[NUM_MENU_ITEMS]); 

这是没有意义的。表达式&menu_items[NUM_MENU_ITEMS]创建索引为NUM_MENU_ITEMS的元素的指针。这样的元素不存在。您的阵列的元素编号从0NUM_MENU_ITEMS - 1。没有索引NUM_MENU_ITEMS的元素。

表达式&menu_items[NUM_MENU_ITEMS]在数组的末尾产生一个指向未知存储器的指针。您将该指针传递给函数。后来你试图使用该指针,就好像它是你的数组一样。你写入那个导致崩溃的未知内存。

如果您想将数组传递给函数,只需传递它即可。你的功能应该叫做

init_menu(menu_items); 

就是这样。没有必要创建任何指向具有奇怪索引的元素的指针。

后,你的函数中你又试图访问您的阵列

menu_items[NUM_MENU_ITEMS] = ... 

此元素NUM_MENU_ITEMS无厘头无论是对非常相同的原因。