An example of a secondary pointer to a c language pointer

  • 2020-04-02 02:20:30
  • OfStack

The concept of a secondary pointer

First address of any value, the level of the value of the pointer is the address, but this address as also need space to store a value, is the address space is, the value that is stored address space with the address, the secondary pointer is to get the address, the primary pointer is associated by its value (one address) name in the space data, the data can be any type and do any purposes, but only a secondary pointer of the associated data types a purpose, is to address, pointer is used for two purposes to provide read or rewrite of the target, So the second level pointer is to provide the memory address to read or rewrite the representation of the pointer is the address, the core is to point to the relationship pointer operator "*" is according to the relationship to access the pointed object. If there is A pointing relation between A and B, then A is the address of B, and "*A" means that B is accessed indirectly through this pointing relation. If the value of B is a pointer and it points to C, B is C address, "* B" indirect access if C C is integer, real, or structure, such as type variable or array elements for these types of data, B (namely C address) is generic pointer, pointer called level, to hold 1st class pointer variable called pointer variable. A(that is, the address of B) is A pointer to A pointer, called A secondary pointer, and the variable used to hold A secondary pointer is called A secondary pointer variable. According to the different situations of B, the secondary pointer is divided into pointer to pointer variable and pointer to array

Classification of secondary Pointers

Pointer to a pointer variable

In above A to B, B to C to relationships, if A, B, C are variable, namely C is common variables, B is the primary pointer variable, storing the addresses of the C of A is the secondary pointer variable, including storing the addresses of the B, the three variables in memory separate storage unit, the relationship between them, as shown in the picture below before and after the position relationship between each other is not important. In this case, B is the first-level pointer variable, and the value of B (that is, the address of C) is the first-level pointer data; A is the secondary pointer variable, and the value of A (that is, the address of B) is the secondary pointer data.

A pointer to an array

In C, arrays are used very differently from other variables. Whether it is a character type, integer type, real type variable, or a variable of structure type or pointer type, the variable name appearing in the statement represents the access to the memory unit where the variable is located, and the variable name represents the storage unit of the entire variable in memory, which can be assigned to the variable or taken out of the data for use. However, after defining an array, the array name does not represent the memory unit occupied by the entire array, but the address of the first element of the array.

Examples of secondary Pointers:


int *q;   //Define a level 1 pointer variable that points to a normal variable (that is, it stores the address of a variable)
int **p;   //Define a secondary pointer variable that points to a pointer variable (it also stores the address of a variable, but only the address of a pointer variable)
int s;
q = &s;   //Q contains the address of the integer variable s, so q is a pointer to the first level
p = &q;   //P stores the address of the first pointer q, so p is the second pointer

Example:


# include <stdio.h>
void f(int ** q);
int main(void)
{
    int i = 9;
    int * p = &i;// int  *p;  p = &i;
    printf("%pn", p);
    f(&p);
    printf("%pn", p);

    return 0;
}
void f(int ** q)
{
    *q = (int *)0xFFFFFFFF;//This is changing the value of p, independent of I, so p is no longer pointing to I
}


1. Secondary pointer related problems

#include "iostream"
#include "string"
#include "cmath"
using namespace std;
int main()
{
char ch='a';
char *p1=&ch;
char **p=&p1;
cout<<ch<<endl;
cout<<p1<<endl;
cout<<&ch<<endl;
cout<<*p1<<endl;
cout<<p<<endl;
cout<<**p<<endl;
char *p3=NULL;
//cout<<p3<<endl;
//cout<<*p3<<endl;
char **p4=&p3;
cout<<p4<<endl;
//cout<<*p4<<endl;
cout<<**p4<<endl;
}

What is the output?

2. What is the output of the following program?


#include "iostream"
using namespace std;
int main()
{
int a=12;
int *p=&a;
int **p1=&p;
cout<<a<<endl;
cout<<&a<<endl;
cout<<p<<endl;
cout<<*p<<endl;
cout<<p1<<endl;
cout<<*p1<<endl;
cout<<**p1<<endl;
}

3. What is the output of the following program?


#include "iostream"
using namespace std;
int main()
{
int *p=NULL;
int **p1=&p;
cout<<p<<endl;
//cout<<*p<<endl;
cout<<p1<<endl;
cout<<*p1<<endl;
cout<<**p1<<endl;
}
void GetMM(char **p,int n){
if(*p!=NULL)
*p=(char*)malloc(n);
(*p)[1]='a';
}
int main()
{
char *str=NULL;
GetMM(&str,10);
}
#include "iostream"
using namespace std;
union A{
int b;
char ch[2];
};
int main()
{
A a;
a.ch[0]=0x38;
a.ch[1]=0x39;
printf("%x",a.b);//3938
}


Related articles: