struct tm 的替代方案
是否存在任何其他替代数据结构来代替 struct tm(与该结构分配相同的内存)?这样我就可以使用 strftime
而不声明
我知道依赖隐式声明不好,但我在一个问题中遇到了这个问题面试。
编辑:准确地说,我被要求使用标准库函数打印相应整数的月份,但不允许包含任何头文件。
Does there exist any other alternative data structure instead of struct tm
(having same memory allocated as this structure) ? So that I could use strftime
without declaring <time.h>
I am aware of the fact that relying on implicit declaration is not good,but I faced this question in an interview.
EDIT: To be precise I was asked to print the month for a corresponding integer using standard library function,but not allowed to include any header file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不,您需要使用 time.h 包含文件。
但是,如果您确实想使用 strftime 并在没有错误或警告的情况下进行编译,则可以在 C 文件中重新定义结构数据类型,以及要使用的函数原型,而不包含该文件。您可以将结构类型命名为不同的名称,只要它与 time.h 文件中当前的名称相匹配即可。
No, you need to use time.h include file.
However, if you really want to use strftime and compile without errors or warnings, you could redefine the struct data type in your C file, and also the function prototype to use without including that file. You could call your struct type a different name as long as it matches up with the one currently in your time.h file.
我唯一的想法是面试官期望打印月份字符串,使用您自己的月份名称 const char 数组忽略语言环境,或者您应该停止的那些定义不明确的“交互式”问题之一并不断提问以澄清面试官真正想要的是什么。您想要明确地表达您想知道面试官正在寻找什么类型的答案。例如,只是一个简短的代码片段,忽略错误检查和区域设置或可重入问题等细节,或者某些非标准嵌入式或遗留环境的答案,寻找另一个标准 C 库函数(ctime??),或平台/操作系统具体答案?
ObCode:
或者如果是 Unix/Linux 系统上的疯狂“横向思考者”:
纯粹是个坏主意。 :)
The only thoughts I have are either the interviewer expected printing month strings, ignoring locale using your own const char array of month names, or one of those ill-defined "interactive" questions where you are suppose to stop and keep asking questions to clarify what the interviewer actually wants. Explicitly you want to express that you want to know what type of answer the interviewer is looking for. For example, just a short code fragment, ignoring details like error-checking and locale or reentrant issues, or an answer for some non-standard embedded or legacy environment, looking for another Standard C Library functions (ctime??), or a platform/OS specific answer?
ObCode:
Or if a wildly "lateral thinker" on a Unix/Linux system:
Pure bad idea. :)
只要您不需要访问 struct tm 的成员,您就可以简单地使用它的前向声明:
但是要使用 strftime(),您还需要一个 its< 的声明/em> 原型。你不想在他们认为这种可疑做法有用的地方工作。
So long as you do not need to access members of
struct tm
you can simply use a forward declaration of it thus:But to use strftime() you'd also need a declaration of its prototype. You don't want to work anywhere where they think such dubious practices are useful.
使用库函数要求您包含一个头文件...
打印出月份名称 - 我假设您被允许
stdio.h
- 与是否或不可以使用strftime
。我对你的编辑很准确。仅使用
int
就可以打印与之关联的月份。但正如已经提到的,打印本身需要包含它自己的......Using a library function requires that you include a header file...
Printing out a month name - I'm assuming you are allowed
stdio.h
- is independant of whether or not you can usestrftime
.I'm being exact about your edit. Using only an
int
you can print a month associated with it. But printing itself, as has been mentioned, requires an include of its own...