Chapter VIII Pointer Pointer

Essay - 8  articles - 0  comments - 0

 

 

The eighth chapter pointer

 

C Programming lab report

experimental project:

<1> base pointer and the pointer arithmetic 8.3.1 <2> 8.3.2 Data Exchange <3> and reverse a string concatenation 8.3.3 <4> 8.3.4 parity array elements arrayed

Name: Jianqing Experimental Venue: Teaching Building 514 classroom experiment Time: 2019.6.12

1, experimental purposes and requirements

<1> pointer control method of concepts and definitions.

<2> master pointer and the pointer arithmetic operators.

<3> grasp the relationship between pointers and arrays.

<4> grasp the relationship pointer string.

<5> are familiar with the function pointer as a parameter and returns the pointer.

<6> learn function pointers.

Second, the experimental content

Lab Exercise 1: basic pointers and pointer arithmetic

1. brief description of the problem: (1) the definition of an integer variable pointer p, an integer variable to point to a, the definition of a floating-point pointer q, to ​​point to a floating point variable b, while further define a whole c variables and initial value 3. (2) use of a pointer variable, calls the function scanf input values ​​are a and b. (3) or indirectly via the pointer and output a, b values. (4) output by hexadecimal numbers p, q and the value of a, b of the address. (5) The point c p, c accessed indirectly by the value p and outputs. (6) the value of p and c output address, and compared with the above results.

2. Experimental Code:

Copy the code
/*8-1.c*/
# include <stdio.h>
int main() { int *p,a,c=3; float *q,b; p=&a; q=&b; printf("Please Input the value of a,b:"); scanf("%d%f",p,q);  printf("Result: \n"); printf(" %d, %f\n",a,b); printf(" %d, %f\n",*p,*q);   printf("The Address of a,b: %p,%p\n", &a, &b); printf("The Address of a,b:%p,%p\n", p, q);  p=&c; printf("c=%d\n ", *p) ; printf("The Address of c: %x, %x\n", p, &c); } 
Copy the code

 

3. Run Results: 4. Analysis: None

Lab Exercise 2: data exchange

1. brief description of the problem: (1) define two functions, namely voidswap1 (inta, intb) and voidswap2 (int a, int b), for exchanging a, b values. (2) from the main function input two integer variables a, b, respectively. (3) the main function call from each of the two switching functions, and print the results a, b of the output switch. 2. Experimental Code:

Copy the 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("\n b=:"); scanf("%d", &b); swap1(a,b); printf("\nAfter Call swap1: a=%d b=%d\n",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;  }
Copy the code

 

3. Run Results: 4. Analysis:

Just need to fill the empty fill a book, there is no problem

3, laboratory exercises (8.3.3):

1. brief description of the problem:

(1) defines two character pointers. (2) Define a function char * reverse (char * str), by reversing the string pointer moves. (3) defines a function char * link (char * str1, char * str2), the pointer moves by connecting two strings. (4) from the main function calls are the above function, the input string and printout.

2. Experimental Code:

Copy the code
#include<stdio.h>
#include<conio.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("\noutput reversed character string:"); puts(str2); printf("input string1:"); gets(str); printf("\ninput string2:"); gets(str1); str2=link(str,str1); puts(str2); return 0; } char *reverse(char *str) { char *p,*q,temp; p=str,q=str; while(*p!='\0') { p++; } p--; 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; q++; p++; } putch('\0'); return str1; }
Copy the code

3. Analysis:

1, the main problem: the biggest problem with this program is to understand how the algorithm to reverse a string output, that is, how to exchange the position of the character. So there is this exchange program must issue a function. Wherein while the first is in the interior of the p ++ while, judgment is carried out character by character, when the time out of the loop, is referred to as '\ 0', so the P-- a need, back \ 0 of the previous character, is the character you want to shift position. Then the second position while finishing is carried out exchange of letters, the position of the character cursor should be moved towards each other so as q ++, p--.

b, the program runs as follows:

4, laboratory exercises (8.3.4):

1. brief description of the problem:

(1) defines a one-dimensional array of integers, any input elements of the array, including the odd and even. (2) the definition of a function, the array element realized in the left odd, even-numbered arranged on the right. (3) the function defined above, is not allowed to increase in the new array. (4) function calls from the main function, respectively above, printout.

2. Experimental Code:

Copy the code
#include<stdio.h>
#define N 10
void arrsort(int a[],int n);
int main() { int a[N],i; for(i=0;i<N;i++) scanf("%d",&a[i]); arrsort(a,N); 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--; } }
Copy the code

3.问题分析:

a、主要的问题:这个程序我放了一个比较细节的错误,也就是我在第二个for循环语句后面加上了一个“ ;”,导致一直都不能输出数据,这个一个比较需要细节的地方,许多同学也在这个地方放过错误,而且该错误是系统不会报错的,所以比较细节,容易被忽略,其他地方基本没有什么问题。

b、程序运行如下:

三、实验小结

1、对于C语言指针来说,按值、按址传递的区别在于,在函数中使用的方式而已。如果在函数中是要修改地址所指向内存的值,则可以修改实参地址指向的内容。因为在内存中内存地址是唯一的,所以不论在什么地方,都可以通过地址来修改内存中的值

2.判断能否到达最后一个字符用p!=‘\0’

