3.1 数组

比较大的数组尽量声明在main之外,否则程序有可能无法运行。

原因:
全局变量在静态存储区分配内存,局部变量是在栈上分配内存空间的。
c语言程序在运行时会动态创建一个堆栈段,里面存放着调用栈,保存着函数的调用关系和局部变量。如果数组太大,可能会造成栈溢出。


void * memcpy ( void * destination, const void * source, size_t num )
功能:copy block of memory (将以source开头后的num个字节的内容,拷贝到以destination开头的块中)
头文件:string.h
The function does not check for any terminating null character in source - it always copies exactly num bytes. (对source不进行null检查)


void * memset ( void * ptr, int value, size_t num )
功能:Fill block of memory (从ptr指向的地址直到num个字节后,全部用value进行填充)
头文件:string.h


int sprintf ( char * str, const char * format, … )
功能:将数据写入至字符串(字符数组)str 中。
头文件:stdio.h
必须保证字符串的容量能够容纳

char temp[20];
sprintf(temp,"%d",123);
printf("%c",temp[1]);

在sprintf中,123用%d接收,但是在存入temp后,temp的元素为字符型,temp[0]=’1’,temp[1]=’2’,temp[2]=’3’


size_t strlen ( const char* str )
功能:求字符串的实际长度
头文件:string.h
在计算长度


const char * strchr ( const char * str, int character );
功能:在str中找到character第一次出现的位置
返回值:返回指向character的指针,如果没找到,返回NULL;

/*转自CPlusPlus*/
/* strchr example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] = "This is a sample string";
  char * pch;
  printf ("Looking for the 's' character in \"%s\"...\n",str);
  pch=strchr(str,'s');
  while (pch!=NULL)
  {
    printf ("found at %d\n",pch-str+1);
    pch=strchr(pch+1,'s');
  }
  return 0;
}


Output:
Looking for the 's' character in "This is a sample string"...
found at 4
found at 7
found at 11
found at 18

猜你喜欢

转载自blog.csdn.net/lansehuanyingyy/article/details/81749907
3.1