Eighth test report

Pilot project: Pointer experiment

Name: Jin Fang experiment Location: 514 Things laboratory experiments Time: June 12, 2019

experimental project

  • Pointer and base pointer arithmetic

  • Data Exchange

  • Reverse connection string and the string

  • Parity array elements are arranged

1, experimental purposes, and requirements

(1) The method of concepts and definitions master pointer.

(2) arithmetic operation and control pointer

(3) to grasp the relationship between pointers and arrays

(4) and a string pointer to grasp the relationship

(5) be familiar with the function pointer as an argument and returns a pointer to the function

(6) Learn function pointer

Second, the experimental content

Experiment 1 8.3.1 pointer and base pointer arithmetic

1, the problem described in
the definition of an integer variable pointer p, so that 'yi it points to an integer variable a, the definition of a floating-point pointer q, to point to a floating point variable B, while the definition of an integer variable c, and additionally 3 the initial assignment. Pointer variable, calls the function scanf input values are a and b. Or indirectly via the pointer and output a, b values. Pq output by hexadecimal value and a, b of the address. The point c p, c accessed indirectly by the value p and outputs. And an output value of address c p, and the results compared with the above.
 2, experiment code

 

#include<stdio.h>
int main ()
{
	int *p,a,c=3;
	float *q,b;
	p=&a;
	q=&b;
	printf("pleasse input the value of a,b:");
	scanf("%d%f",&a,&b);
	printf("result:\n");
	printf("      %d,%f\n",a,b);
	printf("      %d,%f\n",*p,*q);
	printf("the addrese of a,b:%p,%p\n",&a,&b);
	printf("the addrese of a,b:%p,%p\n",p,q);
	p=&c;
	pierintf("c=%d\n",*p);
	printf("the addrese of a,b:%x,%x\n",p,&c);
	return 0;
	 
}
		                                                                           

  

operation result

 

 

3. Analysis: This procedure is relatively simple, there is no difficulty, no problem.

Experiment II. 8.3.2 Data Exchange

1, the problem described in
the definition of the two functions, namely swap1 and swap2, for exchanging a, b values.

2, experiment code

#include<stdio.h>
void swap1(int x,int y);
void swap2(int *x,int *y);
int main ()
{
	int a,b;
	printf("please input a=:");
	scanf("%d",&a);
	printf("\t     b=:");
	scanf("%d",&b);
	swap1(a,b);
	printf("\nAfter call swap1: a=%d b=%d\n"e,a,b);
	swap2(&a,&b);
	printf("\nAfter call swap2: a=%d b=%d\n",a,b);
	return 0;	
}
   void swap1(int x,int y)
   {
   	int temp;
   	temp=x;
   	x = y;
   	y=temp;
   }
    void swap2(int *x,int *y)
    {
    int temp;
   	*&temp=*x;
   	* X = * y;
   	*y=*&temp;	
	}

  

operation result

 

 3. Analysis: This program should pay attention to parameter changes will not affect the change in the argument, but when the address exchanged, it will become a value, no other problems now.

 

Third experiment, 8.3.3 connected in reverse text string

1, the problem described in
the definition of both a character pointer, a character string input by both get () function. Defines a function char Reverse (char STR), by way of the string pointer movement reversed. Then define a function char Link (char str1, char * str2), by a pointer movably connects the two strings. Function calls from the main function, respectively above, the input character marrsort (int a [], int n);

2, experiment code

#include<stdio.h>
char *reverse(char *str);
char *link(char *str1,char *str2);
int main ()
{
	char str[30],str1[30],*str2;
	printf("Input reversing character string:");
	gets(str);
	str2=reverse(str);
	printf("\nOutout reversed character string:");
	puts(str2);
	printf("Input string1:");
	gets(str);
	printf("Input string2:");
	gets(str1);
	str2=link(str,str1);
	printf("\nLink string1 and stringf:");
	puts(str2);
	return 0;
}
char *reverse(char *str)
{
	char *p,*q,temp;
	p=str,q=str;
	while(*p!='\0')
	p++;
q--;
while(q<p)
{
	temp=*q;
	*q=*p;
	*p=temp;
	q++;
	p--;
}
	return str;	
}
char *link(char *str1,char *str2)
{
	
	char *p=str1,*q=str2;
	while(*p!='\0')
	p++;
	while(*q!='\0')
{

	*p=*q;
	p++;
	q++;
	}
char ch='\0';
return str1;		
}

operation result

3. Analysis: the beginning did not understand what was moving towards each other, so that only knows one variable to change, so that only moves in one direction, so the results will not come out, and later realized that the results will come out.

 

Experiment 4, the parity array elements arranged 8.3.4

1, the problem described in
the definition of a one-dimensional array of integers, any input element of the array containing odd and even, the definition of a function, the array element realized in the left odd, even-numbered arranged on the right. In the above-defined functions are not allowed to add new array, the above function calls from the main function, respectively, printout.

int main ()
{
	int a[N],i;
	printf ( "enter:");
	for(i=0;i<N;i++)
	scanf("%d",&a[i]);
	arrsort(a,N);
	printf ( "Output:");
	for(i=0;i<N;i++)
	printf("%d ",a[i]);
}
void arrsort(int a[],int n)
{
	int *p,*q,temp;
	p=a;
	q=a+n-1;
	while(p<q)
	{
		while(*p%2==1)
		p++;
		while(*q%2==0)
		q--;
		if(p>q)
		break;
		temp=*p;
		*p=*q;
		*q=temp;
		p++;
		q--;
		
		
	}
}

 

 

operation result

 

 

3. Analysis: This program lacks the problem, but to understand the algorithm of this program.

Third, test summary

Although this experiment is about pointer part, but this time the difficulty of the experiment is not large, perhaps this is just a little fur pointer fills this part must learn, only to learn the c language pointer to the use of Xpress smoothly.

Guess you like

Origin www.cnblogs.com/fj001024/p/11011575.html