C-language pointer second-level pointer example _c language

Source: Internet
Author: User

The concept of two-level pointers

First, any value has an address, and the value of a level pointer is an address, but this address as a value also need space to store, is the space has the address, this is the address that the value of the space to have the address, the two-level pointer is to obtain this address, the first-level pointer is associated with its value (an address) in the name of the space in the data , this data can be of any type and for any purpose, but the two-level pointer is associated with only one type of data for one purpose, that is, the address, the pointer is two uses to provide the target read or overwrite, then the two-level pointer is to provide for the memory address of the reading or rewriting the pointer is the representation of the address, The core is the function of pointing to the relational pointer operator "*" is to point to the object to which the relationship is accessed. If there is a pointing relationship of a to B, then a is the address of B, and "*a" means indirectly accessing B through this pointing relationship. If the value of B is also a pointer, it points to C, B is the address of C, and "*b" means indirect access to C if C is a variable of type integer, solid, or struct, or an array element that holds data of these types, then B (that is, C's address) is a normal pointer, called a first-level pointer, The variable that holds the first-level pointer is called a primary pointer variable. A (i.e., B's address) is a pointer to a pointer, called a level two pointer, and a variable that holds a level two pointer is called a two-level pointer variable. According to the different cases of B, the level two pointer is divided into pointers to pointer variables and pointers to arrays.

Classification of two-level pointers

Pointer to pointer variable

In the point where a point B and B point to C, if a, B, C are variables, that is, C is a normal variable, b is a pointer variable, which holds the address of C, where a is a two-level pointer variable, which holds the address of B, then these 3 variables in memory occupy their respective storage units, The relationship between them is not important, as shown in the figure below. At this point, B is a pointer variable, the value of B (that is, the address of C) is the first pointer data; A is a level two pointer variable, and the value of a (that is, the address of B) is a level two pointer data.

Pointer to array

In C, an array differs greatly from other variables in its use. Whether it's character type, an integral type, a real variable, or a struct type or a variable of a pointer type, in which a variable name appears to represent access to the memory unit of the variable, which represents the memory unit of the entire variable, which can be assigned a value, or the data can be used from it. But after defining an array, the array name does not represent the memory cells occupied by the entire array, but rather the address that represents the first element of the array.

Second-level pointer example:

Copy Code code as follows:

int *q; Defines a first-level pointer variable that points to a normal variable (that is, it has the address of a variable)
int **p; Defines a two-level pointer variable that points to a pointer variable (it also has a variable address, but is the address of a pointer variable)
int s;
Q = &s; Q is the address of the integer variable s, so Q is a first-level pointer
p = &q; P is the address of the first-level pointer Q, so p is a two-level pointer

Example:

Copy Code code as follows:

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

return 0;
}
void f (int * * q)
{
*Q = (int *) 0xffffffff;//here is a change to the value of P, which has nothing to do with I, p no longer points to I
}


1, two-level pointers related issues
Copy Code code as follows:

#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;
}

Think about what the output above is?

2, the following procedure, what is the output?

Copy Code code as follows:

#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?

Copy Code code as follows:

#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
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.