Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C Programming Language

help me out


   Hi all,

You are given have a datatype, say X in C. The requirement is to get
the size of the datatype, without declaring a variable or a pointer
variable of that type, And, of course without using sizeof operator !

        please tell me how this can be done.

ujjwal wrote:
>    Hi all,

> You are given have a datatype, say X in C. The requirement is to get
> the size of the datatype, without declaring a variable or a pointer
> variable of that type, And, of course without using sizeof operator !

>         please tell me how this can be done.

Why would you want to, except for a homework assignment?

--
Ian Collins.

ujjwal wrote:
>    Hi all,

> You are given have a datatype, say X in C. The requirement is to get
> the size of the datatype, without declaring a variable or a pointer
> variable of that type, And, of course without using sizeof operator !

>         please tell me how this can be done.

Why? Any sane programmer would use sizeof() so this must be homework.
Why should we do your homework?

Bjrn

On Jun 5, 2:47 pm, "B. Augestad" <b@metasystems.no> wrote:

> ujjwal wrote:
> >    Hi all,

> > You are given have a datatype, say X in C. The requirement is to get
> > the size of the datatype, without declaring a variable or a pointer
> > variable of that type, And, of course without using sizeof operator !

> >         please tell me how this can be done.

> Why? Any sane programmer would use sizeof() so this must be homework.
> Why should we do your homework?

> Bjrn

Hi
 you all are right but this not a homework I saw this problem on net
and tried to solve it
and after trying I posted it here

ujjwal

Ian Collins <ian-n@hotmail.com> writes:
> ujjwal wrote:
>>    Hi all,

>> You are given have a datatype, say X in C. The requirement is to get
>> the size of the datatype, without declaring a variable or a pointer
>> variable of that type, And, of course without using sizeof operator !

>>         please tell me how this can be done.

> Why would you want to, except for a homework assignment?

If he doesn't know, surely asking here is indeed doing his homework
assuming he learns something? It's not like he's asking you to spot the
"ready made" bug in a coding assignment.
On Jun 5, 2:39 pm, ujjwal <singhujj@gmail.com> wrote:

>    Hi all,

> You are given have a datatype, say X in C. The requirement is to get
> the size of the datatype, without declaring a variable or a pointer
> variable of that type, And, of course without using sizeof operator !

>         please tell me how this can be done.

one method :-

//to find size of double :-
printf("%d\n",(int)((char *)((double *)0 + 1)));

On Jun 5, 3:11 pm, Anu <annu_iyer2@yahoo.co.in> wrote:

> On Jun 5, 2:39 pm, ujjwal <singhujj@gmail.com> wrote:

> >    Hi all,

> > You are given have a datatype, say X in C. The requirement is to get
> > the size of the datatype, without declaring a variable or a pointer
> > variable of that type, And, of course without using sizeof operator !

> >         please tell me how this can be done.

> one method :-

> //to find size of double :-
> printf("%d\n",(int)((char *)((double *)0 + 1)));

Thnx Annu

Don't spend time solving stupid problems, spend time learning how to
code properly in C.

Bjrn

ujjwal wrote, On 05/06/07 10:53:

> On Jun 5, 2:47 pm, "B. Augestad" <b@metasystems.no> wrote:
>> ujjwal wrote:
>>>    Hi all,
>>> You are given have a datatype, say X in C. The requirement is to get
>>> the size of the datatype, without declaring a variable or a pointer
>>> variable of that type, And, of course without using sizeof operator !
>>>         please tell me how this can be done.
>> Why? Any sane programmer would use sizeof() so this must be homework.
>> Why should we do your homework?

> Hi
>  you all are right but this not a homework I saw this problem on net
> and tried to solve it
> and after trying I posted it here

I suggest you search this group for all the reasons attempting to do so
is a bad idea. Basically, sizeof is part of the language because there
is no portable way to implement a sizeof replacement.

If you want people to tell you what is wrong with your attempt the you
can post it here.
--
Flash Gordon

ujjwal said:

<the usual sizeof-sans-sizeof question>

>  you all are right but this not a homework I saw this problem on net
> and tried to solve it

The correct solution is that the requirement is broken. Use sizeof.
That's what it's for.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.

ujjwal said:

> On Jun 5, 3:11 pm, Anu <annu_iyer2@yahoo.co.in> wrote:

>> //to find size of double :-
>> printf("%d\n",(int)((char *)((double *)0 + 1)));

> Thnx Annu

Why are you thanking him for giving you incorrect information? You could
have thought up a wrong answer all by yourself.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.

ujjwal wrote, On 05/06/07 11:38:

> On Jun 5, 3:11 pm, Anu <annu_iyer2@yahoo.co.in> wrote:
>> On Jun 5, 2:39 pm, ujjwal <singhujj@gmail.com> wrote:

>>>    Hi all,
>>> You are given have a datatype, say X in C. The requirement is to get
>>> the size of the datatype, without declaring a variable or a pointer
>>> variable of that type, And, of course without using sizeof operator !
>>>         please tell me how this can be done.
>> one method :-

