gpt4 book ai didi

c - 如何使用 gets_s 在 C 中使用 atoi 转换 - 包含示例代码

转载 作者:太空宇宙 更新时间:2023-11-04 01:12:33 25 4
gpt4 key购买 nike

我正在自学 C 语言的结构,但在编译这段代码时遇到了问题:

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

struct Date {
int Month;
int Day;
int Year;
};

void AddDecade(struct Date);

int main(int argc, char *argv[]) {
struct Date BDay;
char buffer[50];

printf("What month were you born? ");
BDay.Month = atoi(gets_s(buffer, 50));

printf("What day were you born? ");
BDay.Day = atoi(gets_s(buffer, 50));

printf("What year were you born? ");
BDay.year = atoi(gets_s(buffer, 50));

printf("You were born on %d, %d, %d?\n", BDay.Month, BDay.Day, BDay.Year);

AddDecade(BDay);

printf("You will be 10 years older on %d, %d, %d\n", BDay.Month, BDay.Day, BDay.Year);
}

void AddDecade(struct Date Target) {
Target.Year += 10;
}

代码的作者在Windows机器上编译它没有错误,但是在我的Linux机器上gcc给出了以下错误:

07_04_structures2.c: In function ‘main’:

07_04_structures2.c:18:5: warning: passing argument 1 of ‘atoi’ makes pointer from integer without a cast [enabled by default]

/usr/include/stdlib.h:148:12: note: expected ‘const char *’ but argument is of type ‘int’

07_04_structures2.c:21:5: warning: passing argument 1 of ‘atoi’ makes pointer from integer without a cast [enabled by default]

/usr/include/stdlib.h:148:12: note: expected ‘const char *’ but argument is of type ‘int’

07_04_structures2.c:24:9: error: ‘struct Date’ has no member named ‘year’

07_04_structures2.c:24:5: warning: passing argument 1 of ‘atoi’ makes pointer from integer without a cast [enabled by default]

/usr/include/stdlib.h:148:12: note: expected ‘const char *’ but argument is of type ‘int’

鉴于这一行:

BDay.Month = atoi(gets_s(buffer, 50));

我的理解是gets_s将最多50字节的输入复制到buffer中,并将指向变量'buffer'的指针传递给atoi,但是根据这个错误:

note: expected ‘const char *’ but argument is of type ‘int’

也许是 gets_s 出了问题?我以前从未使用过它...

如果能详细解释问题所在以及解决方法,我将不胜感激。

非常感谢!


更新:

我实现了您的所有建议并提出了以下工作代码:

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

struct Date {
int Month;
int Day;
int Year;
};

struct Date AddDecade(struct Date);

int main(int argc, char *argv[]) {
struct Date BDay;
char buffer[50];
char *buffer_end;

printf("What month were you born? ");
fgets(buffer, sizeof(buffer), stdin);
BDay.Month = strtol(buffer, &buffer_end, 10);

printf("What day were you born? ");
fgets(buffer, sizeof(buffer), stdin);
BDay.Day = strtol(buffer, &buffer_end, 10);

printf("What year were you born? ");
fgets(buffer, sizeof(buffer), stdin);
BDay.Year = strtol(buffer, &buffer_end, 10);

printf("You were born on %d, %d, %d?\n", BDay.Month, BDay.Day, BDay.Year);

BDay = AddDecade(BDay);

printf("You will be 10 years older on %d, %d, %d\n", BDay.Month, BDay.Day, BDay.Year);
}

struct Date
AddDecade(struct Date Target) {
Target.Year += 10;
return Target;
}

这对我来说是一次很棒的学习经历!谢谢你! :)

最佳答案

gets_s是 C11 中定义的函数,它被定义为可选扩展。

您必须使用 C11 支持编译您的程序,否则 gets_s stdio.h 中未声明函数.当函数未在 stdio.h 中声明时并且在你的程序中没有可用的函数原型(prototype),gcc然后将隐含地为 gets_s 提供原型(prototype)返回 int (隐式函数声明)。

关于 gcc用 C11 编译是用选项 -std=c11 完成的在 gcc 4.7 和 -std=c1x在旧gcc .请注意,截至今天,在 gcc 中支持 C11 (以及 glibc 或 eglibc 等 Linux C 库)仍然非常不完整并且 gets_s即使启用 C11 选项,也可能不会声明(更不用说扩展是可选的)。

关于c - 如何使用 gets_s 在 C 中使用 atoi 转换 - 包含示例代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8995738/

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