Thread: Why would they use this segment instead of this one?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    1

    Why would they use this segment instead of this one?

    Why use this segment

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      int numbers[5];
      int * p;
      p = numbers;  *p = 10;
      p++;  *p = 20;
      p = &numbers[2];  *p = 30;
      p = numbers + 3;  *p = 40;
      p = numbers;  *(p+4) = 50;
      for (int n=0; n<5; n++)
        cout << numbers[n] << ", ";
      return 0;
    }
    instead of this segment?

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      int a[5];
      int *b;
      b = a;  *b = 10;
      b = a;  *(b+1) = 20;
      b = a;  *(b+2) = 30;
      b = a;  *(b+3) = 40;
      b = a;  *(b+4) = 50;
      for (int n=0; n<6; n++)
        cout << a[n] << ", ";
      return 0;
    }
    Does the first, seemingly more complicated block do something more than the second block? Even though both print the same thing?

    NOTE in the first block, p is equivalent to b in the second block. and "numbers" is equivalent to a.
    Last edited by hannahfox123; 06-18-2010 at 12:11 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. execute a code in data segment...
    By ravindragjoshi in forum Windows Programming
    Replies: 1
    Last Post: 06-12-2007, 11:43 PM
  3. shmget returning invalid segment size specified
    By BobS0327 in forum Linux Programming
    Replies: 6
    Last Post: 11-08-2006, 06:39 PM
  4. Declare an array in the BSS segment
    By ^xor in forum C Programming
    Replies: 1
    Last Post: 05-27-2005, 05:12 PM
  5. LISP (DrScheme) any one?
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-31-2004, 12:52 PM