Pointer declare

Pages: 12
ok understand thanks
closed account (E0p9LyTq)
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
#include <iostream>

int main()
{
   int v1  = 5;
   int v2 = 15;

   int* p1 = &v1;
   int* p2 = &v2;

   std::cout << "Assigning values.....\n";
   //print out values
   std::cout << "v1: " << v1 << "\t\tv2: " << v2 << '\n';
   //print out addresses
   std::cout << "&v1: " << &v1 << "\tv2: " << &v2 << '\n';

   //print out addresses
   std::cout << "p1:  " << p1 << "\tp2: " << p2 << '\n';
   //print out values
   std::cout << "*p1: " << *p1 << "\t\t*p2: " << *p2 << "\n\n";

   *p1 = 10;
   *p2 = *p1;

   std::cout << "Changing values.....\n";
   std::cout << "v1: " << v1 << "\t\tv2: " << v2 << '\n';
   std::cout << "&v1: " << &v1 << "\tv2: " << &v2 << '\n';

   std::cout << "p1:  " << p1 << "\tp2: " << p2 << '\n';
   std::cout << "*p1: " << *p1 << "\t\t*p2: " << *p2 << "\n\n";

   p1 = p2;

   std::cout << "Changing pointers.....\n";
   std::cout << "v1: " << v1 << "\t\tv2: " << v2 << '\n';
   std::cout << "&v1: " << &v1 << "\tv2: " << &v2 << '\n';

   std::cout << "p1:  " << p1 << "\tp2: " << p2 << '\n';
   std::cout << "*p1: " << *p1 << "\t\t*p2: " << *p2 << "\n\n";

   *p1 = 20;

   std::cout << "Changing values.....\n";
   std::cout << "v1 is " << v1 << '\n';
   std::cout << "v2 is " << v2 << '\n';
}

Assigning values.....
v1: 5           v2: 15
&v1: 0110F748   v2: 0110F744
p1:  0110F748   p2: 0110F744
*p1: 5          *p2: 15

Changing values.....
v1: 10          v2: 10
&v1: 0110F748   v2: 0110F744
p1:  0110F748   p2: 0110F744
*p1: 10         *p2: 10

Changing pointers.....
v1: 10          v2: 10
&v1: 0110F748   v2: 0110F744
p1:  0110F744   p2: 0110F744
*p1: 10         *p2: 10

Changing values.....
v1 is 10
v2 is 20

Line 32. p1 was changed from pointing to the v1 variable to pointing to the v2 variable. Both pointers are now pointing to the same address. The same variable.

You no longer can change v1's value using the pointers.

The addresses that print out will vary from compiler to compiler, and maybe even vary each time you run the program.
ok thanks
what is void function ? Under what circumstances would it be used?
any body tell me simple explain
It's a function that doesn't return anything.

Use it when you want a function that doesn't return anything.
can do simple sample to me see ? thanks
void foo(string s)
{
cout << s << endl;
}

void is just a keyword for nothing, or unknown.
Last edited on
ok thanks
#include <iostream>
using namespace std;

void increment_all (int* start, int* stop)
{
int * current = start;
while (current != stop) {
++(*current); // increment value pointed
++current; // increment pointer
}
}

void print_all (const int* start, const int* stop)
{
const int * current = start;
while (current != stop) {
cout << *current << '\n';
++current; // increment pointer
}
}

int main ()
{
int numbers[] = {10,20,30};
increment_all (numbers,numbers+3);
print_all (numbers,numbers+3);
return 0;
}

any body can teach me step by step ? tq
Teach what?
pointer const
Topic archived. No new replies allowed.
Pages: 12