Putting entire array in pointer?

I want to know how to write an entire array to a pointer, i know how to write the element but not the entire array itself

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

using namespace std;

int main()
{
    int my_array[] = {1,2,5,6,8,9,2,0,9,6};
    int *p_my_array = &my_array[8];

    cout << *p_my_array << endl;

    return 0;
}
Correct me if I'm wrong. An array is a pointer to sequential blocks of memory that hold the same data type. You cannot write an entire array to a single pointer. You can print the address of the array, but not it's contents all at once
oh ok.
How would i compare the contents of an address?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
    int number = 900;
    int *p_address = &number;


    cout << p_address << endl;
    cout << &number << endl;
    cin >> number;

    if(&number != p_address)
    {
        cout << "does not equal address" << endl;
    }
    if(&number == p_address)
    {
        cout << "equals address" << endl;
    }

    return 0;
}
This code has no sense

1
2
3
4
5
6
7
8
    if(&number != p_address)
    {
        cout << "does not equal address" << endl;
    }
    if(&number == p_address)
    {
        cout << "equals address" << endl;
    }


because p_address initially was set to the address of number.

int *p_address = &number

To compare values you may use the following code

if ( *p_address == number ) }{ /*...*/ }
Ok, now how would i check to see if the variable in the memory address has been changed? i was trying to compare them and see if it changed at all so one would have the initial var and the other would hold the changed version. i did this i think this is what i want:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{
    int number = 900;
    int number2;
    int *p_address = &number2;


    cout << p_address << endl;
    cout << &number << endl;
    cin >> number2;

    if(*p_address != number)
    {
        cout << "does not equal address" << endl;
    }
    if(*p_address == number)
    {
        cout << "equals address" << endl;
    }

    return 0;
}
Last edited on
Here you are not comparing addresses. You are comparing values

1
2
3
4
5
6
7
8
    if(*p_address != number)
    {
        cout << "does not equal address" << endl;
    }
    if(*p_address == number)
    {
        cout << "equals address" << endl;
    }


that is the value of number with the same value because p_address points to number.
so how would i check to see if the value has been changed? i used cheat engine to modify the value of 900 and changed it to 7 but the output still said that it was the same.
A pointer to the memory address is not sufficient enough to detect a change in the information being pointed to. Because a pointer just "points" whenever a change is made to the value at that memory address, the address of the pointer is still the same as that of the object. You will need a second variable to store the value the pointer is pointing to in order to detect a change in the variable:

1
2
3
4
5
6
7
8
9
10
11
12
#include <cstdio>

int main() {
	int number = 900, number2, *p_address = &number;
	printf("Address of p_address = %p\n", p_address);
	printf("Address of number = %p\n", &number);
	number2 = *p_address; //Store initial value held by the pointer
	number = 901;
	if (number2 != *p_address) puts("value changed!");
	if (number2 == *p_address) puts("value still the same");
	return 0;
}
uhh, thats all C code?
Not that different from c++

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

int main(void) {
	int number = 900, number2, *p_address = &number;
	std::cout << "Address of p_address = " << p_address << '\n';
	std::cout << "Address of number = " << &number << '\n';
	number2 = *p_address; //Store initial value held by the pointer
	number = 901;
	if (number2 != *p_address) std::cout << "value changed!\n";
	else if (number2 == *p_address) std::cout << "value still the same\n";
	return 0;
}
Last edited on
*pointer == number Compares the value pointed to by pointer with the value stored in number

pointer == &number Compares the value stored in pointer with the address of number

Basically * means "value pointed to by"
and & means "address of"
Topic archived. No new replies allowed.