understanding pointer arithmetic

This is a discussion on understanding pointer arithmetic within the C Programming forums, part of the General Programming Boards category; In my C programing class I was given an assignment to create an array stack only using pointers to keep ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    2

    understanding pointer arithmetic

    In my C programing class I was given an assignment to create an array stack only using pointers to keep track of and change the values, however the use of pointer arithmetic was not covered completely. The course book doesn't explain it either. I'm trying to understand how to do this.

    The code I'm trying to learn with is:
    Code:
    //test file
    #include <stdio.h>
    
    
    int stack[] = {11,22,33,44};
    int *top = stack;
    
    int main()
      {
      printf("%d\n",*top);//should print 11
      *top = *(top + 1);  
      
      printf("%d\n",*top);//should print 22
      *top = *(top + 1);  
      
      printf("%d\n",*top);//should print 33
      *top = *(top - 1);  
      
      printf("%d\n",*top);//should print 22
      
      scanf("%c");//keeps window open
      return 0;
      }
    and the output I get is:
    Code:
    11
    22
    22
    0
    Im having a little bit of trouble understanding this...

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Welcome to the boards. Notice that there is a separate forum for C related questions, so you should probably post future questions there. I'm sure a moderator will move this one for you soon.

    In the code you are not incrementing the pointer. (top + 1) adds 1 to the pointer, but doesn't change it. So throughout your code, top points to the first spot in the array and top + 1 points at the second. When you do (top - 1) you are actually getting undefined behavior because it tries to point to memory before the array.

    The solution is probably to increment top with ++ rather than adding 1 to it. Using ++ will actually change what top points to.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    2
    weird, the first time I tried that it didnt work, but it works now

    and the code works the same no matter if its in a c compiler or c++
    Last edited by firstofthegreat; 02-28-2008 at 10:10 PM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,679
    Quote Originally Posted by firstofthegreat View Post
    and the code works the same no matter if its in a c compiler or c++
    Indeed. Your code has the same meaning in C and C++ (with only a minor quibble as <stdio.h> is deprecated -- slated for future removal -- in C++). The line "*top = *(top - 1);" yields undefined behaviour in both languages, as top points to the first element of the array named stack (i.e. anything is possible in either language). The rest of your code is equivalent in both languages.

    In C++ there are preferred alternatives (eg iterators) so use of raw pointers is usually discouraged. That is why those in a C++ forum will often suggest questions like yours be asked in a C forum -- even though your problem is technically the same in both C and C++. Sort of a religious snobbery.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. Replies: 41
    Last Post: 07-04-2004, 04:23 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  4. Pointer arithmetic
    By freebu in forum C Programming
    Replies: 4
    Last Post: 04-27-2003, 10:27 AM
  5. pointer arithmetic with characters
    By VanJay011379 in forum C++ Programming
    Replies: 6
    Last Post: 07-21-2002, 02:50 PM

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