C++. Pointers. Part 3. Unmanaged pointers and arrays. Pointer to structure. Pointer to class




Pointers. Part 3. Unmanaged pointers and arrays. Pointer to structure. Pointer to class

This topic is based on the theme: “Pointers. Managed and unmanaged pointers”.


Contents


Search other websites:

1. How to declare an unmanaged pointer (*) to an array of integers? Example

To set up a pointer to an array, you must assign this pointer address of the first element of the array.

You can also, if desired, assign a pointer to the address of i-th element of the array.

Example. Suppose we are given an array of integers and a pointer to an int:

// Setting a pointer to an array of integers
int M[20]; // Array M of 20 integers
int *p;    // pointer to int, value of p is undefined

To set up a pointer to the first element of the array, there are 2 ways:

// way №1
p = &M[0]; // Pointer p points to the first element of the array

// way №2
p = M; // Too, as in the No. 1

2. How can define an unmanaged pointer (*) to an array of real numbers?

For real numbers, the pointer to the array is set exactly as for integers:

// Setting a pointer to an array of real numbers
float X[20]; // array X of 20 real numbers
float *p;    // Pointer to float, p is undefined

// way №1
p = &X[0];
// way №2
p = X; 

// Access to an array element via a pointer
*p = -4.5f; // X[0] = -4.5

3. Ways to assign an unmanaged pointer (*) to the value of the address of a certain element of the array. Examples

Example 1. Methods for assigning to pointer the address of the first item of one-dimensional array.

// Access to array elements through the pointer
int A[20]; // array
int * p;   // pointer

// way 1
p = A; // p points to array A

// way 2
p = &A[0]; // p points to array A

Example 2. Methods for assigning an address to the i-th element of a one-dimensional array.

// Access to array elements through the pointer
int A[20]; // array
int * p;   // pointer
int i;

i = 3;

// way 1
p = &A[i]; // pointer p points to the i-th element of the array

// way 2
p = A+i;

4. How to access the array items using unmanaged (*) pointer? Examples

Example 1. Suppose given array A, containing 10 real numbers. Using the pointer, you need to change the values of the elements of the array with indices 0, 2, 7.

// one-dimensional array and pointer
float A[10];    // array of 10 real numbers
float *p;       // pointer to float

p = &A[0];      // p points to A

*p = 13.6;      // A[0] = 13.6
*(p+2) = 50.25; // A[2] = 50.25
*(p+7) = -3.2;  // A[7] = -3.2

Figure 1 shows the result of the above example.

С++ pointer array figure

Figure 1. The result of Example 1

Example 2. Zeroing an array of integers using a pointer to the array.

// Access to array elements through the pointer
int A[20]; // array
int * p;   // pointer
int i;

for (i=0; i<20; i++)
    A[i] = i;

p = A; // p points to A

// way 1
for (i=0; i<20; i++)
    *(p+i) = 0;

// way 2
for (i=0; i<20; i++)
    *(A+i) = 0; // The name of the array is also a pointer

// way 3
for (i=0; i<20; i++)
    p[i] = 0;

5. How to access the items of a two-dimensional array through a pointer? Examples

For the pointer to point to a two-dimensional array, the pointer must be assigned the value of the first item of the two-dimensional array. There are several ways for this. For a pointer to point to a row in a two-dimensional array, you need to assign the value of the address of that string.






Example 1. Given a two-dimensional array M of integers and pointer p. Implement access to the items of array M through a pointer.

// pointer to two-dimensional array
int M[6][9]; // two dimensional array M of integers
int *p1, *p2, *p3; // pointers to int

p1 = (int *)M; // p1 = &M[0], p1 points to the string with index 0
p2 = (int *)M[2]; // p2 points to the string with index 3
p3 = (int *)&M[5]; // p3 points to the string with index 5

// Access to a specific item of an array through a pointer
 *p1 = 39;  // M[0][0] = 39
 *p2 = 88;  // M[2][0] = 88
 *p3 = 100; // M[5][0] = 100

*(p1+3) = 27;  // M[0][3] = 27
*(p2+5) = -13; // M[2][5] = -13
*(p3+8) = 19;  // M[5][8] = 19

Figure 2 shows the result of the above code.

C++ pointer two-dimensional array

Figure 2. Two dimensional array and pointer

Example 2. A two-dimensional array of real numbers of size 3×4 is given. By using a pointer you need to get access to the array item that is in position 2, 3.

// A pointer to a specific item
double B[3][4]; // array
double * pb;    // pointer to double

