Pointer learning (2)

Arrays and pointers:
A variable has an address, and an array contains several elements. Each array element occupies a storage unit in the memory, and they all have a corresponding address.

Since a pointer variable can point to a variable, it can also point to an array element (put the address of an element in a pointer variable).

The pointer to the so-called array element is the address of the array element.

The method of defining a pointer variable pointing to an array element is the same as the pointer variable pointing to a variable introduced previously.
For example:
int a[10]; (define a as an array of 10 integer data)
int *p; (define p as a pointer variable pointing to an integer variable)

Note
If the array is of type int, the base type of the pointer variable should also be of type int.

p=&a[10];
Assign the address of the a[0] element to the pointer variable p.
That is to make p point to the 0th element of the a array.

To quote an array element, you can use:
(1) Subscript method: such as a[i] form;
(2) Pointer method: such as
*(a+i)

*(p+i)

Among them, a is the name of the array, p is a pointer variable pointing to the array element, and its initial value p=a.
Note: The array name is "translated into the address of the first element of the array.
Specific examples:

#include<stdio.h>
int main()
{
    
    
	int a[10];
	int i;

	printf("please input 10 numbers:\n");
		for(i=0;i<10;i++)
		{
    
    
	scanf("%d",&a[i]);
		}
		printf("output the 10 numbers again!\n");

		for(i=0;i<10;i++)
		{
    
    
			printf("%d",*(a+i));
		}
		printf("\n");
}

Code analysis:

	for(i=0;i<10;i++)
		{
    
    
			printf("%d",*(a+i));
		}
		printf("\n");

This section is to re-output the input ten numbers, because the array name a is the address in the array element, so you can directly use the value symbol * to output 10 numbers.

Therefore even the definition of int *p; p=a; is correct.

Guess you like

Origin blog.csdn.net/yooppa/article/details/112724791