Quick question about pointers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MonaLisaO
    New Member
    • Mar 2008
    • 13

    Quick question about pointers

    Hi I have a question about pointers:

    Code:
    int a = 10;
    BOOL rc =  myMethod (struct_A, &a);
    My method is like this:

    Code:
    BOOL myMethod (STRUCT aStruct, int* numElements)
    {
      int temp = *numElements;
      *numElements = 0;
       ...
       return true;
    }
    Does temp == 10 or 0?
  • Parul Bagadia
    New Member
    • Mar 2008
    • 188

    #2
    Originally posted by MonaLisaO
    Hi I have a question about pointers:

    Code:
    int a = 10;
    BOOL rc =  myMethod (struct_A, &a);
    My method is like this:

    Code:
    BOOL myMethod (STRUCT aStruct, int* numElements)
    {
      int temp = *numElements;
      *numElements = 0;
       ...
       return true;
    }
    Does temp == 10 or 0?
    according to me, when u wrote following stement;
    int temp = *numElements;
    temp became equal to 10;
    temp==10.
    N after you wrote this statement;
    *numElements = 0;
    temp==0;
    so recently its 0.

    Comment

    • MonaLisaO
      New Member
      • Mar 2008
      • 13

      #3
      Originally posted by Parul Bagadia
      according to me, when u wrote following stement;
      int temp = *numElements;
      temp became equal to 10;
      temp==10.
      N after you wrote this statement;
      *numElements = 0;
      temp==0;
      so recently its 0.
      So then how do I give temp the value of numElements then change the value of numElements to 0?

      Comment

      • AmaraEmerson
        New Member
        • Nov 2007
        • 5

        #4
        temp is still 10, when you did *numElements = 0; only the variable "a" changed AFAIK.

        Comment

        • Parul Bagadia
          New Member
          • Mar 2008
          • 188

          #5
          Originally posted by AmaraEmerson
          temp is still 10, when you did *numElements = 0; only the variable "a" changed AFAIK.
          Yaeh.
          i was wrong. temp is still 10. I agree to that.

          Comment

          • MonaLisaO
            New Member
            • Mar 2008
            • 13

            #6
            Originally posted by AmaraEmerson
            temp is still 10, when you did *numElements = 0; only the variable "a" changed AFAIK.
            Ok, that is what I thought would happen.

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Just a quick confirmation that between you you have got to the correct result

              after the code has run (or just before it finishes running)

              temp == 10

              *numElements == 0

              since numElements == &a
              a also == 0

              Comment

              Working...