|
|||
|
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. 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. He want me to solve the problem using standard library function,but without declaring any header file and of-course I was not allowed to copy paste either from <time.h>.What I was told that if I want to use struct tm it's the same layout as http://en.wikipedia.org/wiki/Time.h,that is the fifth member is the tm_mon. |
|
|
||||
|
||||
|
|
|
|||
|
Debanjan wrote:
> 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. > > 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. That's about as bad as being asked to complete a driving test without using a car. You can't safely use any standard library functions without including the headers that declare them and their data structures. -- Ian Collins |
|
|||
|
On 2010-03-05, Debanjan <debanjan4you@gmail.com> wrote:
> He want me to solve the problem using standard library function,but > without declaring any header file and of-course I was not allowed to > copy paste either from <time.h>.What I was told that if I want to use > struct tm it's the same layout as http://en.wikipedia.org/wiki/Time.h,that > is the fifth member is the tm_mon. Sounds stupid to me. I would see no reason to attempt such a thing, I also don't necessarily think it's a good thing to make the assumption that struct tm has exactly those members in that order. It may or may not be guaranteed, I can't remember, but... I sure wouldn't wanna bet on it. -s -- Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated! |
|
|||
|
> Sounds stupid to me. *I would see no reason to attempt such a thing, I
> also don't necessarily think it's a good thing to make the assumption that > struct tm has exactly those members in that order. *It may or may not be > guaranteed, I can't remember, but... I sure wouldn't wanna bet on it. May be he wants some platform specific stuffs ! |
|
|||
|
On 2010-03-05, Debanjan <debanjan4you@gmail.com> wrote:
>> Sounds stupid to me. *I would see no reason to attempt such a thing, I >> also don't necessarily think it's a good thing to make the assumption that >> struct tm has exactly those members in that order. *It may or may not be >> guaranteed, I can't remember, but... I sure wouldn't wanna bet on it. > May be he wants some platform specific stuffs ! Sounds like a pretty bad interview question to me. Look at it this way: It doesn't sound like a question I'd be able to answer. I was on the language committee for about a decade, and I've done compiler maintenance. If a question on an interview sounds to me like it's unanswerable, I respectfully submit that the question is probably poorly chosen. ![]() -s -- Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated! |
|
|||
|
On 2010-03-05, Debanjan <debanjan4you@gmail.com> wrote:
>> Sounds stupid to me. *I would see no reason to attempt such a thing, I >> also don't necessarily think it's a good thing to make the assumption that >> struct tm has exactly those members in that order. *It may or may not be >> guaranteed, I can't remember, but... I sure wouldn't wanna bet on it. > > May be he wants some platform specific stuffs ! Maybe he's an imbecile. You don't want to work for him. -- Andrew Poelstra http://www.wpsoftware.net/andrew |
|
|||
|
Ian Collins <ian-news@hotmail.com> writes:
> You can't safely use any standard library functions without including > the headers that declare them and their data structures. I don't recommend doing it, but some standard library functions may legitimately be used without including the appropriate standard header: 7.1.4 Use of library functions .... 2 Provided that a library function can be declared without reference to any type defined in a header, it is also permissible to declare the function and use it without including its associated header. -- "...what folly I commit, I dedicate to you." --William Shakespeare, _Troilus and Cressida_ |
|
|||
|
On 3/5/2010 3:08 PM, Debanjan wrote:
> 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> No reliable way. The Standard specifies the names and types of various elements of a struct tm, but does not specify their arrangement. Also, it permits the implementation to include additional, unspecified fields in the struct. As far as I can tell, you cannot even figure out how big a struct tm is, never mind its internal arrangement and its alignment. > 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. Hold on: The "corresponding integer" is given? That is, you've already got a number 0,1,...,11 and you need to print the name of the month? If that's the task, why bother with <time.h> at all? -- Eric Sosman esosman@ieee-dot-org.invalid |
|
|||
|
Eric Sosman <esosman@ieee-dot-org.invalid> writes:
> On 3/5/2010 3:08 PM, Debanjan wrote: >> 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> > > No reliable way. The Standard specifies the names and > types of various elements of a struct tm, but does not specify > their arrangement. Also, it permits the implementation to > include additional, unspecified fields in the struct. As far > as I can tell, you cannot even figure out how big a struct tm > is, never mind its internal arrangement and its alignment. [...] What's wrong with sizeof (struct tm)? -- Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" |
|
|||
|
Ben Pfaff wrote:
> Ian Collins <ian-news@hotmail.com> writes: > >> You can't safely use any standard library functions without including >> the headers that declare them and their data structures. > > I don't recommend doing it, but some standard library functions > may legitimately be used without including the appropriate > standard header: > > 7.1.4 Use of library functions > .... > 2 Provided that a library function can be declared without > reference to any type defined in a header, it is also > permissible to declare the function and use it without > including its associated header. That's why I added "and their data structures"! In the OP's context, struct tm is a classic case of a structure defined (by the standard) to include minimum set of members. An implementation is free to add some of its own. -- Ian Collins |
|
|||
|
Keith Thompson wrote:
> Eric Sosman <esosman@ieee-dot-org.invalid> writes: >> On 3/5/2010 3:08 PM, Debanjan wrote: >>> 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> >> No reliable way. The Standard specifies the names and >> types of various elements of a struct tm, but does not specify >> their arrangement. Also, it permits the implementation to >> include additional, unspecified fields in the struct. As far >> as I can tell, you cannot even figure out how big a struct tm >> is, never mind its internal arrangement and its alignment. > [...] > > What's wrong with sizeof (struct tm)? You have to include <time.h>! -- Ian Collins |
|
|||
|
Ian Collins <ian-news@hotmail.com> writes:
> Keith Thompson wrote: >> Eric Sosman <esosman@ieee-dot-org.invalid> writes: >>> On 3/5/2010 3:08 PM, Debanjan wrote: >>>> 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> >>> No reliable way. The Standard specifies the names and >>> types of various elements of a struct tm, but does not specify >>> their arrangement. Also, it permits the implementation to >>> include additional, unspecified fields in the struct. As far >>> as I can tell, you cannot even figure out how big a struct tm >>> is, never mind its internal arrangement and its alignment. >> [...] >> >> What's wrong with sizeof (struct tm)? > > You have to include <time.h>! Whoops, context error. -- Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" |
|
|||
|
On 3/5/2010 4:26 PM, Keith Thompson wrote:
> Eric Sosman<esosman@ieee-dot-org.invalid> writes: >> On 3/5/2010 3:08 PM, Debanjan wrote: >>> 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> >> >> No reliable way. The Standard specifies the names and >> types of various elements of a struct tm, but does not specify >> their arrangement. Also, it permits the implementation to >> include additional, unspecified fields in the struct. As far >> as I can tell, you cannot even figure out how big a struct tm >> is, never mind its internal arrangement and its alignment. > [...] > > What's wrong with sizeof (struct tm)? Nothing -- except that he's operating under the silly restriction of not including any Standard headers. So, where does he get a struct tm declaration that's known to be the same size as the implementation's own version? Hmmm: It occurs to me that if he knew the size, or an upper bound on the size, he could dodge the alignment issue by obtaining the memory from malloc() (declared free-hand). He still wouldn't know how to find the various elements within the struct, though. -- Eric Sosman esosman@ieee-dot-org.invalid |
|
|||
|
Ian Collins <ian-news@hotmail.com> writes:
> Ben Pfaff wrote: >> Ian Collins <ian-news@hotmail.com> writes: >> >>> You can't safely use any standard library functions without including >>> the headers that declare them and their data structures. >> >> I don't recommend doing it, but some standard library functions >> may legitimately be used without including the appropriate >> standard header: >> >> 7.1.4 Use of library functions >> .... >> 2 Provided that a library function can be declared without >> reference to any type defined in a header, it is also >> permissible to declare the function and use it without >> including its associated header. > > That's why I added "and their data structures"! I can use exit() by declaring it myself, without including <stdlib.h>, but your statement (quoted above) says that I can't. So your statement is wrong. -- "IMO, Perl is an excellent language to break your teeth on" --Micah Cowan |
|
|||
|
Seebs wrote:
> Sounds like a pretty bad interview question to me. Sounds like a terrific one to me. You can just get up, shake his hand, and leave without wasting any more of your time. Brian -- Day 396 of the "no grouchy usenet posts" project |
|
|
![]() |
| Popular Tags in the Forum |
| interview, question |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [Spam] Java forum + article+ interview question atwww.spamcode4you.spam | Andrew Thompson | Newsgroup comp.lang.java.gui | 0 | 06-22-2009 05:29 AM |
| Java forum + article+ interview question at www.sourcecode4you.com | ramesh das | Newsgroup comp.lang.java.beans | 0 | 06-22-2009 05:16 AM |
| Re: experimental design question | Sigurd Hermansen | Newsgroup comp.soft-sys.sas | 0 | 12-12-2008 09:28 PM |
| Re: PROC SQL question | Huang, JS | Newsgroup comp.soft-sys.sas | 0 | 03-22-2007 02:39 PM |
| Re: Help needed with some SAS interview questions! | Jack Hamilton | Newsgroup comp.soft-sys.sas | 0 | 04-01-2006 06:38 AM |