none
Technical Quiz on C / C++ RRS feed

  • Question

  • Hi guys,

    this is a technical quiz thread on C and C++ language. for all the correct answers, i'll marks the replies as answers.

    questions are welcomed from the participants. but they should be of good quality.

    and the rules are,

    1. the next question will be available only when i've got the answer for previous one.

    2. anyone can post a question but only after the previous question owner has answered the correct one.

    3. a valid question will also be marked as an answer if it is of good quality.
     
    4. max time given for the answer is 1 day.
     
    5. at the end of 24 hrs the answer will be posted.
     
    6. the 1st one to answer correct gets the benefit, if supported with proper reasons

    so, let's start...

    Monday, March 19, 2007 7:31 PM

Answers

  • Q. What is an incomplete type?

    Monday, March 19, 2007 7:33 PM
  • Incomplete Types


    An incomplete type is a type that describes an identifier but lacks information needed to determine the size of the identifier. An “incomplete type” can be:

    A structure type whose members you have not yet specified.
    A union type whose members you have not yet specified.
    An array type whose dimension you have not yet specified.
    The void type is an incomplete type that cannot be completed. To complete an incomplete type, specify the missing information. The following examples show how to create and complete the incomplete types.

    To create an incomplete structure type, declare a structure type without specifying its members. In this example, the ps pointer points to an incomplete structure type called student.
              struct student *ps;

    To complete an incomplete structure type, declare the same structure type later in the same scope with its members specified, as in
             struct student
             {
                   int num;
             }                   /* student structure now completed */

    To create an incomplete array type, declare an array type without specifying its repetition count. For example:
             char a[];  /* a has incomplete type */

    To complete an incomplete array type, declare the same name later in the same scope with its repetition count specified, as in
             char a[25]; /* a now has complete type */

     

    Monday, March 19, 2007 7:44 PM
  • Here goes the next question :

    Q 2 : Define precondition and post-condition to a member function.

    Tuesday, March 20, 2007 6:22 PM
  • Precondition: A precondition is a condition that must be true on entry
    to a member function. A class is used correctly if preconditions are
    never false. An operation is not responsible for doing anything
    sensible if its precondition fails to hold.

    For example, the interface
    invariants of stack class say nothing about pushing yet another element
    on a stack that is already full. We say that isful() is a precondition
    of the push operation.

    Post-condition: A post-condition is a condition
    that must be true on exit from a member function if the precondition
    was valid on entry to that function. A class is implemented correctly
    if post-conditions are never false.

    For example, after pushing an
    element on the stack, we know that isempty() must necessarily hold.
    This is a post-condition of the push operation.

    Wednesday, March 21, 2007 12:58 PM
  • Now one question from my side....

    What is the value of NULL in C++ ?? Can we change it?? If yes than how and if no than why??

    Wednesday, March 21, 2007 1:00 PM
  • The value of NULL in C++ is integer 0.
    We can change the value of NULL.

    To change the value of NULL, we need to define its value using #define.

    e.g.

    #define NULL 1234
    #define NULL "rockstar"

    Both of the above will work. I've tested it on my compilers.
    Wednesday, March 21, 2007 5:04 PM
  • sorry aleemu, you are wrong. NULL has a value defined by default. To test, you can use the following code :

    #include<iostream.h>
    #include<conio.h>

    int main(void)
    {
            clrscr();
            cout<<NULL;       //will print 0.
            getch();

            return 0;
    }



    #include<iostream.h>
    #include<conio.h>

    #define NULL "sanket"

    int main(void)
    {
            clrscr();
            cout<<NULL;      //will print sanket.
            getch();

            return 0;
    }
    Wednesday, March 21, 2007 5:09 PM
  • Sanket Your answer is perfect... you do deserve the points but there is one more way to change the value of the NULL.... so start grinding those brains and answer how can you change the value of NULL.... you wont find it in any books but people have been doing this technique not for null but for other purpose... so tell me what is it????
    Wednesday, March 21, 2007 5:13 PM
  • I don't know whats the other way for it. I'm posting the next question.
    Thursday, March 22, 2007 5:56 PM
  • What are Proxy objects ?
    Thursday, March 22, 2007 6:01 PM
  • Write a C/C++ program to print something without using ; (semicolon) in the entire program.

    Easy one though, but good one to start with.
    Friday, March 23, 2007 11:34 AM
  • How about this..

    #include<stdio.h>

    void main()
    {
    if(printf("This is magic"))
    {}
    }



    It works i have checked it.. nice one man...
    Saturday, March 24, 2007 12:36 PM
  • Sanket i had posted the answer to that NULL question.. if you find it satisfactory than please do mark it as answer...

    Here comes the next question...
    Can you swap 2 integers without using any of the following :
    1. Pointers
    2. Temporary variable
    3. Any array
    4. Any mathematical operation.......
    Saturday, March 24, 2007 12:40 PM
  •  Sanket_Shah_734609 wrote:
    It's a nice question Harshil. I'm putting my trial here for the question you've asked. Please tell me whether I'm correct or not. Then only, I'll mark it as answer.

    #include<stdio.h>

    #define test printf("Hello");

    void main(void)
    {
    test
    }



    hey man you did it a good way, but still there is a ; :P

    here is one of the possible solution
    //////////////////
    #include<stdio.h>
    void main() {
    while(!printf("rockstar"),0){}
    }
    //////////another solution

    #include<stdio.h>
    void main() {
    if(printf("rockstar")){}
    }

    there are more, guys try finding it out :)

    plz mark it as answer now :)


    Saturday, March 24, 2007 2:13 PM
  • Here goes the program...

    #include<stdio.h>
    #include<conio.h>

    void main(void)
    {
            int x = 5, y = 10;

            clrscr();

            y = x^y;
            x = x^y;
            y = x^y;

            printf("%d\t%d", x, y);

            getch();
    }
    Saturday, March 24, 2007 3:23 PM
  • Thanks for such a nice question harshil. Smile.

    Keep posting such questions, and please post them once the previous one has been answered. Waiting for more of this kind... Wink
    Saturday, March 24, 2007 3:33 PM
  • If there are copy contructor and a overloaded '=' operator than when you pass an object as an argument(value) to a function, what will be used???
    other part of the same question..
    In same situation, when you use object = object, what will be used to copy the object???
    Sunday, March 25, 2007 4:43 AM
  • in the program, we perform Exclusive OR operations between 2 numbers. As the integer numbers are stored as bytes, we can perform easily Exclusive OR Operations.

    The XOR operation works as follows :

    1st digit            2nd digit               XOR bit
    0                     0                           0
    0                     1                           1
    1                     0                           1
    1                     1                           1

    Here is the trace for XOR when you take two numbers a & b with values 10 and 5 respectively.

    after 1st XOR Operation
    -=-=-=-=-=-=-=-=-=-=-=-
    a --> 10 --> 1010
    b -->  5 --> 0101
    -----------------
    b --> 15 --> 1111


    after 2nd XOR Operation
    -=-=-=-=-=-=-=-=-=-=-=-
    a --> 10 --> 1010
    b --> 15 --> 1111
    -----------------
    a -->  5 --> 0101


    after 3rd XOR Operation
    -=-=-=-=-=-=-=-=-=-=-=-
    a -->  5 --> 0101
    b --> 15 --> 1111
    -----------------
    b --> 10 --> 1010

    Sunday, March 25, 2007 4:47 AM
  • when you pass as parameter, copy constructor will be used.

    else in the situation, like <<object>> = <<object>>, overloaded = is used.
    Sunday, March 25, 2007 5:42 PM
  • This is the right ans...
    just check out this code if any one has a doubt on it.. it will show when a copy constructor is called and when overloaded operator is called...

    #include<iostream.h>
    #include<conio.h>

    class vector
    {
        int tmp;
        public:
        vector(){}
        vector(int a = 0)
        {
            tmp = a;
        }
        vector( vector &v1)
        {
            cout<<"\nThis is copy contructor";
            tmp = v1.tmp;
        }

        vector & operator =(vector &v1)
        {
            cout<<"\nThis is the overloaded operator";
            tmp = v1.tmp;
            return *this;
        }
        friend void operator <<(ostream &cout, vector &v);
        friend void copy(vector &v1, vector v2);
    };
    void copy(vector &v1, vector v2)
    {
        v1.tmp = v2.tmp;
    }
    void operator <<(ostream &cout, vector &v)
    {
        cout<<v.tmp;
    }
    void main()
    {
        vector v1(4), v2(8), v3(12);
        clrscr();
        cout<<v1;
        v1 = v2;
        cout<<v1;
        copy(v2, v3);
        cout<<v2;
        getch();
    }
    Monday, March 26, 2007 3:53 AM
  • Is it possible to have a parameterized constructor and no default constructor??
    If no then why and if yes than how??
    Monday, March 26, 2007 3:56 AM
  • its upto us whether we want paprameterised constructor or not and also the default constructor

    i am giving example of both:

    class a

    {

    private x;

            a()       /*default constructor*/

        {

            x=0;

        }

                  a(int alpha)    /* parameterised constructor*/

                 {

                     x=alpha;

                 }

    }    /* end of class*/

    void main()

    {

    a obj1;  /* default constructor is run and x is initialised to 0.*/

    a obj2(76); /* parameterised constructor initialises the value of x to 76*/

     

    Monday, March 26, 2007 9:40 AM
  • Sorry kanav but you didnt understand the question... what i m asking is that if you have a parametrized constructor, than is it possible not to have a default constructor???
    Monday, March 26, 2007 12:01 PM
  • It is possible to have no default constructor when parameterized constructor is simple. The answer is simple. Put default values in member functions when defining them. This way, we need not to pass any values as arguments. The default values will be taken automatically.
    Monday, March 26, 2007 12:48 PM
  • This is the right ans.. actually the ans was just above my question.. check out the code i gave for question 6, their put the default constructor in comments and run the prog.. it will run perfectly without any warning or error, but after that try to remove the default values from the parameterized constructor.. and observe what outcome comes....
    Monday, March 26, 2007 3:23 PM
  • Write a code that prints the output as the code itself....(the whole code and nothing else)
    Monday, March 26, 2007 3:32 PM
  • Here goes the code

    char a[] = "int main(){ printf(b,34,a,34,10,34,b,34,10,10,a,10); }";
    char b[] = "char a[] = %c%s%c;%cchar b[] = %c%s%c;%c%c%s%c";

    int main(){ printf(b,34,a,34,10,34,b,34,10,10,a,10); }


    Tuesday, March 27, 2007 11:14 AM
  • Here goes the next question :

    Q 9. What is a dangling pointer?
    Tuesday, March 27, 2007 11:20 AM
  • Dangling pointers and wild pointers in computer programming are pointers that do not point to a valid object of the appropriate type, or to a distinguished null pointer value in languages which support this. Dangling pointers arise when an object is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory. As the system may reallocate the previously freed memory to another process, if the original program then dereferences the (now) dangling pointer, unpredictable behavior may result, as the memory may now contain completely different data. This is especially the case if the program writes data to memory pointed by a dangling pointer, as silent corruption of unrelated data may result, leading to subtle bugs that can be extremely difficult to find, or cause segmentation faults (*NIX) or general protection faults (Windows). If the overwritten data is bookkeeping data used by the system's memory allocator, the corruption can cause system instabilities. Wild pointers arise when a pointer is used prior to initialization to some known state, which is possible in some programming languages. They show the same eratic behaviour as dangling pointers, though they are less likely to stay undetected.
    Tuesday, March 27, 2007 11:29 AM
  • Give the output of the following program and explain it:

    class base

    {

        public:

        int bval;

        base()

        {

            bval=0;

        }

    };

    class deri:public base

    {

        public:

        int dval;

        deri()

        {

            dval=1;

        }

    };

    void SomeFunc(base *arr, int size)

    {

        for(int i=0; i<size; i++,arr++)

           cout<<arr->bval;

        cout<<endl;

    }

    int main()

    {

        base BaseArr[5];

        SomeFunc(BaseArr,5);

        deri DeriArr[5];

        SomeFunc(DeriArr,5);

    }



    Tuesday, March 27, 2007 5:30 PM
  • The output is
    00000
    01010

    This is really a nice one.. now i don know how well i can explain this but here it goes...
    Now as we know that we can point to the derived class using a base class pointer, so that is not a prob, thatz y we use a base * in the function that we use to print the value...

    Now the for base class output ie 00000 is simple which everyone knows about... the thing to notice is why we are getting 01010 in derived class??

    1st of all the memory is allocated consequently ie for lets say deriarr[2] its like this
    mem for ->bval
    mem for ->dval
    mem for ->bval
    mem for ->dval

    ie the derived class has memory of 4 bytes, 2 for bval, 2 for dval
    Now we can point to the derived class using base class pointer but when we derefrence the base pointer, it will dereff only 2 byte... so increment operator will move the add in function only by 2 bytes and not by 4 bytes...

    so now 1st we get the bval of derived class, after increment the pointer points to the dval of the same class instead of jumping to the next object...

    so we get the output as 01010

    try out this, in the function use the increment iperator 10 times,
    the output will be
    0101010101
    Tuesday, March 27, 2007 7:22 PM
  • Thanks for the overwhelming response from the participants of this thread !!!

    Here goes the next question. Please participate as much possible.

    What's the difference between deep copy and shallow copy ?

    Wednesday, March 28, 2007 6:20 PM
  • Whenever we copy a object to another, and if there is a dynamic memory allocation in the object, than a care is to be taken on how we copy the data...
    There are 2 ways to copy the data

    1. Shallow Copy.
      Consider two objects, A and B, which each refer to two memory blocks xi and yi.You can think of A and B as strings and of xi and yi as the characters they contain.

    In Shallow copy B is attached to the same memory block as A, i.e B is made to point at the same block as A. This results in a situation in which some data is shared between A and B, thus modifying the one will alter the other. The original memory block of B is now no longer referred to from anywhere.This results in a situation in which some data is shared between A and B, thus modifying the one will alter the other. The original memory block of B is now no longer referred to from anywhere.

    2. Deep Copy.
        An alternative are deep copies. Here the data is actually copied over.
    The result is different from the result a shallow copy gives. The advantage is that A and B do not depend on each other but at the cost of a slower more expensive copy.

    Wednesday, March 28, 2007 6:28 PM
  • Looks like the threads going dead.. buck up people.. here is a nice question to keep the heat up...

    Q. What is the difference between a Inline function and a Macro??
    The best answer gets marked as Answer...
    Friday, March 30, 2007 4:06 PM
  • In inline function the code of the inline function is replaced at the place where it was called by the COMPILER during compilation & linkage , while in case of MACRO the block in the macro is replaced even before compile begins, the text in macro is replaced by the PREPROCESSOR before the compilation process starts.

    please mark as answer, i have answered this question myself,even without having any reference, so, i guess i deserve the credit Smile
    Friday, March 30, 2007 5:10 PM
  • yes Harshil your ans is right, but there is also another major diff.. waiting for that one also.. can anyone tell me another major diff??
    Friday, March 30, 2007 6:38 PM
  • Unlike #define macros, inline functions avoid infamous macro errors since inline functions always evaluate every argument exactly once. In other words, invoking an inline function is semantically just like invoking a regular function, only faster:

    Also unlike macros, argument types are checked, and necessary conversions are performed correctly.



    Saturday, March 31, 2007 6:17 AM
  • So this is the final answer with major differences between the two..
    Harshil gave most of them so he does deserved to get marked..
    Ill list down the differences..
    1.) Their replacement in the code is different, macros are replaced before by preprocessor compile time, where as inline functions are replaced at linkage and compiling time by the compiler..
    2.) Inline functions are checked for any errors where as macros are not...

    3.) Now the diff that most of us forget..
         The passing of arguments, lets say when we pass 2*3 in macro, than 2*3 directly gets replaced, but in inline function 2*3 is evaluated to 6 and than 6 gets replaced in inline function...
    Saturday, March 31, 2007 10:06 AM
  • ahh thanks for the 3rd difference Smile i forgot about it Smile Human tends to forget the most easy things Stick out tongue hehe
    Saturday, March 31, 2007 12:31 PM
  • Thanks to all for your participation...
    Here goes the next question...


    What will happen if I say delete this?
    Sunday, April 1, 2007 5:06 PM
  • The program will not terminate in any case. Here goes the answer :

    destructor executed, but memory will not be freed (other than work done by destructor). If we have class Test and method Destroy { delete this } the destructor for Test will execute, if we have Test *var = new Test()
    1. pointer var will still be valid
    2. object created by new exists until explicitly destroyed by delete
    3. space it occupied can be reused by new
    4. delete may only be applied to a pointer by new or zero, applying delete to zero = no FX
    5. delete = delete objects
    6. delete[] – delete array
    7. delete operator destroys the object created with new by deallocating the memory assoc. with the object
    8. if a destructor has been defined fir a class delete invokes that desructor

    Sunday, April 1, 2007 5:31 PM
  • I think question 11 is a bit high level, will take some time for us to digest . May be question 11 should be asked as question 50  Stick out tongue
    Sunday, April 1, 2007 5:57 PM
  • Rightly said brother, rightly said...
    Sunday, April 1, 2007 6:00 PM
  • Thanks varun for supporting me.
    Sunday, April 1, 2007 6:07 PM
  • Turbo C++ is full of bugs. I don't have lot faith on it. The response is based on the experiments conducted by professionals in the virtual labs.
    Sunday, April 1, 2007 6:08 PM
  • try gcc
    Sunday, April 1, 2007 6:14 PM
  • @ Varun, Harshil,

    I'm sorry that I asked really a tough question. My next question will be a piece of cake for you. Smile
    Sunday, April 1, 2007 6:15 PM
  • Its good that you post such questions, it will increase the knowledge base..

    Sunday, April 1, 2007 6:20 PM
  • Yes its good to post such questions, i agree. But at an increase in level  of toughness Smile let the toughness level increase gradually Smile We got lot and lot of time to learn in this forum Smile
    Monday, April 2, 2007 4:35 AM
  • As promised, I'm putting down an easy question. But please give the reason also alongwith the answer...
    What will be the contents of one.txt file ?

    void main(void)
    {
        FILE *fp1,*fp2;
        fp1=fopen("one.txt","w");
        fp2=fopen("one.txt","w");
        fputc('A',fp1);
        fputc('B',fp2);
        fclose(fp1);
        fclose(fp2);
    }

    Monday, April 2, 2007 6:13 PM
  •  Sanket_Shah_734609 wrote:
    As promised, I'm putting down an easy question. But please give the reason also alongwith the answer...
    What will be the contents of one.txt file ?

    void main(void)
    {
    FILE *fp1,*fp2;
    fp1=fopen("one.txt","w");
    fp2=fopen("one.txt","w");
    fputc('A',fp1);
    fputc('B',fp2);
    fclose(fp1);
    fclose(fp2);
    }



    Nice one buddy.. though you say that its easy but its not so simple..
    Ok now when you open a file in fp1 and fp2, 2 buffers are created for both and its adress is passed in fp1 and fp2, so you have 2 different buffers now.. Your proceedings will be as per you code,
    The trick lies in the fclose, ie. now when you first close fp1, file with content as A gets saved, but when you close file fp2, file with B gets overwritten, so

    Finally you get the file with the content as only B, in it...

    Change the order of the fclose command, the output will change
    Monday, April 2, 2007 6:53 PM
  • That exactly what i thought varun, but i was lazy to try out the code and be 100% sure Smile but anyways, good one varun.
    Tuesday, April 3, 2007 12:57 PM

  • What will be the output of the following code ? Give reason.

    void main()
    {
        int i=-1,j=-1,k=0,l=2,m;
        m=i++&&j++&&k++||l++;
        printf("%d %d %d %d %d",i,j,k,l,m);
    }

    Tuesday, April 3, 2007 1:54 PM
  • Final answer would be
    i=0
    j=0
    k=1
    l=3
    m=1

    This one was quite easy  Smile i did it without any compiler Smile
    Tuesday, April 3, 2007 2:23 PM
  • Next question please..... Can i post the next question ?
    Tuesday, April 3, 2007 2:24 PM
  • Hey Harshil, your answer is right and you can post next question here. I'm not going to put any for this time. But please give the logic of deriving value of 'm' so that others can understand it. Smile
    Tuesday, April 3, 2007 3:37 PM
  • Here is the logic of how to get the value of 'm'

    i++ , j++, k++, l++<-- this is post-increment, so the value returned from these expr will be -1, -1, 0, and 2 respectively


    so lets dissect this equation
    m = (i++)   &&   (j++)   &&   (k++)   ||   (l++)   ;
    m = TRUE && TRUE && FALSE || TRUE

    m = FALSE || TRUE
    M = TRUE

    hence the value of m will be TRUE(non-zero)  which by default is 1



    Tuesday, April 3, 2007 3:53 PM
  • Waiting for the next question... I missed the last one, but it was easy.. still good go Harshil..
    Hope Next question is something new that i would have to really think about...
    Q12 Was a good one...
    Tuesday, April 3, 2007 3:58 PM
  • What should be the datatype of 'x' in the following code ???


    printf("[%d]\n",x); // This displays 1000
    x++;
    printf("[%d]",x); // This displays 1004





    Tuesday, April 3, 2007 3:59 PM
  • Nice one harshill, it does not click at the 1st stroke, but the ans is simple..
    Its a pointer to a Float,
    The reason is that a float takes up the mem of 4 bytes, The first time you are getting the answer as a base address which x is pointing to , after incrementing, the add increments by 4 places, so now it shows 1004..
    Also there is one more answer to it, can you tell what??
    and i m sure that you didnt ran the code, because there is a mistake in the code, if my answer is right,
    so if my answer is right, than you have 2 more hidden questions..

    Tuesday, April 3, 2007 4:20 PM
  • What i meant was that you should have used %ud, ie unsigned integer, it will show the correct value of the adress.. just try it out.. we used to sometime even get negative values for the address pointed by the pointers, but that is not possible, so our faculty said that we have to use a unsigned integer to get the correct value..

    Now the other question is that there is one more answer to you question, can you tell me what it it???
    Tuesday, April 3, 2007 5:32 PM
  • The first answer that i gave was a pointer to Float,
    The other answer would also be a pointer to a long integer..
    Wednesday, April 4, 2007 1:59 PM
  • Nice one varun, your opst really deserves a mark as an answer :0

    btw, the third answer is unsigned long  pointer Stick out tongue
    Wednesday, April 4, 2007 4:35 PM
  • Thanks to all, especially Varun and Harshil for handling thread in my absence. Smile

    But,,,,,,,,,,,,,,,, I'm back again......

    This is my next very very easy question. But think about that before giving answer.


    Code Snippet

    void main(void)
    {
        char far *farther, *farthest;
        printf("%d..%d", sizeof(farther), sizeof(farthest));
    }
    Thursday, April 5, 2007 4:13 PM
  • answer is
    4..2

    Reason : any far pointer is of 4 bytes, and a normal pointer is of 2 bytes, irrespective of the datatype Wink
    Thursday, April 5, 2007 5:28 PM
  • Ohh so far pointers are used in big programs to point to some memory location which could not have been expressed in 2 bytes as an ofset value.. Am i right about it???
    Thursday, April 5, 2007 6:51 PM
  • Yes varun, you are right.

    But you can also use far pointers to access data in the same segment of the memory. But its worthless to use 4byte pointer to access a memory location which can be accessed by a 2byte pointer Smile

    To take an analogy " Goto M.G.Road" this means that go to the m.g.road in your city. but if you want to point to another m.g.road that is in another city, you have to say " goto Bangalore : M.G.Road"
    Friday, April 6, 2007 5:16 AM
  • @ varun and harshil,

    nice efforts for the discussion on far pointer. Smile

    but let me make one thing clear. the concept of far pointer is derived from assembly language. it is used to make jumps between different segments.
     in assembly, the max length of a segment can be 64K. if you want to jump into another segment, we need to use far pointer.

    to know more about far pointers, refer to books on assembly programming. The best book to refer is a book by Peter Abel. I don't remember the name of the book, nor I do have access to it.
    Friday, April 6, 2007 5:42 PM
  • Its simple man,
    Remember the manipulators?? remember how they work?? They use pointers to functions...
    lets say that when we write
    cout<<"Test"<<endl;
    endl is the address of the function endl which we pass in the overloaded operator function of Cout... This is the most practicle example of the use of pointers to the function...
    Saturday, April 7, 2007 4:05 PM
  • Thanks varun, i didn;t think about that endl thing in c++ Stick out tongue

    any other use you know of ?
    Saturday, April 7, 2007 7:06 PM
  • I think that even the virtual table concept uses pointers to the functions, as the adress of the respective functions are stored in the virtual table..
    Trying to find more of such examples....
    Saturday, April 7, 2007 7:27 PM
  • After a long time,
    This is a really important question.. it will be really hard to answer it, because its not given in Balaguruswami and Complete Reference... So here it goes..

    Q.  What is the difference between the user defined manipulators with and without arguments??
    for eg the diff between cout<<mymanip and cout<<mymanip(5)

    Answer fast people, it has quite a few differences, and my next question follows from your answer...
    Tuesday, April 10, 2007 6:53 PM
  • Manipulators that do not have any parameters, like endl, are the simplest form of manipulator. The manipulator type manipT is a function pointer type, the manipulator Manip is the function pointer, and the associated function is the function pointed to.

    Manipulators with parameters are more complex than those without because there are additional issues to consider. Eg:  setprecision(), setw(), etc.
    Tuesday, April 10, 2007 7:14 PM
  • Buddy, even i know that.. That what i m asking what the actual difference is?? Why are the manipulators with arguments more complex... what is the reason?? Also i asked for the user defined manipulators which make the question area more smaller, so why are the manipulators with arguments more complex???

    The best is to calculate how to create both types of manipulators, you will get the answer...
    Tuesday, April 10, 2007 7:25 PM
  • What?? Was the question so tough?? I will give away the answer than...

    The formost difference is that lets say i have manipulator  mymanip with no arguments and yourmanip with arguments like yourmanip(5)

    Now mymanip is the function which does the actual formating that we want.. Where as yourmanip is a function which will return an object that will encapculate the address of the function which will do the formating and the argument in one..

    As we know that if we write cout<<yourmanip(5)
    yourmanip(5) will become a function call, which will call the funtion.. So what we do is that in yourmanip, we create an object or structure, mainly of OMANIP Class, which holds the address of lets say myformat function which does the formating and the argument that we want to pass on to myformat...
    now this object and cout will invoke the friend operator << which does the actual call to the myformat function...

    this means that cout<<yourmanip(5) will become
    cout<<[object holding address of myformat]

    this is how it works.. So how was it???
    Thursday, April 12, 2007 1:29 PM
  • What is the prototype of fread function in C ? Also explain the Prototype....
    Sunday, April 15, 2007 10:36 AM
  • #include <stdio.h>

    size_t fread(void *restrict ptr, size_t size, size_t nitems
    ,
           FILE *restrict
    stream);

     

     

    EXAMPLES

    Reading from a Stream

    The following example reads a single element from the fp stream into the array pointed to by buf.

    #include <stdio.h>
    ...
    size_t bytes_read;
    char buf[100];
    FILE *fp;
    ...
    bytes_read = fread(buf, sizeof(buf), 1, fp);
    ...
    
    Sunday, April 15, 2007 4:52 PM
  • prototype if a function is datatype of the value when it returns a value else void has to be defined,

    and prototype of FREAD function is int as by default "int" is the prototype

    Sunday, April 15, 2007 6:51 PM
  • void pointers are generic pointers and can be type casted into any type of pointer. that's why, the argument is void pointer in fread prototype.
    Sunday, April 15, 2007 8:20 PM
  • Now thatz the answer i was looking for...
    Try it out.. you can copy any address to a void pointer. ie int char or any other..
    and the other way also.. All this without typecasting explicitly...
    This is the reason, why we dont typecast the address when we pass them to fread function because the void pointer can take any adress....

    Now this leads to my other question, Why in the read function in C++ for reading files in the fstream class objects, we have to typecast the pointers?? that is to typecast the pointers to Char and then pass them to the read function???
    Sunday, April 15, 2007 8:27 PM
  • The reason what I believe is that in C++, everything is in form of stream of bytes. Most of the information in files is stored in the form of characters. So, if you want to read characters from file, you need to typecast them into characters.

    Also keep in mind that if we don't specify typecasts explicitly, the default typecasting is taken to be integer.
    Monday, April 16, 2007 5:21 AM
  • well you are quite right.. but their is no default typecasting done.. we have to typecast the adress because the prototype of the read() in fstream class has the argument as Char pointer....

    Now thee reason for this is that the file will be writing byte by byte i.e. 1 byte in the text file for each character.. and the size of the char is also 1 byte.. So the argument is as pointer to character...
    Monday, April 16, 2007 3:52 PM
  • Here comes a good one.. i had nearly blown my head off to get the answer to it...

    please give the output of the following code and please please do give the reason for it... my advice, dont try it out turbo C, as their are bugs in it and you wont get correct answer in it.. try using gnu compiler or VC++.. so here it goes...

    Code Snippet

    int i=0, j=0;

    j = i+ ++i + ++i;

    printf("%d... %d ", i, j);


    Monday, April 16, 2007 4:05 PM
  • I do agree that the thread was almost dead for quiet a few days. That was due to my personal reasons. But now, am back and will try to keep it more lively.

    Thanks to both of you and other participants for keeping thread alive in my absence. Smile
    Monday, April 16, 2007 4:42 PM
  • Well the answer you gave is correct...
    Now what actually happens is that, the system will maintain a stack for the postfix operations...
    i.e. a stack is created and all the postfix operators are pushed into it.. now as value of i is 0
    0 + 0 + 0 = 0;
    after the expression got evaluated, the stack will be emptied and the operations take place, so the value of i is incremented 2 times to make 0 as 2.....
    Thursday, April 19, 2007 4:00 AM
  • Time for some questions on files again.....

    Which file occupies less space :
    1. Text file
    2. Binary File

    Give reason to support your answer...
    Thursday, April 19, 2007 4:02 AM
  • The file space totally depends on data type and the data you are storing. suppose you opt to store integer. In binary file, it'll occupy 4 bytes, irrespective of data stored, whereas in text file, it'll occupy space only as many characters as there are.

    e.g. = Assume size of integer = 2 bytes and size of character = 1 byte. if you want to store 2 numbers 1  and 2, with a space, like "1 2".

    In binary file, space occupied = 2 + 1 + 2 = 5 bytes

    whereas in text file, space occupied = 1 + 1 + 1 = 3 bytes.


    Here, text file occupies less space.



    but if the numbers would have been 125 and 124,

    In binary file, space occupied = 2 + 1 + 2 = 5 bytes

    whereas in text file, space occupied = 3 + 1 + 3 = 7 bytes.


    Here, binary file occupies less space..




    Thus, the bottom line is, the size of file totally depends on file format selected, data type selected and the data being stored.
    Thursday, April 19, 2007 9:45 AM
  • Yes, this is the perfect answer.. no more explanation required....
    Thursday, April 19, 2007 11:13 AM
  • Thanks m8, this was a nice one Smile
    Thursday, April 19, 2007 11:59 AM
  • Actuually list is not a class. It is a template.

    In short :
    Lists are sequences of elements stored in a linked list. Compared to vectors, they allow fast insertions and deletions, but slower random access.

    Lets find some more people who can give examples, and more detailed info about the list template in C++.
    Sunday, April 22, 2007 4:38 PM
  • Hey Harshil,

    list is a template class, and hence a class. I'm also waiting for responses from others. Lets see what they know and can discover for this template class. Smile
    Sunday, April 22, 2007 4:51 PM
  • @ Varun,

    I'm not satisfies with your answer. It's a text from Turbo C++. Paste something really good and that can relate to the actual matter of how to use <list>.

    @ Harshil,

    Hey Harshil, as I told, am not satisfied with Varun's answer. I'm expecting some good answer from both of you. Please do fast.

    I'll wait till night. After that I'll start explaining <list>, obviously from some site. Wink
    Monday, April 23, 2007 1:19 PM
  • #include <list>
    using std::list;


    You declare a list of any type you want, including one of your own making (including a class or struct!). You will notice the usage shares much with the vector class: this is not a coincidence. Anyways, you declare such a list in the following way:

    list<string> listofstrings;
    list<Monkey> monkeylist;


    The type you put between the angle brackets (this is a 'templated' class) is the type of what will be stored inside our list. You can't mix lists with different types, and even lists with the same types are a little bit sketchy, you've been forwarned.

    From then on, you can do pretty much what you'd expect from a list, you can push (add)

    listofstrings.push_back("Hello");
    listofstrings.push_front(string("Huzzah"));
    monkeylist.push_back(Monkey()); //puts a default monkey
    listofstrings.pop_front(); //removes front element


    things to the back and front of the list, and can insert or delete elements anywhere. You don't have to worry about malloc'ing or freeing anything, the list class does it for you. If it sounds pretty solid, that's because it is.

    To access the data in the list, you have to use things called iterators, which are basically pointers, by another name. To iterate all the way through a list, the following would be one method:

    list<string>::iterator myiter;//declare the iterator
    for(myiter = listofstrings.begin(); myiter != listofstrings.end(); myiter++)
    {
    printf("This %s is the same as this %s\n",iter->c_str(),(*iter).c_str());
    }


    This is the equivalent to the standard for loop for an array. Note that the STL actually provides many handy functions like one called for_each which you can use to speed up iterating through things like lists and vectors. Note I also used the begin() and end() functions. Begin returns an iterator to the first element in the list, and end returns a special iterator which is past the last element in the list (not one that points to an actual element in the list).

    Other useful things to do include removing elements in the middle of the list. You do this with... you guessed it, iterators. Say you wanted to remove all occurances of the word "java" from your list:

    list<string>::iterator myiter;//declare the iterator
    myiter = listofstrings.begin();
    while(myiter != listofstrings.end())
    {
    if((*miter) == "java")
    { miter = listofstrings.erase(miter);//erase returns an iterator to the next element in the list
    }
    else
    ++miter;
    }


    Pretty slick eh? Of course, there is no random access in lists (no usage of the [] operators), and they're generally a leetle to a lot slower than vectors or deques (or a lot, depending on your usage). However, constant time insertion and deletion is often pretty nice, so don't forget about or write off lists.

    It's informative sometimes to compare list with vector. Vector is generally faster (it's basically a wrapper around an array) and has random access, however if you want to frequently insert elements at the front, or remove elements from anywhere but the end, then you might be better off using a list or a deque.

    Monday, April 23, 2007 1:29 PM
  • This is the thoughest one I ever came across...

    Explain the following Statement..
    " There can be a Virtual Distructor but not a Virtual Constructor "

    This needs a very good explanation.... Lets see if Sanket Can give it
    Monday, April 23, 2007 2:59 PM
  • The virtual destructor has the same purpose as a virtual function. At runtime looking at the type of object referred by a pointer or reference it is decided which destructor to call -- the destructor of the base class or the destructor of the derived class.

    For example, suppose B is a base class and D is a class derived from B and suppose both classes have declared their destrcutor as virtual. Suppose a pointer B *ptr is initialized as follows:

    B *ptr = new D();

    Now the ptr is of type B* but points to an object of D. So when this object is freed or goes out of scope D's destructor will be called since the destructors have been declared as virtual.



    A Pure Virtual Destructor
    Unlike ordinary member functions, a virtual destructor is not overridden when redefined in a derived class. Rather, it is extended: the lower-most destructor first invokes the destructor of its base class and only then, it is executed. Consequently, when you try to declare a pure virtual destructor, you may encounter compilation errors, or worse: a runtime crash. However, there's no need to despair--you can enjoy both worlds by declaring a pure virtual destructor without a risk. The abstract class should contain a declaration (without a definition) of a pure virtual destructor:
     
    //Interface.h file
    class Interface {
    public:
    virtual ~Interface() = 0; //pure virtual destructor declaration
    };
    Somewhere outside the class declaration, the pure virtual destructor has to be defined like this:
     
    //Interface.cpp file
    Interface::~Interface()
    {} //definition of a pure virtual destructor; should always be empty
    Monday, April 23, 2007 3:15 PM
  • Yes m8, even i think this question still got many things to discuss upon. But actually i dont know anything much Stick out tongue

    This thing requires a in depth of OOPS Smile
    Tuesday, April 24, 2007 1:04 PM
  • Class with virtual functions are called Abstract Class whose instance cannot be created...
    Now the basic reason for creating a abstract class is because they are suppose to be inherited...
    As yes you are right we can have to have a virtual distructor in it..
    The reason is lets say i have a abstract base class  B....

    There are 2 derived class from D1 and D2.... both are different and have their own destructors....
    So now when we free object of one derived class we will need to have the address of that destructor in the virtual table of the class...

    Now as we know that we cannot have its address unless we have a virtual destructor in the base class.. and as the base class is abstract there is no function of the derived class, so we have a pure virtual destructor in the base class which does nothing, its existance is only for making the destructors of other class virtual....

    I feel proud of myself
    Tuesday, April 24, 2007 4:24 PM
  • I guess ill also take a bit of credit, as i tried to clear out your question about WHY virtual destructor exists and ARE possible. Smile

    And thanks for the detailed explanation. Now others can have a good visualization about it in their brains Smile
    Wednesday, April 25, 2007 2:19 PM
  • the reason is simple. In C++, you need to reserve 1 character extra in array to store null character.
    Friday, April 27, 2007 5:49 PM
  • 5 6 0 1 4 9

    that will be the answer. I've done by both ways.

    The reason is simple. When we use post increment operator, the values are assigned first in the whole expression, but not in the printf function. This means that if we have post incremented a particular variable in expression, it does not get evaluated till the assignment operation has been performed. But in the case of printf() function, the expressions get evaluated from right to left.

    That's the reason 5 is obtained in the printf() function for the given program.
    Thursday, May 3, 2007 5:53 AM

