嵌入式(c)指针

指针的概念

 

指针的算数运算 

 指针的关系运算

 指针的赋值运算

 -----------------------------------------------------------------------------------------------------------------------------------------

使用指针封装字符串函数

MYstrcpyM


#include<stdio.h>

char my_strcpy(char* strDest, const char* strSrc)

{

	char* strDestCopy = strDest;

	while ((*strDest++ = *strSrc++) != '\0');

	return *strDestCopy;
}
int main(void)
{
	char arr1[32] = "hello world";
	char arr2[32] = "nihao bejing";
	char arr3=my_strcpy(arr1, arr2);
	printf("%s\n", arr1);
	return 0;
}

 Mystrcmp

#include<stdio.h>
#pragma warning(disable:4996)
int MYstrncmp( const char* s1,const char* s2, size_t n)
{
	if(s1==NULL||s2==NULL)
	{
		return -1;
	}
	const char* p = s1;
	const char* q = s2;
	q = s2;
	while ((*p != '\0' || *q != '\0') && (n < 0 || n != 0))
	{
		n--;
		if (*p > *q)
		{
			return 1;
		}
		else if (*p < *q) 
		{
			return -1;
		}
		p++;
		q++;
	}
	return 0;
}
int main(void)
{
	char buf1[32] = "hello world";
	char buf2[32] = "hell";
	int ret=MYstrncmp(buf1, buf2, 4);
	printf("ret=%d", ret);
	return 0;
}

Mystrcat

#include<stdio.h>
char* MYstrncat(char* strdest, const char* strsrc, size_t n)
{
	char* strDestncat = strdest;
	if(*strdest==NULL||*strsrc==NULL)
	{
		return NULL;
	}
	char* p = strdest;const char *q = strsrc;
	while(*p!='\0')
	{
		p++;
	}
	while(*q!=0&&(n!=0||n<0))
	{
		n--;
		*p++ = *q++;
	}
	*p = '\0';
	return strDestncat;

}
int main(void)
{
	char buf1[32] = "hello world";
	char buf2[32] = "nihao beijing";
	MYstrncat(buf1,buf2,5);
	printf("buf1=%s", buf1);
	return 0;
}

 ---------------------------------------------------------------------------------------------------------------------

指针函数问题

判断子串

#include<stdio.h>
#include<string.h>
int IssubString(char* p, char* q)
{

	if (p == NULL || q == NULL) {
		return -1;
	}
	int len1 = strlen(p);
	int len2 = strlen(q);
	int ret = 0;
	for (int i = 0; i < len1 - len2 + 1; i++) {
		ret = strncmp(q, p, len2);
		if (ret == 0)
		{
			return 0;
		}
		p++;
	}
	return -1;
}
	int main(void)
	{
		char buf1[32] = { 0 };
		char buf2[32] = { 0 };
		printf("输入两个字符串:\n");
		scanf_s("%s%s", buf1, 32, buf2, 32);
		int ret = IssubString(buf1, buf1);
		if(ret!=0)
		{
			printf("buf2不是buf1的子串\n");
		}
		else
		{
			printf("buf2是buf1的子串\n");
		}
		return 0;
	}
	
	

 实现atoi

#include<stdio.h>
int Myatoi(const char* nptr)
{
     int n = 0;
     while (*nptr != '\0')
     {
       n = (n * 10) + *nptr-'0';
        nptr++;
     }
     return n;

}
int main()
{
    char str[] = "59216";
    int num;
    num = Myatoi(str);
    printf("%lld", num);
    return 0;
}

 实现字符逆序

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值