C、结构、简单示例抗议语法已更新
更新:
我发现了错误,单引号错误。将其替换为双引号并且工作正常。很抱歉给您带来不便,但很快就发现了。
=================================================== =========================
这是一个结构声明和访问其数据的简单示例,但我得到了错误列表抱怨“字符常量对于其类型来说太长”,并且“在 printf 中传递参数 1 使得指针来自整数而不进行强制转换”。因此,程序会因溢出而崩溃。 Nombre 是名字,apellido 是姓氏,edad 是年龄。试图将所有内容收集在一个结构中,并打印通过指针访问它们的结果。 (为什么我必须通过指针而不是简单地通过点符号来访问它们?)
#include <stdio.h>
#include <stdlib.h>
struct estructura_amigo {
char nombre[30];
char apellido[40];
char telefono[10];
int edad;
};
struct estructura_amigo amigo = {
'Juanjo',
'Lopez',
'592-0483',
30
};
struct estructura_amigo *p_amigo;
int main()
{
p_amigo = &amigo;
printf( '%s tiene ', p_amigo->apellido );
printf( '%i años ', p_amigo->edad );
printf( 'y su teléfono es el %s.\n' , p_amigo->telefono );
}
UPDATED:
I found the bug, single quotes wrong. Replaced it by double quotes and worked fine. Sorry for the inconvenience but caught it soon.
===========================================================================
This is a simple example of what should be a structure declaration and access to its data, but I get a list of errors complaining that the "character constant is too long for its type" and also "passing of argument 1 in printf makes pointer from integer without a cast". So, the program crashes with overflow stuff. Nombre is first name, apellido is last name, and edad is age. Was trying to gather all that in a structure and the print the results accessing them through a pointer. (Why do I have to access them through a pointer and not simply by the dot notation?)
#include <stdio.h>
#include <stdlib.h>
struct estructura_amigo {
char nombre[30];
char apellido[40];
char telefono[10];
int edad;
};
struct estructura_amigo amigo = {
'Juanjo',
'Lopez',
'592-0483',
30
};
struct estructura_amigo *p_amigo;
int main()
{
p_amigo = &amigo;
printf( '%s tiene ', p_amigo->apellido );
printf( '%i años ', p_amigo->edad );
printf( 'y su teléfono es el %s.\n' , p_amigo->telefono );
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用双引号,就像这样
在 C 中单引号 = char,双引号 = string。
并且
应该可以工作,不需要获取指向结构的指针。
Use double quotes, like this
In C single quote = char, double quote = string.
And
should work, no need to get a pointer to the struct.