All replies

  • Q. What is an incomplete type?

    Monday, March 19, 2007 7:33 PM
  • Incomplete Types


    An incomplete type is a type that describes an identifier but lacks information needed to determine the size of the identifier. An “incomplete type” can be:

    A structure type whose members you have not yet specified.
    A union type whose members you have not yet specified.
    An array type whose dimension you have not yet specified.
    The void type is an incomplete type that cannot be completed. To complete an incomplete type, specify the missing information. The following examples show how to create and complete the incomplete types.

    To create an incomplete structure type, declare a structure type without specifying its members. In this example, the ps pointer points to an incomplete structure type called student.
              struct student *ps;

    To complete an incomplete structure type, declare the same structure type later in the same scope with its members specified, as in
             struct student
             {
                   int num;
             }                   /* student structure now completed */

    To create an incomplete array type, declare an array type without specifying its repetition count. For example:
             char a[];  /* a has incomplete type */

    To complete an incomplete array type, declare the same name later in the same scope with its repetition count specified, as in
             char a[25]; /* a now has complete type */

     

    Monday, March 19, 2007 7:44 PM
  • Here goes the next question :

    Q 2 : Define precondition and post-condition to a member function.

    Tuesday, March 20, 2007 6:22 PM
  • Precondition: A precondition is a condition that must be true on entry
    to a member function. A class is used correctly if preconditions are
    never false. An operation is not responsible for doing anything
    sensible if its precondition fails to hold.

    For example, the interface
    invariants of stack class say nothing about pushing yet another element
    on a stack that is already full. We say that isful() is a precondition
    of the push operation.

    Post-condition: A post-condition is a condition
    that must be true on exit from a member function if the precondition
    was valid on entry to that function. A class is implemented correctly
    if post-conditions are never false.

    For example, after pushing an
    element on the stack, we know that isempty() must necessarily hold.
    This is a post-condition of the push operation.

    Wednesday, March 21, 2007 12:58 PM
  • Now one question from my side....

    What is the value of NULL in C++ ?? Can we change it?? If yes than how and if no than why??

    Wednesday, March 21, 2007 1:00 PM
  • i think there is no value for NULL.. It does not return any value
    Wednesday, March 21, 2007 1:14 PM
  • The value of NULL in C++ is integer 0.
    We can change the value of NULL.

    To change the value of NULL, we need to define its value using #define.

    e.g.

    #define NULL 1234
    #define NULL "rockstar"

    Both of the above will work. I've tested it on my compilers.
    Wednesday, March 21, 2007 5:04 PM
  • sorry aleemu, you are wrong. NULL has a value defined by default. To test, you can use the following code :

    #include<iostream.h>
    #include<conio.h>

    int main(void)
    {
            clrscr();
            cout<<NULL;       //will print 0.
            getch();

            return 0;
    }



    #include<iostream.h>
    #include<conio.h>

    #define NULL "sanket"

    int main(void)
    {
            clrscr();
            cout<<NULL;      //will print sanket.
            getch();

            return 0;
    }
    Wednesday, March 21, 2007 5:09 PM
  • Sanket Your answer is perfect... you do deserve the points but there is one more way to change the value of the NULL.... so start grinding those brains and answer how can you change the value of NULL.... you wont find it in any books but people have been doing this technique not for null but for other purpose... so tell me what is it????
    Wednesday, March 21, 2007 5:13 PM
  • I don't know whats the other way for it. I'm posting the next question.
    Thursday, March 22, 2007 5:56 PM
  • What are Proxy objects ?
    Thursday, March 22, 2007 6:01 PM
  •  Varun_Modi_a59ed9 wrote:
    Sanket Your answer is perfect... you do deserve the points but there is one more way to change the value of the NULL.... so start grinding those brains and answer how can you change the value of NULL.... you wont find it in any books but people have been doing this technique not for null but for other purpose... so tell me what is it????

    The value of NULL is defined in _NULL.h file... so you can directly go there and change the value of NULL there itself... its like what we do in devsquare quiz.. for c we change the value in header file to 1 and for c++ we change it to other value....
    Friday, March 23, 2007 2:59 AM
  • Write a C/C++ program to print something without using ; (semicolon) in the entire program.

    Easy one though, but good one to start with.
    Friday, March 23, 2007 11:34 AM
  • post has been removed

    Friday, March 23, 2007 1:41 PM
  • OK

    Friday, March 23, 2007 1:43 PM
  •  

    Friday, March 23, 2007 1:46 PM
  • It's a nice question Harshil. I'm putting my trial here for the question you've asked. Please tell me whether I'm correct or not. Then only, I'll mark it as answer.

    #include<stdio.h>

    #define test printf("Hello");

    void main(void)
    {
        test
    }
    Friday, March 23, 2007 3:58 PM
  •      

    Saturday, March 24, 2007 4:21 AM
  • How about this..

    #include<stdio.h>

    void main()
    {
    if(printf("This is magic"))
    {}
    }



    It works i have checked it.. nice one man...
    Saturday, March 24, 2007 12:36 PM
  • Sanket i had posted the answer to that NULL question.. if you find it satisfactory than please do mark it as answer...

    Here comes the next question...
    Can you swap 2 integers without using any of the following :
    1. Pointers
    2. Temporary variable
    3. Any array
    4. Any mathematical operation.......
    Saturday, March 24, 2007 12:40 PM
  •  Sanket_Shah_734609 wrote:
    It's a nice question Harshil. I'm putting my trial here for the question you've asked. Please tell me whether I'm correct or not. Then only, I'll mark it as answer.

    #include<stdio.h>

    #define test printf("Hello");

    void main(void)
    {
    test
    }



    hey man you did it a good way, but still there is a ; :P

    here is one of the possible solution
    //////////////////
    #include<stdio.h>
    void main() {
    while(!printf("rockstar"),0){}
    }
    //////////another solution

    #include<stdio.h>
    void main() {
    if(printf("rockstar")){}
    }

    there are more, guys try finding it out :)

    plz mark it as answer now :)


    Saturday, March 24, 2007 2:13 PM
  • we can still be friends, if we don't start threads that interfere each other. i'm removing my responses from your threads. You also do that.

    If you're ready with my proposal, than reply here, else, it's fine with me.
    Saturday, March 24, 2007 3:13 PM
  • Here goes the program...

    #include<stdio.h>
    #include<conio.h>

    void main(void)
    {
            int x = 5, y = 10;

            clrscr();

            y = x^y;
            x = x^y;
            y = x^y;

            printf("%d\t%d", x, y);

            getch();
    }
    Saturday, March 24, 2007 3:23 PM
  • Thanks for such a nice question harshil. Smile.

    Keep posting such questions, and please post them once the previous one has been answered. Waiting for more of this kind... Wink
    Saturday, March 24, 2007 3:33 PM
  • yes dear sanket, i think lets forget what misunderstanding we had developed and do something good in forum, i am writting this as u told me to give response, else i wouldn't have done this becoz this reply is again taking important space of ur thread, so once again lets be friends,

    but just remember, finally check and delete all the non-technical post from my my thread and urs too, i will also check for the same same and do the needful.

    bye....

    Saturday, March 24, 2007 4:23 PM
  • Hey this code do work.. now i knew the ans but didnt knew how it works.. can you explain how it works???
    Saturday, March 24, 2007 5:54 PM
  • If there are copy contructor and a overloaded '=' operator than when you pass an object as an argument(value) to a function, what will be used???
    other part of the same question..
    In same situation, when you use object = object, what will be used to copy the object???
    Sunday, March 25, 2007 4:43 AM
  • in the program, we perform Exclusive OR operations between 2 numbers. As the integer numbers are stored as bytes, we can perform easily Exclusive OR Operations.

    The XOR operation works as follows :

    1st digit            2nd digit               XOR bit
    0                     0                           0
    0                     1                           1
    1                     0                           1
    1                     1                           1

    Here is the trace for XOR when you take two numbers a & b with values 10 and 5 respectively.

    after 1st XOR Operation
    -=-=-=-=-=-=-=-=-=-=-=-
    a --> 10 --> 1010
    b -->  5 --> 0101
    -----------------
    b --> 15 --> 1111


    after 2nd XOR Operation
    -=-=-=-=-=-=-=-=-=-=-=-
    a --> 10 --> 1010
    b --> 15 --> 1111
    -----------------
    a -->  5 --> 0101


    after 3rd XOR Operation
    -=-=-=-=-=-=-=-=-=-=-=-
    a -->  5 --> 0101
    b --> 15 --> 1111
    -----------------
    b --> 10 --> 1010

    Sunday, March 25, 2007 4:47 AM
  • dear sanket i think may be  u have forgot to delete ur link from my thread

    visit

    technical quiz thread started by me or else visit:

    http://forums.microsoft.com/SamVaad/ShowPost.aspx?PostID=1371102&SiteID=43

    and delete ur link or else click on edit link and erase the message.

     

    Sunday, March 25, 2007 8:32 AM
  • when you pass as parameter, copy constructor will be used.

    else in the situation, like <<object>> = <<object>>, overloaded = is used.
    Sunday, March 25, 2007 5:42 PM
  • This is the right ans...
    just check out this code if any one has a doubt on it.. it will show when a copy constructor is called and when overloaded operator is called...

    #include<iostream.h>
    #include<conio.h>

    class vector
    {
        int tmp;
        public:
        vector(){}
        vector(int a = 0)
        {
            tmp = a;
        }
        vector( vector &v1)
        {
            cout<<"\nThis is copy contructor";
            tmp = v1.tmp;
        }

        vector & operator =(vector &v1)
        {
            cout<<"\nThis is the overloaded operator";
            tmp = v1.tmp;
            return *this;
        }
        friend void operator <<(ostream &cout, vector &v);
        friend void copy(vector &v1, vector v2);
    };
    void copy(vector &v1, vector v2)
    {
        v1.tmp = v2.tmp;
    }
    void operator <<(ostream &cout, vector &v)
    {
        cout<<v.tmp;
    }
    void main()
    {
        vector v1(4), v2(8), v3(12);
        clrscr();
        cout<<v1;
        v1 = v2;
        cout<<v1;
        copy(v2, v3);
        cout<<v2;
        getch();
    }
    Monday, March 26, 2007 3:53 AM
  • Is it possible to have a parameterized constructor and no default constructor??
    If no then why and if yes than how??
    Monday, March 26, 2007 3:56 AM
  • its upto us whether we want paprameterised constructor or not and also the default constructor

    i am giving example of both:

    class a

    {

    private x;

            a()       /*default constructor*/

        {

            x=0;

        }

                  a(int alpha)    /* parameterised constructor*/

                 {

                     x=alpha;

                 }

    }    /* end of class*/

    void main()

    {

    a obj1;  /* default constructor is run and x is initialised to 0.*/

    a obj2(76); /* parameterised constructor initialises the value of x to 76*/

     

    Monday, March 26, 2007 9:40 AM
  • Sorry kanav but you didnt understand the question... what i m asking is that if you have a parametrized constructor, than is it possible not to have a default constructor???
    Monday, March 26, 2007 12:01 PM
  • It is possible to have no default constructor when parameterized constructor is simple. The answer is simple. Put default values in member functions when defining them. This way, we need not to pass any values as arguments. The default values will be taken automatically.
    Monday, March 26, 2007 12:48 PM
  • This is the right ans.. actually the ans was just above my question.. check out the code i gave for question 6, their put the default constructor in comments and run the prog.. it will run perfectly without any warning or error, but after that try to remove the default values from the parameterized constructor.. and observe what outcome comes....
    Monday, March 26, 2007 3:23 PM
  • Write a code that prints the output as the code itself....(the whole code and nothing else)
    Monday, March 26, 2007 3:32 PM
  • Here goes the code

    char a[] = "int main(){ printf(b,34,a,34,10,34,b,34,10,10,a,10); }";
    char b[] = "char a[] = %c%s%c;%cchar b[] = %c%s%c;%c%c%s%c";

    int main(){ printf(b,34,a,34,10,34,b,34,10,10,a,10); }


    Tuesday, March 27, 2007 11:14 AM
  • Here goes the next question :

    Q 9. What is a dangling pointer?
    Tuesday, March 27, 2007 11:20 AM
  • This is the right ans, good one...

    also a simple way is to just open the code file in the prog and display it.. but this one is way too good...

    Tuesday, March 27, 2007 11:27 AM
  • Dangling pointers and wild pointers in computer programming are pointers that do not point to a valid object of the appropriate type, or to a distinguished null pointer value in languages which support this. Dangling pointers arise when an object is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory. As the system may reallocate the previously freed memory to another process, if the original program then dereferences the (now) dangling pointer, unpredictable behavior may result, as the memory may now contain completely different data. This is especially the case if the program writes data to memory pointed by a dangling pointer, as silent corruption of unrelated data may result, leading to subtle bugs that can be extremely difficult to find, or cause segmentation faults (*NIX) or general protection faults (Windows). If the overwritten data is bookkeeping data used by the system's memory allocator, the corruption can cause system instabilities. Wild pointers arise when a pointer is used prior to initialization to some known state, which is possible in some programming languages. They show the same eratic behaviour as dangling pointers, though they are less likely to stay undetected.
    Tuesday, March 27, 2007 11:29 AM
  • nice thread
    Tuesday, March 27, 2007 12:19 PM
  • Give the output of the following program and explain it:

    class base

    {

        public:

        int bval;

        base()

        {

            bval=0;

        }

    };

    class deri:public base

    {

        public:

        int dval;

        deri()

        {

            dval=1;

        }

    };

    void SomeFunc(base *arr, int size)

    {

        for(int i=0; i<size; i++,arr++)

           cout<<arr->bval;

        cout<<endl;

    }

    int main()

    {

        base BaseArr[5];

        SomeFunc(BaseArr,5);

        deri DeriArr[5];

        SomeFunc(DeriArr,5);

    }



    Tuesday, March 27, 2007 5:30 PM
  • The output is
    00000
    01010

    This is really a nice one.. now i don know how well i can explain this but here it goes...
    Now as we know that we can point to the derived class using a base class pointer, so that is not a prob, thatz y we use a base * in the function that we use to print the value...

    Now the for base class output ie 00000 is simple which everyone knows about... the thing to notice is why we are getting 01010 in derived class??

    1st of all the memory is allocated consequently ie for lets say deriarr[2] its like this
    mem for ->bval
    mem for ->dval
    mem for ->bval
    mem for ->dval

    ie the derived class has memory of 4 bytes, 2 for bval, 2 for dval
    Now we can point to the derived class using base class pointer but when we derefrence the base pointer, it will dereff only 2 byte... so increment operator will move the add in function only by 2 bytes and not by 4 bytes...

    so now 1st we get the bval of derived class, after increment the pointer points to the dval of the same class instead of jumping to the next object...

    so we get the output as 01010

    try out this, in the function use the increment iperator 10 times,
    the output will be
    0101010101
    Tuesday, March 27, 2007 7:22 PM
  • Thanks for the overwhelming response from the participants of this thread !!!

    Here goes the next question. Please participate as much possible.

    What's the difference between deep copy and shallow copy ?

    Wednesday, March 28, 2007 6:20 PM
  • Whenever we copy a object to another, and if there is a dynamic memory allocation in the object, than a care is to be taken on how we copy the data...
    There are 2 ways to copy the data

    1. Shallow Copy.
      Consider two objects, A and B, which each refer to two memory blocks xi and yi.You can think of A and B as strings and of xi and yi as the characters they contain.

    In Shallow copy B is attached to the same memory block as A, i.e B is made to point at the same block as A. This results in a situation in which some data is shared between A and B, thus modifying the one will alter the other. The original memory block of B is now no longer referred to from anywhere.This results in a situation in which some data is shared between A and B, thus modifying the one will alter the other. The original memory block of B is now no longer referred to from anywhere.

    2. Deep Copy.
        An alternative are deep copies. Here the data is actually copied over.
    The result is different from the result a shallow copy gives. The advantage is that A and B do not depend on each other but at the cost of a slower more expensive copy.

    Wednesday, March 28, 2007 6:28 PM
  • Looks like the threads going dead.. buck up people.. here is a nice question to keep the heat up...

    Q. What is the difference between a Inline function and a Macro??
    The best answer gets marked as Answer...
    Friday, March 30, 2007 4:06 PM
  • In inline function the code of the inline function is replaced at the place where it was called by the COMPILER during compilation & linkage , while in case of MACRO the block in the macro is replaced even before compile begins, the text in macro is replaced by the PREPROCESSOR before the compilation process starts.

    please mark as answer, i have answered this question myself,even without having any reference, so, i guess i deserve the credit Smile
    Friday, March 30, 2007 5:10 PM
  • yes Harshil your ans is right, but there is also another major diff.. waiting for that one also.. can anyone tell me another major diff??
    Friday, March 30, 2007 6:38 PM
  • Unlike #define macros, inline functions avoid infamous macro errors since inline functions always evaluate every argument exactly once. In other words, invoking an inline function is semantically just like invoking a regular function, only faster:

    Also unlike macros, argument types are checked, and necessary conversions are performed correctly.



    Saturday, March 31, 2007 6:17 AM
  • So this is the final answer with major differences between the two..
    Harshil gave most of them so he does deserved to get marked..
    Ill list down the differences..
    1.) Their replacement in the code is different, macros are replaced before by preprocessor compile time, where as inline functions are replaced at linkage and compiling time by the compiler..
    2.) Inline functions are checked for any errors where as macros are not...

    3.) Now the diff that most of us forget..
         The passing of arguments, lets say when we pass 2*3 in macro, than 2*3 directly gets replaced, but in inline function 2*3 is evaluated to 6 and than 6 gets replaced in inline function...
    Saturday, March 31, 2007 10:06 AM
  • ahh thanks for the 3rd difference Smile i forgot about it Smile Human tends to forget the most easy things Stick out tongue hehe
    Saturday, March 31, 2007 12:31 PM
  • Thanks to all for your participation...
    Here goes the next question...


    What will happen if I say delete this?
    Sunday, April 1, 2007 5:06 PM
  • Man this is a very dangerous thing that you are doing... It wont create any logical or compilation error, but a runtime error.. As you are asking to free the memory that you are currently standing on, the program will terminate itself.. ie the program will crash down...
    Sunday, April 1, 2007 5:17 PM
  • The program will not terminate in any case. Here goes the answer :

    destructor executed, but memory will not be freed (other than work done by destructor). If we have class Test and method Destroy { delete this } the destructor for Test will execute, if we have Test *var = new Test()
    1. pointer var will still be valid
    2. object created by new exists until explicitly destroyed by delete
    3. space it occupied can be reused by new
    4. delete may only be applied to a pointer by new or zero, applying delete to zero = no FX
    5. delete = delete objects
    6. delete[] – delete array
    7. delete operator destroys the object created with new by deallocating the memory assoc. with the object
    8. if a destructor has been defined fir a class delete invokes that desructor

    Sunday, April 1, 2007 5:31 PM
  • Man most of the things you said went above me,, n i tried delete this in Turbo C++, and the prog crashed..
    Sunday, April 1, 2007 5:44 PM
  • I think question 11 is a bit high level, will take some time for us to digest . May be question 11 should be asked as question 50  Stick out tongue
    Sunday, April 1, 2007 5:57 PM
  • Rightly said brother, rightly said...
    Sunday, April 1, 2007 6:00 PM
  • Thanks varun for supporting me.
    Sunday, April 1, 2007 6:07 PM
  • Turbo C++ is full of bugs. I don't have lot faith on it. The response is based on the experiments conducted by professionals in the virtual labs.
    Sunday, April 1, 2007 6:08 PM
  • try gcc
    Sunday, April 1, 2007 6:14 PM
  • @ Varun, Harshil,

    I'm sorry that I asked really a tough question. My next question will be a piece of cake for you. Smile
    Sunday, April 1, 2007 6:15 PM
  • Its good that you post such questions, it will increase the knowledge base..

    Sunday, April 1, 2007 6:20 PM
  • Yes its good to post such questions, i agree. But at an increase in level  of toughness Smile let the toughness level increase gradually Smile We got lot and lot of time to learn in this forum Smile
    Monday, April 2, 2007 4:35 AM
  • As promised, I'm putting down an easy question. But please give the reason also alongwith the answer...
    What will be the contents of one.txt file ?

    void main(void)
    {
        FILE *fp1,*fp2;
        fp1=fopen("one.txt","w");
        fp2=fopen("one.txt","w");
        fputc('A',fp1);
        fputc('B',fp2);
        fclose(fp1);
        fclose(fp2);
    }

    Monday, April 2, 2007 6:13 PM
  •  Sanket_Shah_734609 wrote:
    As promised, I'm putting down an easy question. But please give the reason also alongwith the answer...
    What will be the contents of one.txt file ?

    void main(void)
    {
    FILE *fp1,*fp2;
    fp1=fopen("one.txt","w");
    fp2=fopen("one.txt","w");
    fputc('A',fp1);
    fputc('B',fp2);
    fclose(fp1);
    fclose(fp2);
    }



    Nice one buddy.. though you say that its easy but its not so simple..
    Ok now when you open a file in fp1 and fp2, 2 buffers are created for both and its adress is passed in fp1 and fp2, so you have 2 different buffers now.. Your proceedings will be as per you code,
    The trick lies in the fclose, ie. now when you first close fp1, file with content as A gets saved, but when you close file fp2, file with B gets overwritten, so

    Finally you get the file with the content as only B, in it...

    Change the order of the fclose command, the output will change
    Monday, April 2, 2007 6:53 PM
  • That exactly what i thought varun, but i was lazy to try out the code and be 100% sure Smile but anyways, good one varun.
    Tuesday, April 3, 2007 12:57 PM

  • What will be the output of the following code ? Give reason.

    void main()
    {
        int i=-1,j=-1,k=0,l=2,m;
        m=i++&&j++&&k++||l++;
        printf("%d %d %d %d %d",i,j,k,l,m);
    }

    Tuesday, April 3, 2007 1:54 PM
  • Final answer would be
    i=0
    j=0
    k=1
    l=3
    m=1

    This one was quite easy  Smile i did it without any compiler Smile
    Tuesday, April 3, 2007 2:23 PM
  • Next question please..... Can i post the next question ?
    Tuesday, April 3, 2007 2:24 PM
  • Hey Harshil, your answer is right and you can post next question here. I'm not going to put any for this time. But please give the logic of deriving value of 'm' so that others can understand it. Smile
    Tuesday, April 3, 2007 3:37 PM
  • Here is the logic of how to get the value of 'm'

    i++ , j++, k++, l++<-- this is post-increment, so the value returned from these expr will be -1, -1, 0, and 2 respectively


    so lets dissect this equation
    m = (i++)   &&   (j++)   &&   (k++)   ||   (l++)   ;
    m = TRUE && TRUE && FALSE || TRUE

    m = FALSE || TRUE
    M = TRUE

    hence the value of m will be TRUE(non-zero)  which by default is 1



    Tuesday, April 3, 2007 3:53 PM
  • Waiting for the next question... I missed the last one, but it was easy.. still good go Harshil..
    Hope Next question is something new that i would have to really think about...
    Q12 Was a good one...
    Tuesday, April 3, 2007 3:58 PM
  • What should be the datatype of 'x' in the following code ???


    printf("[%d]\n",x); // This displays 1000
    x++;
    printf("[%d]",x); // This displays 1004





    Tuesday, April 3, 2007 3:59 PM
  • Nice one harshill, it does not click at the 1st stroke, but the ans is simple..
    Its a pointer to a Float,
    The reason is that a float takes up the mem of 4 bytes, The first time you are getting the answer as a base address which x is pointing to , after incrementing, the add increments by 4 places, so now it shows 1004..
    Also there is one more answer to it, can you tell what??
    and i m sure that you didnt ran the code, because there is a mistake in the code, if my answer is right,
    so if my answer is right, than you have 2 more hidden questions..

    Tuesday, April 3, 2007 4:20 PM
  • you are absolutely right Varun, His post requires to be marked as an answer Smile

    I have tested that code, and it worked perfectly in TC. No errors. I didn't check about whether there was any warnings or not, but it worked perfectly.

    I think you are thinking that the type conversion will be required ? to display the value of the hex pointer to float into interger , right ? if not then please put some light onto it Smile
    Tuesday, April 3, 2007 4:37 PM
  • What i meant was that you should have used %ud, ie unsigned integer, it will show the correct value of the adress.. just try it out.. we used to sometime even get negative values for the address pointed by the pointers, but that is not possible, so our faculty said that we have to use a unsigned integer to get the correct value..

    Now the other question is that there is one more answer to you question, can you tell me what it it???
    Tuesday, April 3, 2007 5:32 PM
  • The first answer that i gave was a pointer to Float,
    The other answer would also be a pointer to a long integer..
    Wednesday, April 4, 2007 1:59 PM
  • Nice one varun, your opst really deserves a mark as an answer :0

    btw, the third answer is unsigned long  pointer Stick out tongue
    Wednesday, April 4, 2007 4:35 PM
  • Thanks to all, especially Varun and Harshil for handling thread in my absence. Smile

    But,,,,,,,,,,,,,,,, I'm back again......

    This is my next very very easy question. But think about that before giving answer.


    Code Snippet

    void main(void)
    {
        char far *farther, *farthest;
        printf("%d..%d", sizeof(farther), sizeof(farthest));
    }
    Thursday, April 5, 2007 4:13 PM
  • answer is
    4..2

    Reason : any far pointer is of 4 bytes, and a normal pointer is of 2 bytes, irrespective of the datatype Wink
    Thursday, April 5, 2007 5:28 PM
  • I was able to find that, but what is a far pointer?? there is also some far function, but i was not able to understand its meaning..
    Also far works in Turbo C, but not in vc++
    Thursday, April 5, 2007 5:43 PM
  • the 16bit intels was using the segmented memory segments, so if the data was lying in the other segment other than the current segment in use, then it should be accessed through the segmentnameSurpriseffset where offset is the distance from the starting of the segment to the place where the data lies.

    Same is applicable for functions. Hope you understand it now.

    But as Windows uses flat memory addressing scheme, there was no need for far and near pointers, so its excluded in vc++ for win32 environment. It is still there for the 16bit environment mostprobably.
    Thursday, April 5, 2007 5:56 PM
  • Ohh so far pointers are used in big programs to point to some memory location which could not have been expressed in 2 bytes as an ofset value.. Am i right about it???
    Thursday, April 5, 2007 6:51 PM
  • Yes varun, you are right.

    But you can also use far pointers to access data in the same segment of the memory. But its worthless to use 4byte pointer to access a memory location which can be accessed by a 2byte pointer Smile

    To take an analogy " Goto M.G.Road" this means that go to the m.g.road in your city. but if you want to point to another m.g.road that is in another city, you have to say " goto Bangalore : M.G.Road"
    Friday, April 6, 2007 5:16 AM
  • Can you give a code which displays the actual use of far pointer, I have never seen any such use before, also we were not taught about it in the class...
    Friday, April 6, 2007 3:02 PM
  • Sorry Varun, i am afraid to say that i dont have any practical example about it. And what i think is it was used in early programming days, and now its not used anymore. May be sanket can put some more light onto it.
    Friday, April 6, 2007 3:56 PM
  • @ varun and harshil,

    nice efforts for the discussion on far pointer. Smile

    but let me make one thing clear. the concept of far pointer is derived from assembly language. it is used to make jumps between different segments.
     in assembly, the max length of a segment can be 64K. if you want to jump into another segment, we need to use far pointer.

    to know more about far pointers, refer to books on assembly programming. The best book to refer is a book by Peter Abel. I don't remember the name of the book, nor I do have access to it.
    Friday, April 6, 2007 5:42 PM
  • Give me a practicle application of "Pointers to Function"
    Friday, April 6, 2007 5:49 PM
  • I dont have any practical example about it in C++, but in VC++ "Pointers to function" are used to point to the functions that are in the DLL files and then call that functions using pointers to function Stick out tongue
    Friday, April 6, 2007 6:28 PM
  • Its simple man,
    Remember the manipulators?? remember how they work?? They use pointers to functions...
    lets say that when we write
    cout<<"Test"<<endl;
    endl is the address of the function endl which we pass in the overloaded operator function of Cout... This is the most practicle example of the use of pointers to the function...
    Saturday, April 7, 2007 4:05 PM
  • Thanks varun, i didn;t think about that endl thing in c++ Stick out tongue

    any other use you know of ?
    Saturday, April 7, 2007 7:06 PM
  • I think that even the virtual table concept uses pointers to the functions, as the adress of the respective functions are stored in the virtual table..
    Trying to find more of such examples....
    Saturday, April 7, 2007 7:27 PM
  • thanks varun for all the hard work that you are doing for us.
    Sunday, April 8, 2007 1:33 PM
  • After a long time,
    This is a really important question.. it will be really hard to answer it, because its not given in Balaguruswami and Complete Reference... So here it goes..

    Q.  What is the difference between the user defined manipulators with and without arguments??
    for eg the diff between cout<<mymanip and cout<<mymanip(5)

    Answer fast people, it has quite a few differences, and my next question follows from your answer...
    Tuesday, April 10, 2007 6:53 PM
  • Manipulators that do not have any parameters, like endl, are the simplest form of manipulator. The manipulator type manipT is a function pointer type, the manipulator Manip is the function pointer, and the associated function is the function pointed to.

    Manipulators with parameters are more complex than those without because there are additional issues to consider. Eg:  setprecision(), setw(), etc.
    Tuesday, April 10, 2007 7:14 PM
  • Buddy, even i know that.. That what i m asking what the actual difference is?? Why are the manipulators with arguments more complex... what is the reason?? Also i asked for the user defined manipulators which make the question area more smaller, so why are the manipulators with arguments more complex???

    The best is to calculate how to create both types of manipulators, you will get the answer...
    Tuesday, April 10, 2007 7:25 PM
  • What?? Was the question so tough?? I will give away the answer than...

    The formost difference is that lets say i have manipulator  mymanip with no arguments and yourmanip with arguments like yourmanip(5)

    Now mymanip is the function which does the actual formating that we want.. Where as yourmanip is a function which will return an object that will encapculate the address of the function which will do the formating and the argument in one..

    As we know that if we write cout<<yourmanip(5)
    yourmanip(5) will become a function call, which will call the funtion.. So what we do is that in yourmanip, we create an object or structure, mainly of OMANIP Class, which holds the address of lets say myformat function which does the formating and the argument that we want to pass on to myformat...
    now this object and cout will invoke the friend operator << which does the actual call to the myformat function...

    this means that cout<<yourmanip(5) will become
    cout<<[object holding address of myformat]

    this is how it works.. So how was it???
    Thursday, April 12, 2007 1:29 PM
  • What is the prototype of fread function in C ? Also explain the Prototype....
    Sunday, April 15, 2007 10:36 AM
  • #include <stdio.h>

    size_t fread(void *restrict ptr, size_t size, size_t nitems
    ,
           FILE *restrict
    stream);

     

     

    EXAMPLES

    Reading from a Stream

    The following example reads a single element from the fp stream into the array pointed to by buf.

    #include <stdio.h>
    ...
    size_t bytes_read;
    char buf[100];
    FILE *fp;
    ...
    bytes_read = fread(buf, sizeof(buf), 1, fp);
    ...
    
    Sunday, April 15, 2007 4:52 PM
  • prototype if a function is datatype of the value when it returns a value else void has to be defined,

    and prototype of FREAD function is int as by default "int" is the prototype

    Sunday, April 15, 2007 6:51 PM
  • Let me make the question more specific..
    Why is the argument Void Pointer in the fread prototype... Thatz what the real question is...
    Answer fast.. I just remembered one more question.. when you answer this, i will post the next question...
    Sunday, April 15, 2007 6:57 PM
  • void pointer is a pointer that points to an object of unknown type and are commonly used to allocate memory to blocks of data.
    Sunday, April 15, 2007 7:06 PM
  • void pointers are generic pointers and can be type casted into any type of pointer. that's why, the argument is void pointer in fread prototype.
    Sunday, April 15, 2007 8:20 PM
  • Now thatz the answer i was looking for...
    Try it out.. you can copy any address to a void pointer. ie int char or any other..
    and the other way also.. All this without typecasting explicitly...
    This is the reason, why we dont typecast the address when we pass them to fread function because the void pointer can take any adress....

    Now this leads to my other question, Why in the read function in C++ for reading files in the fstream class objects, we have to typecast the pointers?? that is to typecast the pointers to Char and then pass them to the read function???
    Sunday, April 15, 2007 8:27 PM
  • The reason what I believe is that in C++, everything is in form of stream of bytes. Most of the information in files is stored in the form of characters. So, if you want to read characters from file, you need to typecast them into characters.

    Also keep in mind that if we don't specify typecasts explicitly, the default typecasting is taken to be integer.
    Monday, April 16, 2007 5:21 AM
  • Thanks for the explanation guys, you really rock, i have got to learn a lot after this thread has been started.
    I have one complain, this thread is starting to get slower than before Wink let it stay heated, guys contribute Smile
    Monday, April 16, 2007 3:00 PM
  • well you are quite right.. but their is no default typecasting done.. we have to typecast the adress because the prototype of the read() in fstream class has the argument as Char pointer....

    Now thee reason for this is that the file will be writing byte by byte i.e. 1 byte in the text file for each character.. and the size of the char is also 1 byte.. So the argument is as pointer to character...
    Monday, April 16, 2007 3:52 PM
  • Here comes a good one.. i had nearly blown my head off to get the answer to it...

    please give the output of the following code and please please do give the reason for it... my advice, dont try it out turbo C, as their are bugs in it and you wont get correct answer in it.. try using gnu compiler or VC++.. so here it goes...

    Code Snippet

    int i=0, j=0;

    j = i+ ++i + ++i;

    printf("%d... %d ", i, j);


    Monday, April 16, 2007 4:05 PM
  • I do agree that the thread was almost dead for quiet a few days. That was due to my personal reasons. But now, am back and will try to keep it more lively.

    Thanks to both of you and other participants for keeping thread alive in my absence. Smile
    Monday, April 16, 2007 4:42 PM
  • I cant fugure out the reason bro. But in TC it shows 2..6 while in vc++ it shows 2..4 Sad

    strange results. If you can please explain the reason why , it would be really nice m8.
    Monday, April 16, 2007 6:42 PM
  • i dont know why it  is happening it is really out of the way solution on TC  but what i could think was

     

     

    i= 2

    & j=3

    as

    for i+ the value of i will be used

    ++i will pre-increment the value of i to 1 and uses it in place of ++i

    again the same ++i now makes i=2 and uses the value in equation.

     

    hence j= 0 + 1+ 2;

    j=3;

    & i=2

     

    Monday, April 16, 2007 8:03 PM
  • for TC,

    unary operator has higher precedence than binary addition operator. So, value of i will become 2 before addition. value of j will become 2+2+2 = 6.

    For vc++,

    it pushes value of i (0) while no operation is to be performed on first operand of addition. And after that it performs unary operations on i twice making its value 2. so, 0 + 2 + 2 = 4. It is the only explanation because I tried j = i + ++i + ++i, j = ++i + ++i + i, j = ++i + ++i. these three gives the same answer.
    Tuesday, April 17, 2007 1:31 AM
  • Well I have still not recieved the right answer yet... I think i will wait for the evening to give the answer.. I think Sanket can give the answer
    And please i had asked you not to use Turbo C, i knew it will give wrong answer... their are lots of bugs in Turbo C... please dont rely on it...
    On all the other standard compilers you will get ans as 2..4
    Tuesday, April 17, 2007 3:15 AM
  • Thanks Varun, i will eagerly wait for the answer today evening Smile
    Tuesday, April 17, 2007 4:19 AM
  • i=2

    j=5

    Tuesday, April 17, 2007 11:06 AM
  • people finally the answer to the most avaited Question of the year....
    the expression is
    i=0; j = i + ++i + ++i ;
    now most of the time we misstook it that as the ++ operator has high precedence rule,
     i will get incremented 2 times before....
    but that does not happen... try to think in the actual core, what will happen at the machine level...
    j = i + ++i + ++i
         -------
    1st  i + i++ gets evaluated
     as the associativity for + operator is left to right
    now here, ++ has higher precedence, so the value of i  become from 0 to 1,
    the expression will become
    1 + 1 = 2
    now the expression
    j = i + ++i + ++i becomes
          -------
             2      + ++i  with  i = 1
    again in 2 + ++i , increment operator has higher precedence, so i will become from 1 to 2
    so the expression is
    2 + 2 = 4
    now this 4 gets assigned to j,
    as a result, the magic,,, j = 4 and i = 2...
    What do you say guys.. it took 3 days and 5 people to trace this out.. but finally our Mam did it... so thought of sharing it with you guys here
    Tuesday, April 17, 2007 3:33 PM
  • If you still have confusions.. try tracing out following..
    for  i =1
    j = i + ++i + ++i ;
    for diff values of i....
    for i= 1, j will be 7, for i = 2, j will be 10....

    also try out this for i = 0 or 1  or 2

    j = i + ++i + ++i + ++i;

    Tuesday, April 17, 2007 3:36 PM

  • Give the output of the following code with explanation..

    i=0;

    j = i + i++ + i++;

    printf("%d...%d", i, j);



    Waiting for the answer eagerly...

    Tuesday, April 17, 2007 3:41 PM
  • Thanks @Varun, i thought about the left associative rule thingy, but instead of doing (2+2) i was doing 1+2 for i + ++i Stick out tongue

    Good one brov, this was really a good brainteaser.

    Now ill try to solve the other question Smile
    Tuesday, April 17, 2007 4:41 PM
  • Varun, the question 22 is very easy. may be everyone can solve it ):

    answer is
    2..0

    Reason :
    As it is post increment, the value of i wont change until the statement is evaluated.
    Thus
    i + i++ + i++ will be 0 + 0+0 = 0
    hence the value of j=0 and as there are 2 increments to i, value of i will be i=2 Smile
    Tuesday, April 17, 2007 4:48 PM
  • Yes brother your answer is right.. and you shall get marked..

    But can you tell me how does this work at machine level??? How the expression gets evaluated..

    Tuesday, April 17, 2007 6:05 PM
  • Not much idea about the assembly level thing. May be ill leave that for someone else Wink
    Tuesday, April 17, 2007 7:13 PM
  • Well the answer you gave is correct...
    Now what actually happens is that, the system will maintain a stack for the postfix operations...
    i.e. a stack is created and all the postfix operators are pushed into it.. now as value of i is 0
    0 + 0 + 0 = 0;
    after the expression got evaluated, the stack will be emptied and the operations take place, so the value of i is incremented 2 times to make 0 as 2.....
    Thursday, April 19, 2007 4:00 AM
  • Time for some questions on files again.....

    Which file occupies less space :
    1. Text file
    2. Binary File

    Give reason to support your answer...
    Thursday, April 19, 2007 4:02 AM
  • The questions are getting harder now. I like it Smile but i dnot know much about the answer to this :
    Thursday, April 19, 2007 4:54 AM