C语言指向结构的指针

示例

当变量包含时struct,可以使用点运算符(.)访问其字段。但是,如果您有指向的指针struct,则将无法使用。您必须使用箭头运算符(->)来访问其字段。这是一个使用structs指针并演示了箭头运算符的堆栈的非常简单的示例(有些人会说“可怕而简单”)。

#include <stdlib.h>

#include <stdio.h>

/* structs */

struct stack

{

    struct node *top;

    int size;

};

struct node

{

    int data;

    struct node *next;

};

/* function declarations */

int push(int, struct stack*);

int pop(struct stack*);

void destroy(struct stack*);

int main(void)

{

    int result = EXIT_SUCCESS;

    size_t i;

    /* allocate memory for a struct stack and record its pointer */

    struct stack *stack = malloc(sizeof *stack);

    if (NULL == stack)

    {

        perror("malloc() failed");

        return EXIT_FAILURE;

    }

    /* initialize stack */

    stack->top = NULL;

    stack->size = 0;

    /* push 10 ints */

    {

        int data = 0;

        for(i = 0; i < 10; i++)

        {

            printf("Pushing: %d\n", data);

            if (-1 == push(data, stack))

            {

                perror("push() failed");

                result = EXIT_FAILURE;

                break;

            }

            ++data;

        }

    }

    if (EXIT_SUCCESS == result)

    {

        /* pop 5 ints */

        for(i = 0; i < 5; i++)

        {

            printf("Popped: %i\n", pop(stack));

        }

    }

    /* destroy stack */

    destroy(stack);

    return result;

}

/* Push a value onto the stack. */

/* Returns 0 on success and -1 on failure. */

int push(int data, struct stack *stack)

{

    int result = 0;

    /* allocate memory for new node */

    struct node *new_node = malloc(sizeof *new_node);

    if (NULL == new_node)

    {

        result = -1;

    }

    else

    {

        new_node->data = data;

        new_node->next = stack->top;

        stack->top = new_node;

        stack->size++;

    }

    return result;

}

/* Pop a value off of the stack. */

/* Returns the value popped off the stack */

int pop(struct stack *stack)

{

    struct node *top = stack->top;

    int data = top->data;

    stack->top = top->next;

    stack->size--;

    free(top);

    return data;

}

/* destroy the stack */

void destroy(struct stack *stack)

{

    /* free all pointers */

    while(stack->top != NULL)

    {

        pop(stack);

    }

}

           

以上是 C语言指向结构的指针 的全部内容, 来源链接: utcz.com/z/326221.html

回到顶部