gpt4 book ai didi

c - 我该如何让这个指针工作?

转载 作者:行者123 更新时间:2023-11-30 16:32:05 24 4
gpt4 key购买 nike

我有两个功能。一种称为 play_card,一种称为 scan_card。

如您所见,在 scan_card 函数中,它扫描了 3 个数字。但是,我需要将这三个数字返回给 play_card。我怎样才能用指针做到这一点?

void play_card(void) {

int card_one;
int card_two;
int card_three;

int card_array[] = {0};

// Calls the scan_card function
play_scanner(card_array);
}

// Scans in the cards a user puts in
void scan_card(int card_array[]) {

int num1;
int num2;
int num3;

scanf("%d %d %d", &num1, &num2, &num3);

*card_one = num1;
*card_two = num2;
*card_three = num3;
}

最佳答案

这是我想到的最简单的实现。

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

void play_card(void);
void scan_card(int* card_array[]);


int main()
{
play_card();
return 0;
}

void play_card(void)
{
int* card_array[3];

// Calls the scan_card function
scan_card(card_array);
int card_one = *card_array[0];
int card_two = *card_array[1];
int card_three = *card_array[2];
printf("\n%d\n%d\n%d\n" , card_one , card_two , card_three);
}

// Scans in the cards a user puts in
void scan_card(int* card_array[])
{

int num1;
int num2;
int num3;

scanf("%d %d %d", &num1, &num2, &num3);

card_array[0] = &num1;
card_array[1] = &num2;
card_array[2] = &num3;
}

关于c - 我该如何让这个指针工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50287819/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com