C + + note pointer

1. Pointers and arrays

The reason why pointers and arrays are basically equivalent is that pointers count and the internal processing of arrays in C + +. In many cases, you can use array names and pointer names in the same way.

  • In most cases, C + + treats the array name as the address of the first element of the array. The value of pointer p is the address of the first element of the array, * p is the value of the first element of the array.
  • After the pointer variable is + 1, the increment is equal to the number of bytes of the type it points to. For example, after adding 1 to the pointer pointing to double type, the pointer value is + 8; After adding 1 to the pointer to the short type, the pointer value is + 2.

The difference between the two:

  • You can modify the value of the pointer (p=p+1 (OK)), and the array name is a constant (arrayname = arrayname+1 (ERROR)).
  • The sizeof operator is applied to the array to get the length of the array, and the sizeof operator is applied to the pointer to get the length of the pointer. Sizeof = 4 regardless of the data type pointed to by the pointer.
#include <iostream>
using namespace std;
int main() {
    short stacks[3] = { 3,2,1 };
    cout << "stacks:" << stacks << endl;
    cout << "&stacks[0]:" << &stacks[0] << endl;
    short* p = stacks;
    cout << "p:" << p  << endl;
    cout << "*p:" << *p << endl;
    cout << "(p+1):" << (p + 1) << endl;
    cout << "*(p+1):" << *(p + 1) << endl;
    cout << "sizeof(p):" << sizeof(p) << endl;
    return 0;
}

Operation results

 

2. Pointer and string

1) If cout is given the address of a character, it will print from that character until it encounters a null character.  

The following are strings in different forms:

  • String in array
  • A quoted string constant
  • The string described by the pointer
char animal[10] = "bear";
const char* bird = "wren";

See the following code:

char flower[10] = "rose";
cout << flower << endl;
char animal[4] = { 'f','i','s','h' };
cout << animal << endl;

Operation results:

 

In cout and most C + + expressions, char array name, char pointer and quoted string constant are interpreted as the address of the first character of the string. The array name flower is the address of the character r, and the array name animal is the address of the character f.

2) In general, give cout a pointer that will print the address. However, if the pointer is of type char *, cout displays the string pointed to. If you want to display the address of the string, you must force this pointer to another pointer type, such as int *. As follows:

const char* bird = "wren";
cout << "bird:" << bird << endl;
cout << "(int*)bird:" << (int*)bird << endl;

Operation results:

3) When reading a string into a program, the allocated memory address should be used. The address can be an array name or a pointer initialized with new.

#include <iostream>
using namespace std;
int main() {
    char flower[10];
    cin >> flower;
    cout << "flower:" << flower << ",(int*)flower:" << (int*)flower << endl;
    cout << "sizeof(flower):" << sizeof(flower) << ",strlen(flower):" << strlen(flower) << endl;
    char* p=new char[strlen(flower)+1];
    strcpy_s(p, strlen(flower) + 1, flower);
    cout << "p:" << p << ",(int*)p:" << (int*)p << endl;
    delete [] p;
    return 0;
}

Operation results:

3. Pointers and objects

When using object pointers, you should pay attention to the following points:

  • Use general notation to declare pointers to objects

String * s;
  • You can initialize a pointer to an existing object

String * s = &sayings[0];
  • You can use new to initialize the pointer, which creates a new object

String * s = new String();
  • Using new on a class will call the corresponding class constructor to initialize the newly created object

String * s1 = new String();//The default constructor was called
String *s2 = new String("my my my");//Called String(const char *)Constructor
String *s3 = new String(sayings[0]);//Copy constructor called String(const String &)
  • Class methods can be accessed through pointers using the - > operator

if(sayings[i].length() < s->length())
  • You can apply the dereference operator (*) to the object pointer to obtain the object

if(sayings[i] < *s)
    s = &sayings[i];

 

 

Tags: C++

Posted on Sun, 05 Dec 2021 19:59:43 -0500 by mecha_godzilla