码迷,mamicode.com
首页 > 编程语言 > 详细

最近笔记1:数组,指针,字符串

时间:2015-11-26 18:51:55      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:

#include <stdio.h>
size_t strlen(char const *string);
main()
{
  char *a="hello";
  int b;
  b=strlen(a);
  printf("%d\n",b);
}
size_t strlen(char const *string)
{
  int length;
  for(length=0;*string++!=\0; )
    length+=1;
  return length;
}

2. 字符查找

#include <stdio.h>
#include <string.h>
main()
{
char string[20]="Hello there,honey.";
char *ans;
int a;
ans=strchr(string,h);
*ans=W;
printf("%c\n",*ans);
}
3. 字符的大小写转换

#include <stdio.h>
#include <ctype.h>
main(){
char s[] = "aBcDeFgH12345;!#$";
int i;
printf("before toupper() : %s\n", s);
for(i = 0; i < sizeof(s); i++)
s[i] = toupper(s[i]);
printf("after toupper() : %s\n", s);
}

4. 字符复制

#include <stdio.h>
#include <string.h>
main()
{
char message[]="Original message";
strcpy(message,"hello Jim");
strcat(message,", how are you?");
printf("%s\n",message);
}

strcpy和memcpy主要有以下3方面的区别。
1、复制的内容不同。strcpy只能复制字符串,而memcpy可以复制任意内容,例如字符数组、整型、结构体、类等。
2、复制的方法不同。strcpy不需要指定长度,它遇到被复制字符的串结束符"\0"才结束,所以容易溢出。memcpy则是根据其第3个参数决定复制的长度。
3、用途不同。通常在复制字符串时用strcpy,而需要复制其他类型数据时则一般用memcpy

5.结构体

#include <stdio.h>
typedef struct{
int a;
short b[2];
} Ex2;
typedef struct EX{
int a;
char b[3];
Ex2 c;
struct EX *d;
} Ex;
main()
{
Ex x={10,"Hi",{5,{-1,25}},0};
Ex *px=&x;
int *pi;
pi=&px->a;
printf("%d %d\n",px->a,*pi); //10 10
printf("%ld\n",sizeof(Ex)); //24
printf("%ld\n",sizeof(pi)); //8
}


 

 

最近笔记1:数组,指针,字符串

标签:

原文地址:http://www.cnblogs.com/htmlphp/p/4998290.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!