Thread: What's wrong with this (pointers)

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    107

    What's wrong with this (pointers)

    Just starting off, trying to understand pointers. The basic idea is ok. I am wondering why you can't increment a pointer in a loop and hence assign the referenced variable a new value this way.

    Such as,

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int a; //int assigns 2 bytes of memory
        int *b; //declare pointer b 
        b = &a; //set b to address of a
        //*b= 0; //set dereference pointer(thus value of a = dereference value)
        for(int i =0; i<10; i++)
        {
            cout <<a<<"\n";
            *b++; //shouldn't this assigned a new value to a??
        }
        return 0;
    }
    If you say

    Code:
    int a=5;
    int *b=&a
    *b = 7
    then a =7.

    Thus a pointer allows you to both know the memory address of a variable(object) and also change that referenced variable through dereference.

    So why doesn't the above loop change the value of a to increase by 1 each time?

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
        for(int i =0; i<10; i++)
        {
            cout <<a<<"\n";
            *b++; //shouldn't this assigned a new value to a??
        }
    the post increment operater will be executed first -> first the pointer is incremented pointing to the next int in memory and that is dereferenced

    you want (*b)++

    Kurt

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by ZuK View Post
    the post increment operater will be executed first -> first the pointer is incremented pointing to the next int in memory and that is dereferenced
    That's not exactly correct. The expression b++ has the effect of incrementing b, and a result equal to the original value of b. It is then the original value of b that is dereferenced.

    First time through the loop *b++ produces a result equal to a, and increments b. The other times through the loop, however, start with b pointing to a non-existant variable (or location in memory). So dereferencing produces undefined behaviour (and the increment causes b to walk through memory that, as far as the program is concerned, does not exist).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    *b++ =>
    *(b++) =>
    *b
    ++b

    Also, a is not initialized, so you will get garbage or undefined behavior when you dereference b.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Something wrong with pointers
    By serg_yegi in forum C Programming
    Replies: 3
    Last Post: 03-26-2010, 06:38 PM
  2. Replies: 6
    Last Post: 06-16-2008, 04:06 AM
  3. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  4. Passing variables by pointers...what am I doing wrong?
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 06-07-2002, 02:10 PM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM