+ Reply to Thread
Results 1 to 7 of 7

Thread: Plz help- C CODE needed

  1. #1
    Junior Member
    Join Date
    Feb 2006
    Location
    India
    Posts
    8
    Rep Power
    8


    1) Can anyone give me the C code for printing the nos 1..n , u must not use any recursion , loops, switch case or branching statements.



    2) In 'C' , how to find the size of a structure without using the 'sizeof' operator. Also no instance of the strucure must be created?



    Edited by: HELP
    baba

  2. #2
    Junior Member
    Join Date
    Feb 2006
    Location
    Posts
    2
    Rep Power
    8


    This code is for printing 1 to n.


    voidprintUp(intstartNumber,intendNum ber)
    {
    start:

    if(startNumber> endNumber)
    {
    gotoend;&amp ;nbs p;
    }
    else
    {
    printf("%d\n",&amp ;nbs p;startNumber++);
    gotostart;&a mp;n bsp;
    }

    end:
    return;
    } Edited by: sivakumar

  3. #3
    Junior Member
    Join Date
    Feb 2006
    Location
    India
    Posts
    8
    Rep Power
    8
    I can't understand the code u gave. Can u explain it.
    What does the & amp; &p; etc means????

    Can U give me a solution that does not involve any if else statement (I mentioned it in the quesion??)

    baba

  4. #4
    Moderator
    Join Date
    Feb 2006
    Posts
    1,411
    Rep Power
    24
    hello



    In 'C' , how to find the size of a structure without using the 'sizeof' operator. Also no instance of the strucure must be created?



    jus try this out



    just declare a int variable after the structure..
    and see the memory address.. remove 2 bytes from the address.. the
    remaining is the size of the structure..



    hope it shud work .This is what i have found .. i donno its
    correct /not . jus check and post message 4 me if it works out



    bye



    sowmya






    http://livetolead.blogspot.com/
    all the best
    Arise Awake N Stop Not Until Ur Goal Is Reached!

  5. #5
    Senior Member
    Join Date
    Mar 2006
    Location
    India
    Age
    28
    Posts
    188
    Rep Power
    9


    the method given by sowmya is not correct...


    this is because if u declare an integer after the structure and display its address... its output would be something like 3150..


    how can u guess the size of struct from that...


    also if u declare an integer before and an integer after the structure.. even this wont work.. the difference between the address of the two integers will be 2 bytes...


    this is because memory is not allocated to the structure but to its instances...


    when we write the definition of a structure.. its only the definition and no memory is allocated. memory is allocated only when the instances of the structure are created...


    i think we can only calculate teh size of the structure theoritically...


    Eg: if we have a structure


    struct a
    {
    int a;
    float b;
    char c[5];
    };


    a takes 2 bytes, b takes 4 bytes, c takes 5 bytes...


    so the size of struct a is 11 bytes....


    to calculate it through a program i think sizeof is necessary...
    -------
    we make our habits...
    and our habits make us...

  6. #6
    Moderator
    Join Date
    Feb 2006
    Posts
    1,411
    Rep Power
    24
    hello



    its possible to find wait i will give the code!



    bye



    sowmya




    http://livetolead.blogspot.com/
    all the best
    Arise Awake N Stop Not Until Ur Goal Is Reached!

  7. #7
    Moderator
    Join Date
    Feb 2006
    Posts
    1,411
    Rep Power
    24
    hello



    This is one way with instances



    #include <stdio.h>

    void main()

    {

    struct s{

    int a;

    float b;

    char aa[10];

    }s1,s2;



    printf("size of s1 is : %d",sizeof(s1));

    printf("%u \t %u",&s1,&s2);

    printf("size of s2 is :%d",(char*)(void *)&s1-(char*)(void*)&s2);



    }





    This wd work .. But without creating intsances



    I think its not possible with out creating any instance even by using
    sizeof

    In sizeof also we are using sizeof(sturct s // here indirectly we are
    creating instance of structure) or sizeof(s1) ..

    Where s1 is an instance of structure s ..

    So we have to write a big program to take care of all the sizes and etc
    etc to get this done ..

    For more info just check the linux source code of sizeof .. That may
    help you ..


    http://livetolead.blogspot.com/
    all the best
    Arise Awake N Stop Not Until Ur Goal Is Reached!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Content Relevant URLs by vBSEO 3.5.1 PL1