指针学习笔记(上)

关于直接复制代码出错原因的解释:以下代码都是经过我编译通过的。如果出现问题,一可能是粘贴复制时出现错误,二可能是与你的编译器有冲突。


1.指针的定义

int i=2;
int *p=&i;//int *p;p=&i;

注:p的作用是一个地址,而*的作用是找到P这个地址,然后读取里面的值。

所以:当使用scanf函数的时候,就不需要&(取址运算符)


2.  指针与一维数组的关系

[]和*是一个作用的,定义数组的两个方法

int a[];
int *p=a;//int *p=&a[0];

P作为地址和a[0]的地址是一样的,所以a[i]=*(p++);p++=&a[i++];


3. 指针与二维数组的关系

二维数组的定义有点特殊,首先二维数组必须要又有一个确定的值。也就是说

Int a[][]={1,2,3,4,5,6};//该定义是不合法的。

Int a[][]={{1,2,3},{4,5,6}};//该定义是不合法的。

Int  a[2][]= {1,2,3,4,5,6};//该定义是不合法的。

Int  a[2][3]= {1,2,3,4,5,6};//该定义是合法的。

Int  a[][3]= {1,2,3,4,5,6};//该定义是合法的。

 

所以在定义二维数组作为函数参数的时候,必须要有一个确定的值

Void pri(int a[][3],int m,int n); //该定义是可行的。

实例代码:

#include "stdio.h"

void seedD(int a[][3],int m,int n);

