2 Quick Pointer Questions:

This is a discussion on 2 Quick Pointer Questions: within the C++ Programming forums, part of the General Programming Boards category; I have two questions about pointers: Code: #include <iostream.h> void somefunc(int * fptr) { *fptr += 250; } void somefunc2(int ...

  1. #1
    People Love Me
    Join Date
    Jan 2003
    Posts
    412

    2 Quick Pointer Questions:

    I have two questions about pointers:
    Code:
    #include <iostream.h>
    void somefunc(int * fptr)
    {
      *fptr += 250;
    }
    
    void somefunc2(int * fptr2)
    {
      *fptr2 = 450;
    }
    
    void somefunc3(int * fptr3)
    {
      *fptr3 ++;// This doesn't work.
    }
    
    main()
    {
      int x = 3;
      int * Xaddr = &x;
    
    	cout << x << endl;
    
      somefunc(Xaddr);
    	cout << x << endl;
    
      somefunc2(Xaddr);
    	cout << x << endl;
    
      somefunc3(Xaddr);
    	cout << x << endl;
      return 0;
    }
    Yet, strangely, the same line here does:
    Code:
    #include <iostream.h>
    void somefunc(int * fptr)
    {
      *fptr += 250;
    }
    
    void somefunc2(int * fptr2)
    {
      *fptr2 = 450;
    }
    
    void somefunc3(int * fptr3)
    {
      *fptr3 += 1; // This does work.
    }
    
    main()
    {
      int x = 3;
      int * Xaddr = &x;
    
    	cout << x << endl;
    
      somefunc(Xaddr);
    	cout << x << endl;
    
      somefunc2(Xaddr);
    	cout << x << endl;
    
      somefunc3(Xaddr);
    	cout << x << endl;
      return 0;
    }
    My two questions are thus:

    1.) In cases like this, does the ++ operator not work?
    2.) Is this one of the primary uses of pointers? (Through functions)

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Could be a problem with the priority of operators (increasing the pointer instead of the data). Try this and see if it works better:

    (*fptr3)++;

    Pointers can be used for many things. Using them to increase single integers in the way you do it isn't so common though.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    *fptr3++ does not deference a pointer and then increment it. It dereferences the pointer and then increments the POINTER sizeof(int) (not the value of the data fptr points to). operator++ is working, but you are confused on it's presedence with respect to the dereference operator.

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Polymorphic, I think you got it the wrong way around. Those operators have precedence from right to left. That means that the following two statements are equal:

    Code:
    *fptr3++;
    *(fptr3++);
    The address contained within the pointer is incremented by sizeof(int), then it is dereferenced.

    Code:
                            Precedence of Operators
    
    Operator                                 Order of evaluation
    
    ()  []  .  ->                                left to right
    ! ~ - ++ -- & * (type) sizeof    right to left //see this line
    *  /  %                                     left to right
    +  -                                          left to right
    <<  >>                                    left to right
    <  <=  >  >=                            left to right
    ==  !=                                     left to right
    Last edited by bennyandthejets; 01-23-2003 at 07:39 PM.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    103

    I don't think so

    Originally posted by bennyandthejets
    Polymorphic, I think you got it the wrong way around. Those operators have precedence from right to left. That means that the following two statements are equal:

    Code:
    *fptr3++;
    *(fptr3++);
    The address contained within the pointer is incremented by sizeof(int), then it is dereferenced.

    This should help you
    Code:
    int x[3] = { 10, 20, 30 };
    
    int *p = x;
    
    int  temp = *p++;
    
    // In your terms, p++ would take it to &x[1] and the value at that place
    // which is 20 would be assigned to temp
    
    but *p++ works this way
    first *p
    then p++
    
    // so reality is temp gets 10 and then p gets incremented from
    &x[0] to &x[1]
    operations done on pointers are w.r.t THEIR DATATYPE and not w.r.t the datatype of the data that they are pointing to
    Last edited by shiv_tech_quest; 01-24-2003 at 09:46 AM.
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

  6. #6
    Registered User
    Join Date
    Dec 2002
    Posts
    103

    Re: 2 Quick Pointer Questions:

    Originally posted by Krak
    I have two questions about pointers:
    [code]
    void somefunc3(int * fptr3)
    {
    *fptr3 ++;// This doesn't work.
    }


    void somefunc3(int * fptr3)
    {
    *fptr3 += 1; // This does work.
    }

    My two questions are thus:

    1.) In cases like this, does the ++ operator not work?
    2.) Is this one of the primary uses of pointers? (Through functions)
    This is the error in first form

    *fptr3++; gets translated this way
    1) *fptr;
    2) fptr++;
    I bet that's now what you wanted

    So the correction is (*fptr)++;

    (*fptr)++ is as good as

    (*fptr) + = 1; or (*fptr) = (*ftpr) + 1;
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

  7. #7
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Are you trying to say that both operations are performed separately? I don't think so. Where brackets are not included, doesn't it evaluate according to operator precedence, for each term in the expression? Ie:
    Code:
    *ptr++;
    //RIGHT TO LEFT precedence, I've proved it before
    //first ptr++ is evaluated.
    //so it becomes:
    *(ptr++);
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  8. #8
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Ok, I concede. More experimentation caused me to remember that if a ++ is after the variable, it is evaluated last. If it is before the variable, it is evaluated first. Damn, this stuff is too confusing.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05:23 AM
  2. Replies: 5
    Last Post: 04-04-2009, 04:45 AM
  3. A quick pointer question
    By samus250 in forum C Programming
    Replies: 7
    Last Post: 07-01-2008, 07:10 PM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21