指针与字符数组

字符型数组 


char s[] = "hello";

//空间首地址 --- 数组名 
s --->怎么能保存s所代表的地址值 
     //s数组名 --- 数组首元素的地址 &s[0]
   --->地址的类型 
       char * 
       
char *p = s; //指针变量p 指向了 s (数组)

注意:
 char s[] = "hello"; //s的空间开在 栈上 (堆,栈,字符串常量区,全局区(静态区),代码区)
   char *s1 = "hello"; //s1的空间开在栈上,但是s1指向的"字符串常量"
                       //存放"字符串常量区"
                       
                       
  *s1 = *s1 - 32; //不能做 ? 原因是 s1 指向的数据是在 字符串常量区
                  //常量区的数据不能修改 

const应用:


const int a = 10; //a成了只读变量 
  
  const char * s1 = "hello";   //表示 将 *s1 限定为只读
                               //如果,不希望修改 *s1 的数据
                               //一般 建议加上const 

  char const *s1 = "hello"; //此时 还是 *s1 不能被修改

也就是说,const char * s1与 char const *s1的效果是一样的

 char * const s1 = "hello";  //const 离谁近,就是限定谁的  
  int *p; //int 代表基类型 
          //p 本身   
  char const * const s1 = "hello"; // *s1 和 s1 本身都不能被修改 

const总结


  1.const 遵循就近原则,离谁近,就限定谁 (离类型近,值不能改,离变量名(指针)近,地址不能修改)
  2.const 修饰的变量,为只读变量 //变量还是变量 只是成了只读的 

  3.这里说明,char *a=“hello”; //此处hello存于字符串常量区

                        char ch[ ]=“hello”; const char *a=&ch;   //此处a的值存于栈中,但是不可读

    且char *a=“hello”,char *b=“hello”,不论几个,字符串常量区只有一个hello,a,b均指向它.
 

总结:

 
   1.指针操作一维字符型数组 //字符串
   2.知识点
     //1.指针变量的 类型 是怎么确定的?
     //2.一维字符型数组 字符串 
     //3.处理字符串数据方式
        char s[] = "hello";
        const char *p = "hello"; 
    //4. const 关键字  
         const 关键字使用方法 
         const 形参          
   3.实现字符串相关函数
     gets 
     puts  //string  --- stdio 
     
     strlen 
     strcpy /strncpy
     strcat /strncat 
     strcmp /strncmp
     strcpy() //拷贝的是两个整型数组
     
     int a[5] = {1,2,3,4,5};
     int b[5];

void *memcpy(void *dest, const void *src, size_t n)
 {
      //一个字节一个字节拷贝 
 }
 void * //NULL 空指针 
        //空类型的指针 --- 万能指针
       //可以接收任意类型的指针  
注意:
  1.注意,如果用 空类型指针 进行 间接运算 
          必须 转换成 有明确类型的指针 

练习部分:

#include <stdio.h>
#include <string.h>

int Puts(const char *s)
{
	if (s == NULL)
	{
		return -1;
	}

	while (*s != '\0')
	{
		putchar(*s++);
	}
	putchar('\n');

	return 0;
}

char * Gets(char *s)
{
	char *ret = s;

	while ((*s = getchar())!='\n')
	{
		++s;
	}
	*s = '\0';

	return ret;
}

//      s
//hello'\0'
size_t Strlen(const char *s)
{
	const char *ret = s;
	
	while (*s!='\0')
		++s;

	return s-ret; 
}

char * Strcpy(char *dest,const char *src)
{
	char *ret = dest;

	while (*src != '\0')
	{
		*dest = *src;
		++dest;
		++src;
	}

	*dest = '\0';

	return ret;
}

char * Strncpy(char *dest,const char *src,size_t n)
{
	char *ret = dest;

	while ( n!=0 &&*src != '\0')
	{
		*dest = *src;
		++dest;
		++src;
		--n;
	}

	while(n)
	{
		*dest = '\0';
		++dest;
		--n;
	}

	return ret;
}

char * Strcat(char *dest,const char *src)
{
	char *ret = dest;

	while (*dest != '\0')
		++dest;

	while (*src != '\0')
	{
		*dest = *src;
		++dest;
		++src;
	}

	*dest = '\0';

	return ret;
}

char * Strncat(char *dest,const char *src,int n)
{
	char *ret = dest;

	while (*dest != '\0')
		++dest;

	while ( n && *src != '\0')
	{
		*dest = *src;
		++dest;
		++src;
		--n;
	}

	*dest = '\0';

	return ret;
}

int Strcmp(const char *s1,const char *s2)
{
	while (*s1==*s2 && *s1!='\0' && *s2!='\0')
	{
		++s1;
		++s2;
	}

	return *s1 - *s2;
}

int Strncmp(const char *s1,const char *s2,size_t n)
{
	//n = 3 s1 s2 
	//n = 2 s1+1 s2+1
	//n = 1 s1+2 s2+2 
	//      s1+3 s2+3 //n = 0 
	while ( n > 1&&*s1==*s2 && *s1!='\0' && *s2!='\0')
	{
		++s1;
		++s2;
		--n;
	}

	return *s1 - *s2;
}
int main(void)
{
	char s[20] = {'h','e','l','l','o','\0',1,1,1,1,1,1,1,1,1,1,1,1};
	char s1[20];
	//Gets(s);

	//Strcpy(s1,s);
	//Puts(s1);
	//printf("len = %ld\n",Strlen(s));
	
	//strncpy(s,"hello",3);
	//Strncpy(s,"hello",3);
	
	char s2[20];
	Gets(s1);
	Gets(s2);
	//printf("%d\n",Strcmp(s1,s2));
	printf("%d\n",Strncmp(s1,s2,3));
	//Strncat(s,s1,3);
#if 0

	printf("s = %s\n",s);


	int i = 0;
	for (i = 0; i < 20; ++i)
	{
		printf("s[%d] %c:%d\n",i,s[i],s[i]);
	}
#endif 

	return 0;
}

  • 21
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值