当前位置:网站首页>C language enhancement -- pointer
C language enhancement -- pointer
2022-07-05 07:52:00 【Soy sauce;】
1. Memory profile
notes : After all pointer types are defined, the memory space takes up four
** The variable is equivalent to the address name
The address is equivalent to the memory name
The corresponding data is stored in memory
But the pointer variable stores the address , Characters are stored in character variables ,( Use four spaces as one ),,,
Seemingly one effect , Actually, it uses four spaces , The operation is exactly the same as that of one ,,,
Integer variables store integers , However, when storing, the data is rationalized and evenly distributed in memory **
A byte space corresponds to an address
To get the address is to get the corresponding variable 
Address distribution in memory
1. Sort 
2. Distribution 
2. Pointer overview
Some operators about pointers 
& Is to get the starting address of the variable , Regardless of data type , Constants also work ( Machine code , Must use & once , I can see it only because I use 16 Binary printing )
* Take out the value in the corresponding address , If variable , Get the value in the width of the corresponding data type , Such as int take 4 individual , If address , Look at type conversion , If not, take one
Array name , Function name , String names all start with addresses !!!
But a single variable is not , The variable represents the value in the address . Other long and ambiguous strings are .

/ First step : All variables are stored in memory , Let's print the storage address of the variable /
/ The second step : All variables can hold certain values , Then assign values and print / 
/ The third step : Use the pointer :1) Value 2) Move the pointer / 

+1 and ++ The effect is the same
int type +1 Move 4 Bytes
char type +1 Move 1 Bytes
Look at the specific type
The array is also more than the same plus 4, But the extracted value will be garbled
See the figure below for details. :

Code
/* * printf_test.c V1.0 * Copyright (c) 2017 Shenzhen 100ask Technology Co.Ltd.All rights reserved. * http://www.100ask.org * 100ask.taobao.com * * Test platform : ubuntu16.04(64 Bit machine ) * ubuntu9.10 (32 Bit machine ) * compiler : gcc */
#include <stdio.h>
void test0()// Test a single variable
{
char c;
char *pc;
/* First step : All variables are stored in memory , Let's print the storage address of the variable */
printf("&c =%p\n",&c);
printf("&pc =%p\n",&pc);
/* The second step : All variables can hold certain values , Then assign values and print */
c = 'A';
pc = &c;
printf("c =%c\n",c);
printf("pc =%p\n",pc);
/* The third step : Use the pointer :1) Value 2) Move the pointer */
printf("*pc =%c\n",*pc);
printf("//=================\n");
}
void test1()// Test variable storage address
{
int ia;
int *pi;
char *pc;
/* First step : All variables are stored in memory , Let's print the storage address of the variable */
printf("&ia =%p\n",&ia);
printf("&pi =%p\n",&pi);
printf("&pc =%p\n",&pc);
/* The second step : All variables can hold certain values , Then assign values and print */
ia = 0x12345678;
pi = &ia;
pc = (char *)&ia;
printf("ia =0x%x\n",ia);
printf("pi =%p\n",pi);
printf("pc =%p\n",pc);
/* The third step : Use the pointer :1) Value 2) Move the pointer */
printf("*pi =0x%x\n",*pi);
printf("pc =%p\t",pc); printf("*pc =0x%x\n",*pc); pc=pc+1;
printf("pc =%p\t",pc); printf("*pc =0x%x\n",*pc); pc=pc+1;
printf("pc =%p\t",pc); printf("*pc =0x%x\n",*pc); pc=pc+1;
printf("pc =%p\t",pc); printf("*pc =0x%x\n",*pc);
printf("//=================\n");
}
void test2()// Test array
{
char ca[3]={
'A','B','C'};
char *pc;
/* First step : All variables are stored in memory , Let's print the storage address of the variable */
printf("ca =%p\n",ca);
printf("&pc =%p\n",&pc);
/* The second step : All variables can hold certain values , Then assign values and print */
// There is already ca[3]={'A','B','C'};
pc = ca;
printf("pc =%p\n",pc);
/* The third step : Use the pointer :1) Value 2) Move the pointer */
printf("pc =%p\t",pc); printf("*pc =0x%x\n",*pc); pc=pc+1;
printf("pc =%p\t",pc); printf("*pc =0x%x\n",*pc); pc=pc+1;
printf("pc =%p\t",pc); printf("*pc =0x%x\n",*pc);
printf("//=================\n");
}
void test3()// Test array storage address
{
int ia[3]={
0x12345678,0x87654321,0x13572468};
int *pi;
/* First step : All variables are stored in memory , Let's print the storage address of the variable */
printf("ia =%p\n",ia);
printf("&pi =%p\n",&pi);
/* The second step : All variables can hold certain values , Then assign values and print */
// There is already ia[3]={0x12345678,0x87654321,0x13572468};
pi = ia;
printf("pi =%p\n",pi);
/* The third step : Use the pointer :1) Value 2) Move the pointer */
printf("pi =%p\t",pi); printf("*pi =0x%x\n",*pi); pi=pi+1;
printf("pi =%p\t",pi); printf("*pi =0x%x\n",*pi); pi=pi+1;
printf("pi =%p\t",pi); printf("*pi =0x%x\n",*pi);
printf("//=================\n");
}
void test4() Test string
{
char *pc="abc";
/* First step : All variables are stored in memory , Let's print the storage address of the variable */
printf("&pc =%p\n",&pc);
/* The second step : All variables can hold certain values , Then assign values and print */
// There is already pc="abc";
/* The third step : Use the pointer :1) Value 2) Move the pointer */
printf("pc =%p\n", pc);
printf("*pc =%c\n",*pc);
printf("pc str=%s\n", pc);
}
int main(int argc,char **argv)
{
printf("sizeof(char )=%d\n",sizeof(char ));
printf("sizeof(int )=%d\n",sizeof(int ));
printf("sizeof(char *)=%d\n",sizeof(char *));
printf("sizeof(char **)=%d\n",sizeof(char **));
printf("//=================\n");
//test0();
//test1();
//test2();
//test3();
test4();
return 0;
}
String printing
%s Use 
边栏推荐
- 1089 Insert or Merge 含测试点5
- Acwing-宠物小精灵之收服-(多维01背包+正序倒序+两种形式dp求答案)
- Extern keyword function
- Latex notes
- Typecho adds Baidu collection (automatic API submission plug-in and crawler protocol)
- 通过sql语句统计特定字段出现次数并排序
- A simple method to prove 1/t Fourier transform
- Practical application cases of digital Twins - fans
- Cadence learning records
- Global and Chinese markets of large aperture scintillators 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

Practical application cases of digital Twins - fans

Win10 shortcut key

MySql——存储引擎

Oracle-触发器和程序包

Significance and requirements of semiconductor particle control

Ten thousand words detailed eight sorting must read (code + dynamic diagram demonstration)

Altium Designer 19.1.18 - 导入板框

A simple method to prove 1/t Fourier transform

万字详解八大排序 必读(代码+动图演示)

MySQL - storage engine
随机推荐
[popular science] some interesting things that I don't know whether they are useful or not
Global and Chinese market of rammers 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of blackbody calibration source 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of resistivity meter 2022-2028: Research Report on technology, participants, trends, market size and share
Apple script
Global and Chinese markets of nano biosensors 2022-2028: Research Report on technology, participants, trends, market size and share
From then on, I understand convolutional neural network (CNN)
如何将EasyCVR平台RTSP接入的设备数据迁移到EasyNVR中?
solver. Learning notes of prototxt file parameters
[professional literacy] core conferences and periodicals in the field of integrated circuits
Record the torch encountered by win10 cuda. is_ False problem in available()
[idea] common shortcut keys
"Source code interpretation" famous programmer TJ's only library
MLPerf Training v2.0 榜单发布,在同等GPU配置下百度飞桨性能世界第一
Embedded AI intelligent technology liquid particle counter
Altium Designer 19.1.18 - 清除测量距离产生的信息
Random function usage notes
Define in and define out
Acwing - the collection of pet elves - (multidimensional 01 Backpack + positive and reverse order + two forms of DP for the answer)
Day01 markdown log entry tips