本實驗旨在增強學生對指針數據類型的理解,熟悉指針的定義、經過指針間接訪問變量。數組
本實驗旨在增強學生對字符指針以及將指針做爲函數的返回類型的理解,並經過指針對字符串進行操做,一般來講,一個字符串在內存中是連續存放的,其開始地址爲指向該字符串的指針值,字符串均以‘\0'做爲結束字符。函數
(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的地址,並與上面的結果進行比較。spa
#include<stdio.h> main() { int *p,a,c=3; float *q,b; p=&a; q=&b; printf("please input the value of a,b:\n"); scanf("%d,%f",p,q); printf("result:\n"); printf(" %d,%lf\n",a,b); printf(" %d,%lf\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); return 0; }
問題:剛開始我獲得的結果以下所示:設計
解決方法:我發現仍是一個老問題,我輸入的數據與scanf中的格式不一樣。3d
(1)定義兩個函數,分別爲voidswap1(inta,intb)和voidswap2(int*a,int*b),用於交換a,b的值。
(2)從主函數中分別輸入兩個整型變量a、b。
(3)從主函數中分別調用上述兩個交換函數,並打印輸出交換後a、b的結果。指針
#include<stdio.h> void swap1(int x,int y); void swap2(int *x,int *y); int main() { int a,b; printf("please input\na=:"); scanf("%d",&a); printf("\nb=:"); scanf("%d",&b); swap1(a,b); printf("\nafter call swap1:a=%d b=%d\n",a,b); swap2(&a,&b); printf("\nafter call swap1: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; }
問題:得出錯誤結果以下:code
解決辦法:blog
將 void swap2(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;
},由於應該交換指針的值而不是指針。字符串
(1)定義兩個字符指針,經過gets()函數輸入兩個字符串。
(2)定義一個函數char *reverse(char *str),經過指針移動方式將字符串反轉。
(3)定義一個函數char *link(char *str1, char *str2),經過指針移動方式將兩個字符串鏈接起來。
(4)從主函數中分別調用上述函數,輸入字符串並打印輸出結果。
#include<stdio.h> char *reverse(char *str); char *link(char *str1,char *str2); int main() { char str[30],str1[30],*str2; printf("Input Reversed Character String:"); gets(str); str2=reverse(str); printf("\nOutput Reversed Character String:"); puts(str2); printf("\nInput String1:"); gets(str); printf("\nInput string2:"); gets(str1); str2=link(str,str1); printf("\nLink string 1 and string 2:"); 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++; } *p='\0'; return str1; }
問題:錯誤以下:
問題分析:
字符串始終沒法鏈接,我發現我在循環中只移動了p指針,而沒有移動q指針。
(1)定義一個整型一維數組,任意輸入數組的元素,其中包含奇數和偶數。
(2)定義一個函數,實現將數組元素奇數在左,偶數在右的排列。
(3)在上述定義的函數中,不容許再增長新的數組。
(4)從主函數中分別調用上述函數,打印輸出結果。
#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!=0) p++; while(*q%2==0) q--; if(p>q) break; temp=*p; *p=*q; *q=temp; p++; q--; } }
問題:我始終沒法輸出結果,結果發現個人代碼中scanf中加了空格。
收穫:
一、輸入的數據格式必定要和scanf中格式一致!!!!
二、對於交換指針,交換地址,交換值有了更深的理解。
三、輸出值有多中方法,能夠直接輸出,也能夠經過指針輸出。
四、判斷奇偶不是用2*n+1而是用*q%2==0.
五、字符串爲空用'\0'表示。
六、瞭解到了指針相向移動,q++表示向下,p--表示向上。
不足:
一、老是犯相同的錯誤。
二、寫代碼的過程當中思路是混亂的。
三、對函數的調用還有些不熟練。