指向结构的指针未递增

时间:2018-09-15 17:12:11

标签: c arrays pointers struct memory-address

我是C语言的新手,所以这段代码中可能存在我不知道的逻辑错误。有一种卡的结构具有价值和适合作为字段。

我对这段代码的思考过程是:

  • 首先,创建一个指向卡片的指针。
  • 第二,在for循环中创建卡并分配该卡的字段。
  • 最后,指向创建的卡片并增加指针。

重复此过程,以便在连续的内存地址中创建52张卡。 基本上,我打算为内存中的每8个字节创建一副纸牌,但是循环中的card_ptr++;行不起作用。知道这里有什么问题吗?

我测试了注释部分,它按照我的意愿将指针增加了8个字节,但是循环并没有这样做。我还添加了打印语句,以帮助您了解我的想法。

#include <stdio.h>

typedef enum {
  SPADES,
  HEARTS,
  DIAMONDS,
  CLUBS,
  NUM_SUITS
} suit_t;

struct card_tag {
  unsigned value;
  suit_t suit;
};
typedef struct card_tag card_t;

int main(){ 

    card_t *card_ptr;
    printf("Initial card pointer created. %d\n", card_ptr);
    for(int i =SPADES; i < NUM_SUITS; i++){
        for(int j = 1; j < 14; j++){
            card_t card;
            card.value = j;
            card.suit = i;
            printf("Card -> Value = %d Suit = %d, is created.\n", card.value, card.suit);
            card_ptr = &card;
            printf("%d points to the last card.\n", card_ptr);
            card_ptr++;
            printf("Pointer is incremented to %p\n\n", card_ptr);
        }   
    }

    /*card_t *card_ptr;
    printf("%d\n", card_ptr);
    card_ptr++;
    printf("%d\n", card_ptr);
    card_ptr++;
    printf("%d\n", card_ptr);
    */
}

2 个答案:

答案 0 :(得分:3)

您的代码显然导致未定义的行为。 如果要使用指针,请考虑以下代码。

1:首先,您需要声明指向指针的指针。

card_t **card_ptr = malloc(sizeof(card_t*)*NUM_SUITS);

2:然后为每个指针分配内存。

card_ptr[i] = malloc(sizeof(card_t)*14);

3:如下所示递增指针。

card_ptr[i]++;

4:工作完成后,使用free释放内存。

示例代码:

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

typedef enum {
  SPADES,
  HEARTS,
  DIAMONDS,
  CLUBS,
  NUM_SUITS
} suit_t;

struct card_tag {
  unsigned value;
  suit_t suit;
};
typedef struct card_tag card_t;

int main(){ 

    card_t **card_ptr = malloc(sizeof(card_t*)*NUM_SUITS);
    if (card_ptr == NULL) return 0;

    printf("Initial card pointer created. %d\n", card_ptr);

    for(int i =SPADES; i < NUM_SUITS; i++){

        card_ptr[i] = malloc(sizeof(card_t)*14);
        if (card_ptr[i] == NULL) return 0;

        card_t *tempPtr = card_ptr[i];

        for(int j = 1; j < 14; j++){

            tempPtr->value = j;
            tempPtr->suit = i;

            printf("Card -> Value = %d Suit = %d, is created.\n", tempPtr->value, tempPtr->suit);

            printf("%p points to the last card.\n", tempPtr);
            tempPtr++;
            printf("Pointer is incremented to %p\n\n", tempPtr);
        }   
    }

    /*card_t *card_ptr;
    printf("%d\n", card_ptr);
    card_ptr++;
    printf("%d\n", card_ptr);
    card_ptr++;
    printf("%d\n", card_ptr);
    */

     for(int i =SPADES; i < NUM_SUITS; i++){
        free(card_ptr[i]);
        card_ptr[i] = NULL;
     }
    free(card_ptr);
    card_ptr = NULL;

}

答案 1 :(得分:1)

更大的问题是卡的声明范围。局部变量在堆栈上分配,当它们超出范围时,某种意义上“释放”内存(可以在其他地方使用,尽管可能不会重新初始化为零)。您的循环会继续使用相同的内存区域,因为卡超出了范围,并在下一次循环迭代时重新创建。

正如其他人指出的那样,您需要静态声明一个卡数组,或使用malloc来确保它们卡在堆上。

以您的代码为起点,下面是一个示例:

示例(声明纸牌数组并使用指针进行迭代)

card_t deck[52];

card_t *card_ptr = deck; /* Set pointer to the beginning of the array of cards */

printf("Initial card pointer created. %d\n", card_ptr);
for(int i =SPADES; i < NUM_SUITS; i++){
    for(int j = 1; j < 14; j++){
        card_ptr->value = j;
        card_ptr->suit = i;
        printf("Card -> Value = %d Suit = %d, is created.\n", card_ptr->value, card_ptr->suit);
        printf("%d points to the last card.\n", card_ptr);
        card_ptr++;
        printf("Pointer is incremented to %p\n\n", card_ptr);
    }   
}
相关问题