C程序设计实验报告

实验项目:

<1>8.3.1 指针基础及指针运算 <2>8.3.2 数据交换 <3>8.3.3 字符串反转及字符串连接 <4>8.3.4 数组元素奇偶排列

姓名:陈建清  实验地点:教学楼514教室  实验时间:2019.6.12

一、实验目的与要求

<1>掌握指针的概念和定义方法。

<2>掌握指针的操作符和指针的运算。

<3>掌握指针与数组的关系。

<4>掌握指针与字符串的关系。

<5>熟悉指针作为函数的参数以及返回指针的函数。

<6>了解函数指针。

二、实验内容

实验练习1: 指针基础及指针运算

1.问题的简单描述: (1)定义一个整型指针变量p,使它指向一个整型变量a,定义一个浮点型指针q,使它指向一个浮点型变量b,同时定义另外一个整型变量c并赋初值3。 (2)使用指针变量,调用scanf函数分别输入a和b的值。 (3)通过指针间接访问并输出a、b的值。 (4)按十六进制方式输出p、q的值以及a、b的地址。 (5)将p 指向c, 通过p 间接访问c的值并输出。 (6)输出p的值及c的地址,并与上面的结果进行比较。

2.实验代码:

Copy the code
/*8-1.c*/
# include <stdio.h>
int main() { int *p,a,c=3; float *q,b; p=&a; q=&b; printf("Please Input the value of a,b:"); scanf("%d%f",p,q);  printf("Result: \n"); printf(" %d, %f\n",a,b); printf(" %d, %f\n",*p,*q);   printf("The Address of a,b: %p,%p\n", &a, &b); printf("The Address of a,b:%p,%p\n", p, q);  p=&c; printf("c=%d\n ", *p) ; printf("The Address of c: %x, %x\n", p, &c); } 
Copy the code

 

3.运行结果: 4.问题分析: 无

实验练习2: 数据交换

1.问题的简单描述: (1)定义两个函数,分别为voidswap1(inta,intb)和voidswap2(inta,intb),用于交换a,b的值。 (2)从主函数中分别输入两个整型变量a、b。 (3)从主函数中分别调用上述两个交换函数,并打印输出交换后a、b的结果。 2.实验代码:

Copy the 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("\n b=:"); scanf("%d", &b); swap1(a,b); printf("\nAfter Call swap1: a=%d b=%d\n",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;  }
Copy the code

 

3.运行结果: 4.问题分析:

仅仅是填书上需要填的空,没有问题

3、实验练习(8.3.3):

1.问题的简单描述:

(1) 定义两个字符指针。 (2)定义一个函数char *reverse(char *str),通过指针移动方式将字符串反转。 (3)定义一个函数char *link(char*str1,char *str2),通过指针移动方式将两个字符串连接起来。 (4)从主函数中分别调用上述函数,输入字符串并打印输出结果。

2.实验代码:

Copy the code
#include<stdio.h>
#include<conio.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("\noutput reversed character string:"); puts(str2); printf("input string1:"); gets(str); printf("\ninput string2:"); gets(str1); str2=link(str,str1); puts(str2); return 0; } char *reverse(char *str) { char *p,*q,temp; p=str,q=str; while(*p!='\0') { p++; } p--; 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; q++; p++; } putch('\0'); return str1; }
Copy the code

3.问题分析:

1、主要的问题:该程序最大的问题就是理解该算法是怎样对一个字符串进行反向输出的,也就是怎样将字符的位置调换的。所以这个程序中必定存在一个函数的调换问题。其中第一个while中的p++是while内部的,是对逐个字符进行判断,当跳出循环的时候,也就是指  ‘ \0 ’,所以需要一个p--,回到\0的前一个字符,才是要调换位置的字符。接着的第二个while是对收尾字母的位置进行调换,所以字符指针的位置应该相向移动,故为q++,p--。

b、程序运行如下:

4、实验练习(8.3.4):

1.问题的简单描述:

(1) 定义一个整型一维数组,任意输入数组的元素,其中包括奇数和偶数。 (2)定义一个函数,实现将数组元素奇数在左,偶数在右的排列。 (3)在上述定义的函数中,不允许在增加新的数组。 (4)从主函数中分别调用上述函数,打印输出结果。

2.实验代码:

Copy the code
#include<stdio.h>
#define N 10
void arrsort(int a[],int n);
int main() { int a[N],i; for(i=0;i<N;i++) scanf("%d",&a[i]); arrsort(a,N); 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--; } }
Copy the code

3. Analysis:

a, the main problem: this program I put a comparison of the details of the error, that is, I added a loop behind in second for ";", resulting in the output data has not been able, this requires a relatively details of the place many students also miss this place error, and this error is the system not being given, it is more detailed, easily overlooked, elsewhere there is little problem.

b, the program runs as follows:

Third, test summary

1, for the C language pointer, the value according to the difference Anzhi transmitted that way to use it in the function. If the function is to modify the address value in the memory pointed to, it is possible to modify the contents of the address pointed to by the argument. Because the memory address in memory is unique, so no matter where you are, can be modified by the value of memory address

2. The judge can reach the last character by p! = '\ 0'

Guess you like

Origin www.cnblogs.com/danwang123/p/11032998.html