C++,
pasted
on Jul 4:
|
|
/**
* This attempts to demonstrate the difference between arrays and pointers. Compile as:
* g++ -ansi -pedantic -Wall int_array.cpp -o test
* OR
* g++ -DBREAK -ansi -pedantic -Wall int_array.cpp -o test
*/
#include <iostream>
int
main()
{
int first[] = { 0 , 1 , 2 , 3 } ;
int second[] = { 4 , 5 , 6 , 7 } ;
int * test_ptr = 0 ;
std::cout << "The address of first is:\t" << first << '\n' ;
std::cout << "The address of second is:\t" << second << '\n' ;
std::cout << "The address of test_ptr is:\t" << test_ptr << '\n' ;
test_ptr = first ;
std::cout << "The address of test_ptr is:\t" << test_ptr << '\n' ;
test_ptr = second ;
std::cout << "The address of test_ptr is:\t" << test_ptr << '\n' ;
#ifdef BREAK
// This breaks because while test_ptr is a pointer, and can point to ANY address, second and first
// are actual ARRAYS, that is they are variables and the address of a variable cannot change
second = first ;
#endif
} /* main() */
|
Output:
|
|
The address of first is: [0, 1, 2, 3]
The address of second is: [4, 5, 6, 7]
The address of test_ptr is: 0
The address of test_ptr is: 0xbf5e2878
The address of test_ptr is: 0xbf5e2868
|
|