调用结构

时间:2016-09-28 08:38:17

标签: c string struct char stack

抱歉,如果之前已经被问了一百万次,但我认为我在理解字符串(字符数组)如何在C中工作时会遇到问题。

在我的计划中,我有一个"堆栈" struct,看起来像这样(在stack.h中):

struct stack_t {
/* Stack-Datentyp */
/* Stacks have a head-node, a length, and a name.
Stacks are filled with links (nodes) that are defined below. These links have
a generic data pointer and a pointer to the next link */
link head;                      
unsigned int length;            
char *name;                     //stack name
};

typedef struct stack_t *stack;

在我的main.c中,我调用了一个名为" stack_new"的函数。这似乎工作正常,因为所有printf调用工作:

stack stack_new(char *stackname) {
  /* creates a new, empty stack */
stack st = (stack)(malloc(sizeof(stack)));
if (st == NULL) {
fprintf(stderr, "Error: Memory for Stack could not be allocated!\n");
return NULL;
}
st->head = NULL;
printf("head works.\n");
st->length = 0;
printf("length works.\n");
st->name = stackname;
printf("stackname works.\n");
return st;
}

现在,当我在main.c中调用此函数时,我得到一个seg错误。这是电话:

if (strcmp(input,"newstack") == 0) {
    printf("Please enter stackname:\n");
    scanf("%s", &stackname);
    printf("Debug: Input works.\n");
    stacklist[NumberOfStacks] = stack_new(stackname);
    printf("stack created works-\n");
    NumberOfStacks++;
    printf("A new stack with the name '%s' was created!\nIt is number %d in stack list.", stacklist[NumberOfStacks]->name, NumberOfStacks);
    continue;
}

当我尝试打印struct stacklist [NumberOfStacks]指向的成员名称时发生seg错误。我在这里做错了什么?

我还收到一堆警告,告诉我有关stackname的scanf如何预期不同的类型:

  

警告:格式'%s'需要类型为'char '的参数,但参数2的类型为'char()[50]'[ - Wformat =]        scanf("%s",& stackname);

其他警告我告诉我,我不是使用我用于堆栈节点中数据存储的void指针进行类型转换,但我不认为这与我的问题相关。

感谢您的帮助!

编辑: 问题是我在使用它作为索引来增加NumberOfStacks,以便打印保存在数组中的结构的成员。

NumberOfStacks++;
    printf("A new stack with the name '%s' was created!\nIt is      number %d in stack list.", stacklist[NumberOfStacks]->name, NumberOfStacks);

这部分程序现在运行正常,谢谢!

2 个答案:

答案 0 :(得分:1)

您的代码中存在一些错误。大多数已在评论中得到解决。我做了更正,似乎没有段错误。 修改由代码中的注释表示。

这是我最终得到的代码:

#include <stdio.h>
// Prototype for malloc
#include <stdlib.h>
// Prototype for strdup
#include <string.h>

typedef void *link; /* Added to make it compile - you may have a different definition in your code */

struct stack_t {
    /* Stack-Datentyp */
    /* Stacks have a head-node, a length, and a name.
     * Stacks are filled with links (nodes) that are defined below. 
     * These links have
     * a generic data pointer and a pointer to the next link */
    link head;
    unsigned int length;
    char *name;                     //stack name
};

typedef struct stack_t *stack;

stack stack_new(char *stackname) {
    /* creates a new, empty stack */
    stack st = malloc(sizeof(*st));    // changed to use the variable name
    if (st == NULL) {
        fprintf(stderr, "Error: Memory for Stack could not be allocated!\n");
        return NULL;
    }
    st->head = NULL;
    printf("head works.\n");
    st->length = 0;
    printf("length works.\n");
    st->name = strdup(stackname);  // Changed: Duplicate the name so it won't get overwritten when creating the next stack
    printf("stackname works.\n");
    return st;
}


int main(void)
{
    char stackname[50];       // Added declaration for stackname.
    stack stacklist[50];      // Added declaration. Can handle up to 50 stacks.
    int NumberOfStacks = 0;

    printf("Please enter stackname:\n");
    scanf("%s", stackname);              // Changed. Removed the "&" since stackname is a char*.
    printf("Debug: Input works.\n");
    stacklist[NumberOfStacks] = stack_new(stackname);
    printf("stack created works-\n");
    // All data is at index 0, so cannot use increased value of NumberOfStacks to get get the data. Also changed the text to "has index" since a 0 will be printed for stacknumber.
    printf("A new stack with the name '%s' was created!\nIt has index %d in stack list.\n", stacklist[NumberOfStacks]->name, NumberOfStacks);
    NumberOfStacks++;    // Moved this line down.
    return 0;
}

答案 1 :(得分:0)

对于警告,scanf正在等待类型'指向char的指针'。你的变量'stackname'被声明为'char stackname [50]'(我想你还没有发布声明)。 使用&amp; stackname表示您想要访问数组的地址。 stackname或&amp; stackname的使用几乎相同,因为在第一种情况下,数组将转换为指向数组第一种情况的指针,在第二种情况下,它将是第一种情况下的数组地址数组的情况。

如果要复制字符串,则必须分配字符串大小的缓冲区(使用malloc)并使用strcpy。

对于segfault,请向我们展示stacklist和NumberOfStacks的声明。原因可能是NumberOfStacks的值大于数组的大小。