int main()
{

	int s[][3]={1,2,4,2,3,4};

	seedD(s,2,3);

	return 0;
}
void seedD(int a[][3],int m,int n)
{
	for (int i=0;i<m;i++)
	{
		for (int j=0;j<n;j++)
		{
			printf("%d",a[i][j]);//此一步可以换为:a[i][j]==*(*(a+i)+j);
		}
		printf("\n");
	}

然而现实则需要的是事先不知道行数和列数(根据用户自己需求确定)

实例代码:

#include<stdio.h>
#include<malloc.h>
void main()
{
	int column,row;
	int **array;
	printf("输入行和列:");
	scanf("%d%d",&column,&row);
	array=(int **)malloc(sizeof(int *)*row);
	for(int i=0;i!=row;i++)
		array[i]=(int*)malloc(sizeof(int)*column);
	for(int j=0;j!=row;j++)
	{
		for(int k=0;k!=column;k++)
		{
			scanf("%d",&array[j][k]);
		}
	}
	for(int m=0;m<row;m++)
	{
		for(int n=0;n<column;n++)
		{
			printf("%d\n",array[m][n]);
		}
	}
	
}<span style="font-family: Arial, Helvetica, sans-serif;">	    </span>

4.  指针数组与数组指针

数组指针:数组中的元素指向某一数据类型的指针,也就是,通过该指针可以找到该数组中的每个元素的位置。

记法:char*array[4]={“zhangsan”,”lisi”,”wanger”,”mazi”}


实例代码:

#include<stdio.h>

void main()
{

	char *a[4]={"zhangsan","lisi","wanger","mazi"};

	for (int i=0;i<4;i++)
	{
		for (int j=0;j<8;j++)//二维数组的长度
		{
			printf("%c",*(*(a+i)+j));
		}
		printf("\n");
	}
	
}
//输出结果:
//zhangsan
//lisi
//wanger
//mazi

Type *p[number]=>p[][number];

具体操作类似


指针数组:指向具有某类元素的数组,也就是说通过指针可以找到该数组。

记法:char  (*array)[4];

实例代码:

#include<stdio.h>

void main()
{

	int s[][4]={1,2,3,4};
	int (*a)[4];
	a=s;
	for (int i=0;i<4;i++)//与下面的相似,可以写成函数,避免重复
	{
		printf("%d\n",(*a)[i]);
	}


	int ss[][4]={2,3,4,5};
	a=ss;
	for (int j=0;j<4;j++)
	{
		printf("%d\n",(*a)[j]);
<span style="white-space:pre">	</span>}
}

改变后的代码:

#include<stdio.h>
void p(int (*s)[4]);

void main()
{

	int s[][4]={1,2,3,4};
	int (*a)[4];
	a=s;
	p(a);

	int ss[][4]={2,3,4,5};
	a=ss;
	p(a);

}

void p(int (*s)[4])
{
	for (int i=0;i<4;i++)
	{
		printf("%d\n",(*s)[i]);
	}
}


5.  指针函数与函数指针

指针函数中主要包括两个方案:一个是函数参数是指针,另一个是函数返回一个指针。

当函数的参数为指针时,由上面的情况可以知道,实际上是参数可以数组的形式表达出来。

也就是说函数得参数为指针等价于函数的参数为数组或者单一值。

实例代码:

#include "stdio.h"
int seekMax(int *a,int b);//该参数为指针

int main()
{

	int a[]={1,2,3,4};
	int b=seekMax(a,4);
	printf("%d",b);

	return 0;

}

int seekMax(int a[],int b)//该参数为数组
{
	int temp;
	for (int i=0;i<b;i++)
	{
		if(a[0]<a[i])
		{
			temp=a[0];
			a[0]=a[i];
			a[i]=temp;
		}
	}
	return a[0];
}

#include "stdio.h"
int seekMax(int a[],int b);//该参数为数组

int main()
{

	int a[]={1,2,3,4};
	int b=seekMax(a,4);
	printf("%d",b);

	return 0;

}

int seekMax(int *a,int b)//该参数为指针
{
	int temp;
	for (int i=0;i<b;i++)
	{
		if(*a<*(a+i))
		{
			temp=*a;
			*a=*(a+i);
			*(a+i)=temp;
		}
	}
	return *a;
}

函数返回是一个指针,也就是说返回的是一个数组(或者单一值)形式的东西

Type *function(para,para…)

#include "stdio.h"
int *seekMax(int a[],int b);//该参数为数组

int main()
{

	int a[]={1,2,3,4};
	int b=*seekMax(a,4);
	printf("%d",b);

	return 0;

}

int *seekMax(int *a,int b)//该参数为指针
{
	int temp;
	for (int i=0;i<b;i++)
	{
		if(*a<*(a+i))
		{
			temp=*a;
			*a=*(a+i);
			*(a+i)=temp;
		}
	}
	return a;
}


再举一例:

#include "stdio.h"
char *getName(char *name);

void main()
{
	char *name="xiaosong";
	char *na=getName(name);
	for (int i=0;i<sizeof("xiaosong")/sizeof(char);i++)
	{
		printf("%c",*(na+i));
	}

}

char *getName(char *name)
{
	return name;
}


指针函数 (*)function(para,para);地址指向函数,所以可以用于替换其他函数

#include "stdio.h"
//Mimo four bad guy bully xiaosong

void A();
void B();
void C();
void D();

void (*E)(); 

int main()

{

	printf("肖松!");
	E=A;
	(*E)();

	E=B;
	(*E)();
	
	E=C;
	(*E)();

	E=D;
	(*E)();


	return 0;
}

void A()
{
	printf("A hit xiaosong...");
}

void B()
{
	printf("B hit xiaosong...");
}

void C()
{
	printf("C hit xiaosong...");
}

void D()
{
	printf(" D hit xiaosong...");
}

格式:

声明:(*function)(para,para…) //指针函数

替换:function=will substitude//函数名替换即可

使用:(*function)(para,para…)//使用函数指针


6.字符串的处理

C语言中字符串的处理需要利用到字符数组因为在C中没有String 类型。字符串的操作主要包括以下几个方面

a.遍历每个字符charAt

#include "stdio.h"
char charAt(char *str,int i);

int main()

{
	char *strs="Hello,world!";

	printf("%c",charAt(strs,3));
	
	return 0;
}
char charAt(char *str,int i)
{
	return *(str+i);
}

b.计算字符串的长度

#include "stdio.h"
int length(char *str);

int main()
{
	
	char *str="HelloWorld!";

	printf("%d",length(str));

	return 0;
}

int length(char *str)
{

	int len=0;

	while (*(str++)!='\0')//'\0'也可以替换成NULL
	{
		len++;
	}

	return len;
}

c.求某个字符在字符串的第一次出现的位置

#include "stdio.h"
int firstindexof(char *str,char ch);

int main()
{
	
	char *str="HelloWorld!";

	printf("%d",firstindexof(str,'o'));

	return 0;
}

int firstindexof(char *str,char ch)
{

	int len=0;
	char tests=*(str);
	while (tests!='\0' )
	{
		
		if(tests==ch) break;
		len++;
		tests=*(str++);
	}

	return len;
}

d.求某个字符在字符串的最后一次出现的位置

#include <stdio.h>

int lastIndexof(char *str,char ch);
int main()
{
	char *ch="Hello!";

	printf("%d",lastIndexof(ch,'l'));
	return 0;
}

int lastIndexof(char *str,char ch)
{
	//首先求出str的长度
	int i=0;

<span style="white-space:pre">	</span>//再次使用时准备
	char *str2=str;

	while(*str++!=NULL)
	{
		i++;
	}

	//利用str长度倒着循环

	for (int j = i-1;j >=0;j--)
	{

		if (str2[j]==ch)
		{
			return j;
		}
	}

	printf("can`t find..\n");

	return 0;
}

e.统计某个字符的个数

#include <stdio.h>
int countCharNumber(char *str,char ch);
int main()
{
	char *ch="Hello!"; 
	printf("%d",countCharNumber(ch,'l'));

	return 0;
}
int countCharNumber(char *str,char ch)
{
	int i=0;
	char temp=str[0];
	while(temp!=NULL)
	{
		if (temp==ch)
		{
			i++;
		}
		temp=*str++;
	}

	return i;

}


f.比较两个字符串的大小

#include <STDIO.H>

int compareStr(char *str1,char *str2);

int main()
{
	char *str1="Hello!";
	char *str2="World";

	printf("%d",compareStr(str1,str2));
	
	return 0;

}

int compareStr(char *str1,char *str2)
{
	int i=0,j=0;
	while(*str1++!=NULL)
	{
		i++;
	}

	while(*str2++!=NULL)
	{
		j++;
	}
	
	if (i>j)
	{
		return 1;
	}
	else if (i<j)
	{
		return -1;
	}

	return 0;

}


限于篇幅原因,就总结到这里啦。

下面的内容一部分是把字符串的处理补充完

包括:

一、连接两个字符串concatStr(char *str1,char *str2)

二、删除字符串的某个字符removeCharFrom(char* str,char ch);

三、从某个位置截取字符串subString(char *str,int i);

四、从某个字符开始截取字符串subString(cahr *str,char ch);

五、截取固定长度的字符串subString(char *str,int start,int end);

六、按照固定方式(空格,或者“_”等)分离单词splitStr(cahr *str,char ch);

七、字符串插入某个字符insertStr(char *str,int place,char ch);

等等。


然后就是下文的主要内容

A:指针与结构体

B:指针与文件

C:链表是实现

D:拓展与特别注意。
























  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值