>> //to find size of double :-
>> printf("%d\n",(int)((char *)((double *)0 + 1)));

> Thnx Annu

Don't thank him too much. It invokes undefined behaviour by doing
arithmetic on a null pointer meaning anything can happen, including
crashing your computer or causing it to mutate in to a badly constructed
ZX80. Also, the conversion from pointer to int is not guaranteed to be
to be a simple byte count (e.g. it is not on the Cray Vector machine)
and even if it is null pointers might convert to something other than an
int value of 0 (although an integer *constant* of zero is a special case
guaranteed to be a null pointer constant).

That's just what I can think of off the top of my head.

Both of you search the group for the repeated long discussions of this.
--
Flash Gordon

On Tue, 05 Jun 2007 11:59:07 +0200, in comp.lang.c , Richard

It kinda depends. When someone makes a post which quite literally is a
copypaste of their homework eg they "Q1. write a code fragment to do
xxxx", especially with daft conditions such as "do not use any
function from stdlib.h", they should get short shrift for being
insufficiently motivated even to make an effort.

OTOH If they post something which shows some effort has been made to
solve the problem and ask for pointers, then help should be
forthcoming. .
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
 Therefore, if you write the code as cleverly as possible, you are,
 by definition, not smart enough to debug it."
--Brian Kernighan

On Jun 5, 6:12 am, Flash Gordon <s@flash-gordon.me.uk> wrote:

#define ICKY_MACRO_SIZEOF(some_type, some_size) \
{ \
some_type arr[2]; \
some_size = (char*)&arr[1] - (char*)&arr[0]; \

}

#include <stdio.h>
typedef struct foobar {
    int             foo;
    double          bar;
    char            barfoo[7];

}   foobar_t;

