另一个初始化使得从指针进行整数没有铸造。错误我不明白

发布于 2025-01-30 03:18:44 字数 1488 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无名指的心愿 2025-02-06 03:18:44

替换:

char z = "c";

char z = 'c';

Replace:

char z = "c";

with

char z = 'c';
你好,陌生人 2025-02-06 03:18:44

“ C”是具有数组类型char [2]的字符字面字符。它在内存中表示,例如数组{'c','\ 0'}。用作初始化器表达式,它被隐式转换为指针,转换为其类型char *的第一个元素。因此,在此声明中,

char z = "c";

char z = &"c"[0];

要试图初始化对象z具有类型char的指针的指针chode> char *。您需要使用字符串字符串,您需要使用一个整数字符常数,例如

char z = 'c';

printf这样的呼叫中的

printf("%d\n %lf\n %c\n", m, fx, z);

length Modifier l以这种格式%lf是多余的,没有效果。您也可以写作

printf("%d\n %f\n %c\n", m, fx, z);

,您应该在printf的调用中将指针投入到类型void *

printf("%p\n %p\n %p\n", ( void * )&m, ( void * )&fx, ( void * )&z);

printf的呼叫

printf("%p\n %p\n %p\n", &ptr_m, &ptr_fx, &ptr_z);

,似乎代替 意思是这个电话

printf("%p\n %p\n %p\n", ( void * )ptr_m, ( void * )ptr_fx, ( void *)ptr_z);

"c" is a character literal having the array type char[2]. It is represented in memory like array { 'c', '\0' }. Used as an initializer expression it is implicitly converted to a pointer to its first element of the type char *. So in this declaration

char z = "c";

that is equivalent to the declaration

char z = &"c"[0];

you are trying to initialize the object z having the type char with a pointer of the type char *. Instead of the string literal you need to use an integer character constant like

char z = 'c';

In calls of printf like this

printf("%d\n %lf\n %c\n", m, fx, z);

the length modifier l in this format %lf is redundant and has no effect. You may write

printf("%d\n %f\n %c\n", m, fx, z);

Also you should cast pointers to the type void * in calls of printf like this

printf("%p\n %p\n %p\n", ( void * )&m, ( void * )&fx, ( void * )&z);

And it seems instead of this call of printf

printf("%p\n %p\n %p\n", &ptr_m, &ptr_fx, &ptr_z);

you mean this call

printf("%p\n %p\n %p\n", ( void * )ptr_m, ( void * )ptr_fx, ( void *)ptr_z);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文