zoukankan      html  css  js  c++  java
  • 字符串与数组

    #define _CRT_SECURE_NO_WARNINGS
    #include<stdlib.h>
    #include<string.h>
    #include<stdio.h>
    
    void main()
    {
    	char a[] = "i am a student";  //默认 结尾,为字符串(字符数组表示)
    	char b[64];
    
    	printf("strlen(a)=%d,sizeof(a)=%d
    ", strlen(a), sizeof(a));   //str为字符串长度14  sizeof为数组长度15,包括
    	int i = 0;
    	for (i = 0; *(a + i) != ''; i++)  //  a[i] = *(a+i)  a代表数组首地址,i步长为1
    	{
    		*(b + i) = *(a + i);
    	}
    	*(b + i) = '';    //若不将最后一位设置为,则表示单纯的字符数组,不是字符串,不能用%s输出
    	printf("a:%s
    ", a);
    	printf("b:%s
    ", b);
    	system("pause");
    	return ;
    }
    

    void copystr(char * from,char * to)      //拷贝字符串函数
    {
    	int i;
    	//1.
    	//for (i = 0; *(from + i) != ''; i++)
    	//{
    	//	//to[i] = from[i];            //to[i] 和 *(to+i)一样
    	//	*(to + i) = *(from + i);
    	//}
    	//*(to + i) = '';
    
    	//2.
    	//for (; *from != '';)
    	//{
    	//	*to++ = *from++;  //*to=*from         to++;from++
    	//}
    	//*to = '';
    
    	//3
    	//while ((*to = *from) != '')
    	//{
    	//	to++;
    	//	from++;
    	//}
    
    	
    	while ((*to++ = *from++) != '')
    	{
    
    	}
    
    	return;
    }
    void main()
    
    {
    	char from[] = "i am a student";  //提前给from 和 to 分配内存
    	char to[64];
    	copystr(from, to);
    	printf("%s
    ",to);
    	system("pause");
    	return;
    }
    

      

  • 相关阅读:
    函数的有用信息,装饰器 day12
    函数名、闭包、装饰器 day11
    函数的动态参数与命名空间 day10
    函数 day9
    集合 day8
    文件操作 day8
    基础数据类型补充,及capy daty7
    day7 回顾
    编码补充 daty 6
    字典的增删改查 daty 5
  • 原文地址:https://www.cnblogs.com/sclu/p/11272144.html
Copyright © 2011-2022 走看看