Array address

I need help with the array address. I turned in this code but my teacher sent it back to me saying "Instead of outputting the address of the arrays, you are outputting the addresses of the pointers. You should display the address that the pointer holds".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
 #include <iostream>
using namespace std;

int *duplicateArray(const int *, int);
void displayArray(const int[], int);
void sortArray(int[], int);
void showArray(const int [], int);

int main()
{
	const int SIZE = 50;
	int numbers[SIZE] = { 888, 552, 642, 191, 935, 584, 959, 495, 945, 27, 240, 637, 712, 608, 924,
	630, 712, 234, 275, 258, 918, 15, 320, 144, 365, 305, 696, 783, 114,
	434, 925, 462, 266, 985, 318, 728, 285, 79, 337, 115, 662, 798, 970,
	433, 758, 213, 284, 154, 126, 543 };
	
	int *dupA = nullptr;
	
	//makes a duplicate of numbers array into array called dupA
	dupA = duplicateArray(numbers, SIZE);

	//sorts duplicate array
	sortArray(dupA, SIZE);
	
	cout << "The address of the original array is " << &numbers << endl;
	cout << "The address of the duplicate array is " << &dupA << endl;
	
	
	//displays original array
	cout << "The contents of the numbers array: \n";
	showArray(numbers, SIZE);
	
	//displays sorted duplicate array
	cout << "The contents of the duplicate array: \n";
	showArray(dupA, SIZE);
	
	delete [] dupA;
	dupA = nullptr;
	
	
	system("pause");
	return 0;
	
}
/* This function duplicates the Original array (numbers)
	Inputs:
		arr - array
		size - size of array, numbers in it
	Outputs: none
	Return: Duplicate array
*/
int *duplicateArray(const int *arr, int size)
{	int *newArray = nullptr;
	
	//validates size. If 0 or less returns null pointer
	if (size <= 0)
		return nullptr;
	
	//Allocate a new array
	newArray = new int[size];
	
	//copies data from numbers array to new array
	for (int index = 0; index < size; index++)
		newArray[index] = arr[index];
	
	return newArray;
}

/* This function sorts an array
	Inputs:
		array[] - array
		size - numbers in array
	Outputs: None
	Return: Swaps the numbers in the array from least to greatest
*/
void sortArray(int array[], int size)
{
	bool swap;
	int temp;

	do
	{
		swap = false;
		for (int count = 0; count < (size - 1); count++)
		{
			if (array[count] > array[count + 1])
			{
				temp = array[count];
				array[count] = array[count + 1];
				array[count + 1] = temp;
				swap = true;
			}
		}
	} while (swap);
}

void showArray(const int array[], int size)
{
	for (int count = 0; count < size; count++)
		cout << array[count] << " ";
	cout << endl;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

int main()
{
    int foo = 7;
    int* bar = &foo;
    std::cout << "foo  " << foo << '\n';
    std::cout << "&foo " << &foo << '\n';
    std::cout << "bar  " << bar << '\n';
    std::cout << "&bar " << &bar << '\n';
    std::cout << "*bar " << *bar << '\n';
    int gaz[] {42, 5};
    std::cout << "gaz  " << gaz << '\n';
    bar = gaz;
    std::cout << "bar  " << bar << '\n';
    std::cout << "&bar " << &bar << '\n';
    std::cout << "*bar " << *bar << '\n';
    return 0;
}

foo  7
&foo 0x716b8e3212bc
bar  0x716b8e3212bc
&bar 0x716b8e3212c8
*bar 7
gaz  0x716b8e3212c0
bar  0x716b8e3212c0
&bar 0x716b8e3212c8
*bar 42
I don't understand what that means I'm sorry
The hex numbers are addresses.
Which lines do print the same value?
Which lines print different value?

How does the output of bar, &bar, and *bar differ?
How does line 14 change the bar?
Its a syntax thing.

the name of the array is just like a pointer in many ways.
what you said was:

cout << "The address of the original array is " << &numbers << endl;
cout << "The address of the duplicate array is " << &dupA << endl;

if the name of the array is like a pointer, as I said, then the above prints the address of the (effective) pointer. dupa actually IS a pointer and there you also take its address.

that is, consider this syntax:

int * ip = numbers; //ip[0] is exactly identical to numbers[0] now. It is not a copy, they point to the same memory because numbers is like a pointer and behaves as one here.

int **ipp = &ip; //ipp is the address of ip. A pointer to a pointer, as denoted by the syntax, **, or a 'double pointer' if you think that way.

this is what you print out:
&dupa which, as seen in the ipp example above, is a ** or the address of the address of something!

what you need is:

cout << numbers << endl; //I think you may have to cast this to an int, and probably want it in hex because its traditional, but this is the gist of it: numbers is the address, don't take the address of that (**) just use it as is (single pointer *).

same for dupa ... just print it, don't take its address again.

One more just to drive it home.
int x[10];
int i =3;
x[i] = 11;
if x were the ram on your computer, i would be a pointer into it.
that is, a pointer is really just an integer that stores an index into the giant array that is your ram. You don't want to print &i (the address of i), you want to print i, if you want to know what the index in question is, see?

*** This explain is entirely conceptual. Actual pointers, array names, etc are more complicated with additional details, but this is the top level simple version to guide you to understanding of what you did wrong and how to fix it.









Topic archived. No new replies allowed.