Pointer practice (2)

Refer to array elements through pointer variables:

#include<stdio.h>
int main()
{
    
    
	int i;
	int shu[10];
	int *p;
    p=shu;

	printf("please input ten numbers!\n");
	for(i=0;i<10;i++)
	scanf("%d",p+i);
    printf("and i will output ten numbers....\n");
	for(i=0;i<10;i++,p++)
		printf("%d",*p);
	printf("\n");
	return 0;
}

Code thoughts:

Through this piece of code, it reflects the flexible use of pointers.
Since the array name is the address, p=shu is made in advance to give each element the corresponding address when inputting each array element in a loop, so no & is added
when doing the input. Each array element has a corresponding address and then uses the value symbol "*" to output each element in turn according to the address.
That is, enter the address to find the address.

Example:
Input a certain year, certain month, and certain day, calculate and output that it is the day of the year.

#include<stdio.h>
void datedays(int year,int month,int day,int *p);
int main()
{
    
    
	int year, month, day,days;
	printf("请输入年月日:\n");
	scanf("%d-%d-%d",&year,&month,&day);
	datedays(year,month,day,&days);
	if(days==-1)
		printf("你的输入有误,请重新输入!\n");
	else
		printf("%d-%d-%d是这一年的第%d天!\n",year,month,day,days);
}
void datedays(int year,int month,int day,int *p)
{
    
    
	int i;
	int shu[12]={
    
    31,28,31,30,31,30,31,31,30,31,30,31};
	*p=0;
	if(year<0||month<=0||month>12)
	{
    
    
		*p=-1;
		return;
	}

	if(year%4==0&&year%100!=0||year%400==0)
	{
    
    
	if(month==2&&day>29)
	{
    
    
		*p=-1;
		return;
	}
        shu[1]++;
		for(i=0;i<month-1;i++)
		{
    
    
		*p+=shu[i];
		}
		
	
	}
	else 
	{
    
    
			if(month==2&&day>28)
			{
    
    
	            *p=-1;
	        	return;
			}
		for(i=0;i<month-1;i++)
		{
    
    
			*p+=shu[i];
		}
	
	}
	*p+=day;

}

This code is coded out by itself based on reading the textbook code: there are basic questions to explain.

Code analysis: the
year involved is naturally judged as a leap year and the average year.
Calculate the date as the day of the year, and the number of days involved in each month is different, plus the leap and average years. February is particularly special. Therefore, the usual selection structure and the nesting of the selection structure are used, and then an array is used to initially define the number of days in 12 months, and the operation is performed on February according to whether it is a leap year or a normal year.
Such as:

if(year%4==0&&year%100!=0||year%400==0)
	{
    
    
	if(month==2&&day>29)
	{
    
    
		*p=-1;
		return;
	}
        shu[1]++;
		for(i=0;i<month-1;i++)
		{
    
    
		*p+=shu[i];
		}

The content of this sub-function is this function.
After this judgment is completed, the cycle structure is used to accumulate and sum the number of days before the entered month, and then add the number of days entered to determine that the date is the day of the year.
At the same time, the code is also interspersed with incorrect input prompts. . .

Question sharing after the first self-coding:

  1. After learning pointers first, the code is more concise and easy to understand (in fact, it can be concise and easy to understand without using pointers, just familiar with pointer usage, hee hee).
  2. The child function datedys defines a pointer variable. If he can get the value of days, he needs to add an address symbol & when calling the child function of datedays, which is &days
  3. In the datedays sub-function, *p=0 in advance; is to accurately output the result, so the if statement of the main function is written as (days==-1) instead of 0; as for why,

Because *p will be calculated in the final number of days and then calculated with the preset initial value, for example: 2020-1-12 should be the 12th day, if the preset initial value is -1, it will be 11 days,
but when you want When canceling this preset initial value, you will find that the output result is negative.
Insert picture description here
That’s it...
that’s because we will define sum=0 when accumulating;

The same is true for pointers: *p is actually sum; therefore pointers are not difficult, but application is difficult.

Guess you like

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