help with arrays in C++

This is a discussion on help with arrays in C++ within the C++ Programming forums, part of the General Programming Boards category; Code: const int SIZE = 10; int a[SIZE] = {1,2,3,4,5,6,7,8,9,10}; int *p; int i; p = a+(SIZE/2); cout << p ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    3

    help with arrays in C++

    Code:
    const int SIZE = 10;
    	int a[SIZE] = {1,2,3,4,5,6,7,8,9,10};
    	int *p;
    	int i;
    
    	p = a+(SIZE/2);
    	cout << p << endl;
    
    	for(i = 0; i < SIZE/2; i++)
    	{
    		cout << (p+i) << " = " << *(p+i) << " "
    			 << (p-(i+1)) << " = " << *(p-(i+1)) 
                 << endl;
    	}
    who can explain this coding to me

    how i assume the starting Aray a is 0012FF30
    Last edited by Salem; 04-10-2011 at 01:04 AM. Reason: Added [code][/code] tags - learn to use them yourself

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,751
    You can't assume the array starts anywhere.

    You could try outputting a+i rather than p+i
    All it is doing is outputting the addresses and contents of various elements of the array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  2. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  3. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  4. Parallel Arrays with Multiple Arrays
    By Billye Scott in forum C++ Programming
    Replies: 0
    Last Post: 03-02-2002, 11:14 PM
  5. separating line of arrays into array of arrays
    By robocop in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2001, 01:43 AM

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