Answer required!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • karthikeyanck
    New Member
    • Oct 2007
    • 23

    Answer required!

    What will be the answer for this?


    Code:
    #include<stdio.h>
    int strA = 1;
    int *swappy(int *ptr);
    
    void main(){
    swappy(&strA);
    }
    
    int *swappy(int *ptr){
    printf("\nThe value is: %d",*ptr);
    *ptr++;
    printf("\nThe value after post increment: %d",*ptr);
    ++*ptr;
    printf("\nThe value after pre increment: %d",*ptr);
    }
    Please suggest the solution and explain why?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Originally posted by karthikeyanck
    What will be the answer for this?

    Please suggest the solution and explain why?
    The answer for what? The solution for what?

    You have not described a problem or asked an answerable question but I will tell you for free that your program exhibits undefined behaviour.

    Comment

    • karthikeyanck
      New Member
      • Oct 2007
      • 23

      #3
      Solved!

      Sorry, I was trying to get the answer without initializing the pointer, solved now! BTW the question was what will printf print?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by karthikeyanck
        Sorry, I was trying to get the answer without initializing the pointer, solved now! BTW the question was what will printf print?
        Your pointer was initialized already in your original version. The execution of that thing still causes undefined behaviour; nothing is solved yet.

        kind regards,

        Jos

        Comment

        • karthikeyanck
          New Member
          • Oct 2007
          • 23

          #5
          I meant the pointer wasn't initialzed to NULL, so i believe it should have been holding someother value if am not wrong..

          Regards,

          Karthik

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by karthikeyanck
            I meant the pointer wasn't initialzed to NULL, so i believe it should have been holding someother value if am not wrong.
            That pointer is a parameter and it's initialized in your main function (line #6). I don't understand your remark/question.

            kind regards,

            Jos

            Comment

            • newb16
              Contributor
              • Jul 2008
              • 687

              #7
              [QUOTE=karthikey anck;3437214]What will be the answer for this?
              [quote]
              Change it to
              Code:
              int strA[3]={0,10,20};
              ...
              void main(){
              swappy(&(strA[0]));
              compile and run.
              In first *q++ it increments pointer beyond your original variable,
              result of dereferencing is ignored, and the value of location where pointer points is printed.

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                ps. better make that 'int main()'.

                kind regards,

                Jos

                Comment

                Working...