int             main(void)
{
    size_t          some_size;
    ICKY_MACRO_SIZEOF(char, some_size);
    printf("ICKY_MACRO_SIZEOF for char = %u\n", (unsigned) some_size);
    ICKY_MACRO_SIZEOF(short, some_size);
    printf("ICKY_MACRO_SIZEOF for short = %u\n", (unsigned)
some_size);
    ICKY_MACRO_SIZEOF(int, some_size);
    printf("ICKY_MACRO_SIZEOF for int = %u\n", (unsigned) some_size);
    ICKY_MACRO_SIZEOF(long, some_size);
    printf("ICKY_MACRO_SIZEOF for long = %u\n", (unsigned) some_size);
    ICKY_MACRO_SIZEOF(long long, some_size);
    printf("ICKY_MACRO_SIZEOF for long long = %u\n", (unsigned)
some_size);
    ICKY_MACRO_SIZEOF(size_t, some_size);
    printf("ICKY_MACRO_SIZEOF for size_t = %u\n", (unsigned)
some_size);
    ICKY_MACRO_SIZEOF(float, some_size);
    printf("ICKY_MACRO_SIZEOF for float = %u\n", (unsigned)
some_size);
    ICKY_MACRO_SIZEOF(double, some_size);
    printf("ICKY_MACRO_SIZEOF for double = %u\n", (unsigned)
some_size);
    ICKY_MACRO_SIZEOF(long double, some_size);
    printf("ICKY_MACRO_SIZEOF for long double = %u\n", (unsigned)
some_size);
    ICKY_MACRO_SIZEOF(char *, some_size);
    printf("ICKY_MACRO_SIZEOF for char * = %u\n", (unsigned)
some_size);
    ICKY_MACRO_SIZEOF(long double *, some_size);
    printf("ICKY_MACRO_SIZEOF for long double * = %u\n", (unsigned)
some_size);
    ICKY_MACRO_SIZEOF(foobar_t, some_size);
    printf("ICKY_MACRO_SIZEOF for foobar_t = %u\n", (unsigned)
some_size);
    ICKY_MACRO_SIZEOF(foobar_t *, some_size);
    printf("ICKY_MACRO_SIZEOF for foobar_t * = %u\n", (unsigned)
some_size);
    return 0;

On Jun 5, 8:20 pm, user923005 <dcor@connx.com> wrote:

> On Jun 5, 6:12 am, Flash Gordon <s@flash-gordon.me.uk> wrote:
> #define ICKY_MACRO_SIZEOF(some_type, some_size) \
> { \
> some_type arr[2]; \
> some_size = (char*)&arr[1] - (char*)&arr[0]; \
> }

Of course, it fails in at least two distinct instances I can think of.
user923005 wrote, On 06/06/07 04:29:

> On Jun 5, 8:20 pm, user923005 <dcor@connx.com> wrote:
>> On Jun 5, 6:12 am, Flash Gordon <s@flash-gordon.me.uk> wrote:
>> #define ICKY_MACRO_SIZEOF(some_type, some_size) \
>> { \
>> some_type arr[2]; \
>> some_size = (char*)&arr[1] - (char*)&arr[0]; \
>> }

> Of course, it fails in at least two distinct instances I can think of.

That's good, it saves me having to think of instances in which it fails :-)
--
Flash Gordon

On Tue, 05 Jun 2007 02:39:03 -0700, ujjwal <singhujj@gmail.com> wrote:
>   Hi all,

>You are given have a datatype, say X in C. The requirement is to get
>the size of the datatype, without declaring a variable or a pointer
>variable of that type, And, of course without using sizeof operator !

>        please tell me how this can be done.

Here's a suggestion.  Would a function definition strictly be considered a
variable?  Are there any types that would fail?

#include <stdio.h>

typedef struct
{
  int i;
  double d;

} S;

static void dummy(){;}
#define GETSIZE(T) (int)((char *)&((T*)dummy)[1] - (char *)&((T*)dummy)[0])
int main()
{
  printf("int size = %d\n", GETSIZE(int));
  printf("double size = %d\n", GETSIZE(double));
  printf("char size = %d\n", GETSIZE(char));
  printf("S size = %d\n", GETSIZE(S));
  return 0;

}

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

neildfergu@hotmail.com wrote:
> On Tue, 05 Jun 2007 02:39:03 -0700, ujjwal <singhujj@gmail.com> wrote:

>>   Hi all,

>>You are given have a datatype, say X in C. The requirement is to get
>>the size of the datatype, without declaring a variable or a pointer
>>variable of that type, And, of course without using sizeof operator !

>>        please tell me how this can be done.

> Here's a suggestion.  Would a function definition strictly be considered a
> variable?  Are there any types that would fail?

Depending on the implementation, all types can fail. What you're doing isn't
allowed in standard C.

getsize.c: In function main:
getsize.c:13: error: ISO C forbids conversion of function pointer to object
pointer type
getsize.c:13: error: ISO C forbids conversion of function pointer to object
pointer type
getsize.c:14: error: ISO C forbids conversion of function pointer to object
pointer type
getsize.c:14: error: ISO C forbids conversion of function pointer to object
pointer type
getsize.c:15: error: ISO C forbids conversion of function pointer to object
pointer type
getsize.c:15: error: ISO C forbids conversion of function pointer to object
pointer type
getsize.c:16: error: ISO C forbids conversion of function pointer to object
pointer type
getsize.c:16: error: ISO C forbids conversion of function pointer to object
pointer type
On Fri, 08 Jun 2007 22:07:13 +0200, Harald van D?k <true@gmail.com> wrote:

Thank you.  This is interesting: in  the ISO C99 standard Annex J.5.7
(Portability Issues/Common Extensions) it says "A pointer to a function may be
cast to a pointer to an object or to void, allowing a function to be inspected
or modified." Does that apply to this case?

Neil Ferguson

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

neildfergu@hotmail.com wrote On 06/08/07 17:02,:

> Thank you.  This is interesting: in  the ISO C99 standard Annex J.5.7
> (Portability Issues/Common Extensions) it says "A pointer to a function may be
> cast to a pointer to an object or to void, allowing a function to be inspected
> or modified." Does that apply to this case?

    The capability is described as a "common extension,"
that is, as something that many implementations allow
but is not guaranteed by the language.  Some compilers
may let you get away with it, but if another compiler
forbids it you have no grounds for complaint.

    IMHO, the correct answer to the O.P.'s question

> You are given have a datatype, say X in C. The requirement is to get
> the size of the datatype, without declaring a variable or a pointer
> variable of that type, And, of course without using sizeof operator !

... is to use `sizeof(X)'.  The question doesn't seek to
explore one's knowledge of C, but one's ability to spot
and reject a stupid specification.

--
Eric.Sos@sun.com

Eric Sosman <Eric.Sos@sun.com> writes:

[...]

>     IMHO, the correct answer to the O.P.'s question

>> You are given have a datatype, say X in C. The requirement is to get
>> the size of the datatype, without declaring a variable or a pointer
>> variable of that type, And, of course without using sizeof operator !

> ... is to use `sizeof(X)'.  The question doesn't seek to
> explore one's knowledge of C, but one's ability to spot
> and reject a stupid specification.

I don't *entirely* agree.  There are (clumsy) solutions involving
pointer arithmetic.  Being able to determine the size of a type
without using sizeof does demonstrate a knowledge of how pointer
arithmetic works in C -- and there could be problems requiring similar
solutions for which the language doesn't provide a built-in operator.
Determining the alignment for a type is one example; offsetof would be
another if it weren't already declared in <stddef.h>.

But of course there is absolutely no reason to determine the size of a
type without using sizeof in the real world.  That's what sizeof is
for, after all.

IMHO, a better response to the OP's question is:

    *Why* can't you use sizeof?

I wouldn't be inclined to provide a solution until that question is
answered.

--
Keith Thompson (The_Other_Keith) k@mib.org  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center             <*>  <http://users.sdsc.edu/~kst>
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc