当前位置:网站首页>c pointer learning (1)

c pointer learning (1)

2022-08-11 05:47:00 sunshine boy cony

定义

  1.指针就是个变量,用来存放数组,地址唯一标识一块内存空间.

  2.指针的大小是固定的4/8个字节(32位平台、64位平台).

  3.指针是有类型.指针的类型决定了指针的+-整数的步长,指针解引用操作的时候的权限.

  4指针的运算

指针类型

    int* p; //整形指针 - 指向整形的指针
    char* pc; //字符指针 - 指向字符的指针
    void* pv; //无类型的指针 - Pointer to an untyped pointer
    void* parr[5]; //指针数组 - 由指针构成的数组
    void (*parra)[5];//数组指针 - 指向数组的指针
    void (*parrf)(void,void);//函数指针 - 指向函数的指针

整形指针

        一个类型为“整形指针”的变量,Stores the address of an integer variable.

	//int 类型指针步长为4
    char str[] = "123456789";
	int* pi = (int*)str;
	printf("%p,%p,%d", pi, pi + 1, (int)(pi + 1) - (int)pi);//输出,地址1,地址1+4,4

 字符型指针

          一个类型为“字符指针”的变量,Stores the address of a character variable.

	//char 类型指针步长为1
    char str[] = "123456789";
	char* pi = (char*)str;
	printf("%p,%p,%d", pi, pi + 1, (int)(pi + 1) - (int)pi);//输出,地址1,地址1+1,1

 Constructor type pointer

         一个类型为“Construct pointer”的变量,Stores the address of a construct variable.

//in this methodstruct存储占用16字符,So start long16
//You need to pay attention to how the struct is stored
typedef struct MyStruct
{
	int ccc;
	int ccc2;
	int ccc3;
	int ccc4;
} struct_new;
int main() {
	struct_new* p;
	char str[] = "123456789";
	p =(struct_new*)str;
	printf("%p,%p,%d",  p, p + 1,(int)(p+1)-(int)p);//地址1,地址1+16,16
}

structsizeof is not always equal to each member sizeof 总和.This is because the compiler adds paddingpadding bytesto avoid alignment issues.Only if the members of the struct are followed by members that occupy more memory,Or add padding only at the end of the structurepadding.Different compilers may have different alignment constraints.

#include <stdio.h>
int main()
{
	struct A {

		// sizeof(int) = 4
		int x;
		// Padding of 4 bytes

		// sizeof(double) = 8
		double z;

		// sizeof(short int) = 2
		short int y;
		// Padding of 6 bytes
	};
	printf("Size of struct: %ld", sizeof(struct A));
	return 0;
}

 The storage order does not change,Align with the longest one-bit type,所以struct A的长度为24.

// C program to illustrate
// size of struct
#include <stdio.h>
int main()
{
	struct B {
		// sizeof(double) = 8
		double z;

		// sizeof(int) = 4
		int x;

		// sizeof(short int) = 2
		short int y;
		// Padding of 2 bytes
	};
	printf("Size of struct: %ld", sizeof(struct B));
	return 0;
}

 

 The padding of the previous bit can drop the position,to put the data in it.

指针数组

        一个类型为“整形指针”的数组变量,存储了多个整数变量的地址. 

	int a[5] = { 1,2,3,4,5 };
	int* parr[5] = { &a[0],&a[1],&a[2],&a[3],&a[4] };
	printf("%d,%d,%d,%d,%d",*parr[0], *parr[1], *parr[2], *parr[3], *parr[4]);
    //输出1,2,3,4,5

 类型 指针[m]

 数组指针

一个类型为“数组指针”的变量,存储了一个数组变量”的地址. 

	int a[5] = { 1,2,3,4,5 };
	int(*parr)[5] = a;
	printf("%d", *((int*)(parr+1)-1));
    //输出5

数组指针parr存储a数组的地址,因为parr是数组指针,所以parr+1增加5*4=20个字节,在被(int*)After being converted to an integer pointer(int*)(parr+1)-1减少4个字节,最后指向a[5]的地址,输出5.

类型 指针[n]

数组指针数组

int main() {
	int a[5] = {1,2,3,4,5};
	int b[5] = {6,7,8,9,10};
	int(*parr[2])[5];
	parr[0] = &a;
	parr[1] = &b;
	//printf("%x,%x\n", &a,&b);
	printf("%d,%d", *((int*)parr[0]+3), *((int*)(parr[1]) + 0));
    //输出4,6
}

 一个parr[2]指针数组,指向的数组类型为int长度为5.

类型 (指针[m])[n]

函数指针

    int add(int a, int b) {
	    return a+b;
    }
    int main() {
	    int a = 1;
	    int b = 1;
    	int (*ptemp)(int, int) = add;
	    printf("%d", ptemp(a, b));
        //输出2
    }

对于函数指针int (*ptemp)(int,int)=temp;中int (*ptemp)(int,int)=temp;3Each variable type is ANDtempkeep it aligned.

 函数指针数组

int add(int a, int b) {
	return a+b;
}
int cut(int a, int b) {
	return a - b;
}
int main() {
	int a = 1;
	int b = 1;
	int (*ptemp[2])(int, int) = {add,cut};
	printf("%d,%d", ptemp[0](a, b),ptemp[1](a,b));
}

The attribute of the function pointer array is inint (*ptemp[2])(int, int)而不是int (*ptemp)(int, int)[2].

函数类型 指针(参数1类型,参数2类型).

 

原网站

版权声明
本文为[sunshine boy cony]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/223/202208110512478741.html