Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.c

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 03-05-2010, 07:08 PM
Debanjan
Guest
 
Posts: n/a
Default An interview question

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.

Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 03-05-2010, 07:21 PM
Ian Collins
Guest
 
Posts: n/a
Default Re: An interview question

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
Reply With Quote
  #3 (permalink)  
Old 03-05-2010, 07:28 PM
Seebs
Guest
 
Posts: n/a
Default Re: An interview question

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!
Reply With Quote
  #4 (permalink)  
Old 03-05-2010, 07:43 PM
Debanjan
Guest
 
Posts: n/a
Default Re: An interview question

> 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 !
Reply With Quote
  #5 (permalink)  
Old 03-05-2010, 07:46 PM
Seebs
Guest
 
Posts: n/a
Default Re: An interview question

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!
Reply With Quote
  #6 (permalink)  
Old 03-05-2010, 07:47 PM
Andrew Poelstra
Guest
 
Posts: n/a
Default Re: An interview question

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
Reply With Quote
  #7 (permalink)  
Old 03-05-2010, 07:51 PM
Ben Pfaff
Guest
 
Posts: n/a
Default Re: An interview question

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_
Reply With Quote
  #8 (permalink)  
Old 03-05-2010, 08:24 PM
Eric Sosman
Guest
 
Posts: n/a
Default Re: An interview question

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
Reply With Quote
  #9 (permalink)  
Old 03-05-2010, 08:26 PM
Keith Thompson
Guest
 
Posts: n/a
Default Re: An interview question

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"
Reply With Quote
  #10 (permalink)  
Old 03-05-2010, 08:31 PM
Ian Collins
Guest
 
Posts: n/a
Default Re: An interview question

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
Reply With Quote
  #11 (permalink)  
Old 03-05-2010, 08:33 PM
Ian Collins
Guest
 
Posts: n/a
Default Re: An interview question

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
Reply With Quote
  #12 (permalink)  
Old 03-05-2010, 08:39 PM
Keith Thompson
Guest
 
Posts: n/a
Default Re: An interview question

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"
Reply With Quote
  #13 (permalink)  
Old 03-05-2010, 08:41 PM
Eric Sosman
Guest
 
Posts: n/a
Default Re: An interview question

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
Reply With Quote
  #14 (permalink)  
Old 03-05-2010, 09:05 PM
Ben Pfaff
Guest
 
Posts: n/a
Default Re: An interview question

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
Reply With Quote
  #15 (permalink)  
Old 03-05-2010, 09:30 PM
Default User
Guest
 
Posts: n/a
Default Re: An interview question

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
Reply With Quote
 
Reply

Popular Tags in the Forum
interview, question

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


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



All times are GMT. The time now is 06:53 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.