Analysis of pointers

A pointer consists of two elements: address and data type.

The address is a (unsigned long) data that indicates the location of the data in memory; the data type indicates how to parse the data.

 

The address pointed to is different, the pointer is different, which is easier to understand;

It should be noted that when the data type is different, the pointer is also different, which leads to many interesting uses of pointers.

 

1. A pointer to the same address will resolve the content pointed to by the address into different data according to the type of the pointer

#include <stdio.h>

int main()
{
	void *p = NULL;
	char str[6] = {0, 1, 2, 3, 4, 5};

	p = str;
	printf("0x%x\n",(int)(*(char *)p));
	printf("0x%x\n",(int)(*(int *)p));
	

	return 0;
}

rivsidn@rivsidn:~/demo/C$ ./a.sh

0x0
0x3020100

I've seen a bit of a roundabout usage like this

#include <stdio.h>

struct TEST{
	struct TEST *next;
	struct TEST **prev;
	int data;
}aa, bb;

int main()
{
	aa.data = 1;
	bb.data = 2;

	aa.next = &bb;
	bb.prev = &aa.next;

	printf("%d\n", (**bb.prev).data);
	printf("%d\n", ((struct TEST*)bb.prev)->data);

	return 0;
}

rivsidn@rivsidn:~/demo/C$ ./a.sh
2
1

2. Different types of pointers have different offsets when +1

#include <stdio.h>

int main()
{
	void *p = NULL;
	char str[6] = {0, 1, 2, 3, 4, 5};

	p = str;

	printf("%p\n", p);	
	printf("%p\n", (char *)p + 1);
	printf("%p\n", (int *)p + 1);

	return 0;
}

rivsidn@rivsidn:~/demo/C$ ./a.sh
0xbfae58b6
0xbfae58b7
0xbfae58ba

The more interesting usage is

#include <stdio.h>
#include <stdlib.h>

#define SIZE 5

struct HEADER {
	int i;
	/*...*/
};

int main()
{
	int i;
	struct HEADER *ph;
	char *p;

	ph = (struct HEADER *)malloc(sizeof(struct HEADER) + SIZE);
	p = (char *) &ph[1];
	
	return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325904681&siteId=291194637