ผลต่างระหว่างรุ่นของ "Adt lab/pointers"

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
แถว 13: แถว 13:
 
</source>
 
</source>
  
 +
To see how pointers work, let's follow this code.
  
Let's start by a simple example.
+
<source lang="cpp">
 +
  int a = 10;
 +
  int b = 20;
 +
  p = &a;
 +
  cout << (*p) << endl;
 +
 
 +
  *p = 100;
 +
  cout << a << endl;
 +
 
 +
  a++;
 +
  cout << (*p) << endl;
 +
 
 +
  p = &b;
 +
  a = *p;
 +
  cout << a << endl;
 +
</source>
  
 
== Pointers and arrays ==
 
== Pointers and arrays ==
  
 
== Other links ==
 
== Other links ==

รุ่นแก้ไขเมื่อ 15:43, 27 สิงหาคม 2558

This is part of adt lab.

Pointers

In C/C++, there is a special kind of types: pointers. Pointer variables keep locations in the memory. To declare a pointer variable, we use symbol *:

type* variable;

For example, the following code declares p as a pointer to an integer.

  int* p;

To see how pointers work, let's follow this code.

  int a = 10;
  int b = 20;
  p = &a;
  cout << (*p) << endl;

  *p = 100;
  cout << a << endl;

  a++;
  cout << (*p) << endl;

  p = &b;
  a = *p;
  cout << a << endl;

Pointers and arrays

Other links