赞
踩
数组、指针、指针数组(2018.12.28练习)
-
- #include<stdio.h>
-
- int main()
- {
-
- int i;
- char name[20] ={1,2,3,4,5,6,7,8,9,0};//
-
- char *p = "akjflkasjdl";//
-
- char *arr[12] = {"qee","qe","re","qwer"} ;//指针数组、字符串数组
-
- //p = name;//指针地址指向name[0] 数组名=数组首地址
-
-
- printf("print value :%s\n",p); //指针字符串
-
-
- printf("print char is:%c\n",*(p+1));//指针"akjflkasjdl"字符串中单个字符'k'
-
-
- for(i = 0; i < 20;i++) //数组的遍历
- {
- printf("%-1d",name[i]);
-
- }
-
- printf("\n");
-
- for(i = 0;i<12;i++) //指针数组的遍历
- {
-
- printf("%-1s",*(arr+i));//指针数组中的元素
-
-
- }
-
- }
YouTube教学视频:
练习代码:
-
-
- ****************************************指*针*与*数*组:*************************************************************
- #include <stdio.h>
-
- void test(){
- int a[2];
-
- int *p;
- //p = a[0];
- p = a;
-
- *p = 10;
-
- printf("%d\n",a[0]);
- //p=a=a[0]=10=*p
- }
-
-
- void test1(){
- //数组的普通遍历
- int a[3] = {1,2,3};
- int i;
-
- for( i = 0;i<3;i++){
-
- printf("a[%d] = %d\n",i,a[i]);
-
- }
- }
- void test2(){
-
- //一维数组的初始化
- char a[3] = {1,2,3};
-
- //让指针指向第一个元素//a = a[0]
- char *p = a;
-
- int i;
-
- for(i = 0;i<3;i++){
- //在遍历的过程中p的值一直不变
- printf("a[%d] = %d\n",i,*(p+i));//p+1是p+数组数据类型的1 例如:char = 1byte int = 2byte
- }
- }
- void test3(){
-
- char a[3] = {1,2,3};//char 1;int 2;double 4;long ;float;
-
- char *p = a; //a = a[0]
-
- int i;
-
- for(i = 0;i<3;i++){
- //在遍历的过程中p的值改变
- printf("a[%d] = %d\n",i,*(p++));//
- }
- }
- void test4(){
- char a[3] = {1,2,3};//char 1;int 2;double 4;long ;float;
-
- //char *p = a; //a = a[0]
-
- int i;
-
- for(i = 0;i<3;i++){
- //!=printf("a[%d] = %d\n",i,*(a++))因为:a是数组的首地址,数组的首地址不能改变!
- printf("a[%d] = %d\n",i,*(a+i));
- }
- }
- int main(void)
- {
-
- test4();
-
- return 0;
- }
-
-
-
- *******************************字符串数组&指针数组******************************************************************
-
-
- #include<stdio.h>
-
- //字符串输入
- void test3()
- {
- char name[20]; //定义字符串数组
-
- printf("请输入姓名:\n");
-
- scanf("%s",name); //数组名name就是地址
- //'j' 'a' 'c' 'k' '\0'
-
- printf("%c\n",name[2]); //name[0] = 'j' name[2] = 'c'
-
- printf("你刚才输入的字符串是:%s\n",name);
-
-
-
- }
- //定义字符串数组
- void test2()
- {
-
- //指针字符串
- char *name = "jack";
- // int age[5];
-
- //指针数组(字符串数组)
- char *names[5] = { "jack","rose","jack"};
-
- //二维字符数组(字符串数组)
- char names2[2][10] = {"jack2","rose2"};
- }
-
- //定义字符串
- void test()
- {
- char a = 'i'; //单个字符
-
- char name[] = "hello "; //字符串变量
-
-
- char *name2 = "it"; //字符串常量
-
- char *name3 = "it";
-
- }
- int main()
- {
-
-
-
-
- return 0;
- }
-
- **************************************二维数组***********************************************************
- int main()
- {
-
- int i,j,a[3][5];
- int m,n;
-
- int array[2][3]={{4,2,5},{2,6,4}};
-
- int i2,j2;
-
- printf("input score\n");
-
- for(i=0;i<3;i++)
- {
-
- for(j=0;j<5;j++)
- {
-
- scanf("%d",&a[j][i]);
-
-
- }
- }
- printf(" stop input score\n");
- Sleep(1000);
-
-
-
- for(i2=0;i2<2;i2++)
-
- {
-
- for(j2=0;j2<3;j2++)
-
- printf("%d",array[i2][j2]);
-
- printf("\n");
-
- }
-
-
-
- return 0;
-
- }
-
-
-
- ****************************************************************************
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。