pb = &B[2][3]; // Pb points to an item of array B with index (2,3)
*pb = 8.55;    // B[2][3] = 8.55
6. Access to items of a multidimensional array through a pointer. Examples

Example. Description of the pointer to the three-dimensional array of size 2 × 3 × 5. Access to the element of the array with a pointer.

// pointer to a three-dimensional array
float A[2][3][5]; // three-dimensional array
float *p; // pointer to float

p = (float *)A; // p points to the firs item of array
p = &A[0][0][0]; // same

// Access to an array element with index [2] [0] [3]
p = &A[2][0][3];
*p = -30; // A[2][0][3] = -30

7. How to describe a unmanaged (*) pointer to a structure? Example

Example 1. Let outside the description of the class describe a new type – the structure ARRAY_INTS:

typedef struct Array_Ints
{
    int n;
    int A[20];
} ARRAY_INTS;

Then, in some class method (for example, the click event handler on a button), you can use the structure

ARRAY_INTS AI; // structure variable AI
ARRAY_INTS * pAI; // pointer to the structure

pAI = &AI; // pAI points to structure of ARRAY_INTS type

// access fields of the structure using a variable AI
AI.n = 2;
AI.A[0] = 25;
AI.A[1] = 33;

// access fields of the structure using pointer pAI
pAI->n = 3;      // AI.n = 3
pAI->A[0] = 28;  // AI.A[0] = 28
pAI->A[1] = 345; // AI.A[1] = 345
pAI->A[2] = -33; // AI.A[2] = -33

Example 2. Allocating memory for an ARRAY_INTS structure (see previous example) and access to structure fields via a pointer:

// Example of allocating memory for a pointer to a structure
ARRAY_INTS * pAI2; // A pointer to an ARRAY_INTS structure

pAI2 = (ARRAY_INTS *)malloc(sizeof(ARRAY_INTS)); // memory allocation

// Filling fields
pAI2->n = 2;
pAI2->A[0] = 82;
pAI2->A[1] = 34;

8. How to define unmanaged (*) pointer to the class? Example

Let in the module “MyClass.h” the description is given:

// Class definition in the module "MyClass.h"
class MyClass
{
    private:
        int x; // class fields
        int y;
    public:
        // class methods
        void SetXY(int nx, int ny);
        int GetX(void);
        int GetY(void);
        MyClass(void);
};

The “MyClass.cpp” module provides the implementation of class methods:

// Implementation of class methods in the module "MyClass.cpp"
#include "StdAfx.h"
#include "MyClass.h"

MyClass::MyClass(void)
{
    x = 0;
    y = 0;
}

void MyClass::SetXY(int nx, int ny)
{
    x = nx;
    y = ny;
}

int MyClass::GetX(void)
{
    return x;
}

int MyClass::GetY(void)
{
    return y;
}

To access the fields and methods of a class through a pointer, you can write the following code:

// Access to methods and fields of a class through a pointer
MyClass * p; // pointer to a class

// Allocating memory for the pointer
p = (MyClass *)malloc(sizeof(MyClass));

// Access to class methods through a pointer
p->SetXY(5, 6); // Call the SetXY() method of the class

int x = p->GetX(); // x = 5
int y;
y = p->GetY(); // y = 6

9. Example of definition a managed pointer (^) to a class.

In Visual C++, if an application is created to run in the CLR environment, you can describe a managed pointer to the class. In this case, the class must be declared with the qualifier ref. The allocation of memory for the pointer is performed by the gcnew utility.

Example. Let there be given a class that is described in the module “MyClass2.h“.

ref class MyClass2
{
    private:
        int x;
        int y;

    public:
        void SetXY(int nx, int ny);
        int GetX(void);
        int GetY(void);
        MyClass2(void);
};

Implementation of class methods in the module “MyClass2.cpp” is as follows:

#include "StdAfx.h"
#include "MyClass2.h"

MyClass2::MyClass2(void)
{
    x = 0;
    y = 0;
}

void MyClass2::SetXY(int nx, int ny)
{
    x = nx;
    y = ny;
}

int MyClass2::GetX(void)
{
    return x;
}

int MyClass2::GetY(void)
{
    return y;
}

Then you can use a managed pointer to the class as follows:

// An example of class access via a managed pointer
MyClass2 ^p; // managed pointer to a class
int x, y;

p = gcnew MyClass2; // Allocating memory for the pointer

p->SetXY(-8, 32); // Calling the SetXY() method via pointer p

x = p->GetX(); // x = -8
y = p->GetY(); // y = 32


Related topics