Stdc--07 字符串


HighLight:

1. Pointer

  1).  用法

  2).  指针参数

  3).  野指针 

  4).  指针返回值

  5).  指针加减整数

  6).  数组和指针

  7).  const指针和指针const

2. String

 


 

Pointer

1. Basic

  多字节的数据,将其首字节地址作为该数据的地址。将指针存放在一个变量中, 该变量叫指针变量。

    int *p = &a;
        -->  *号前面类型 为目标类型。 指针指向什么类型*号前面就写什么类型
        -->  a变量的首字节的地址存放在p中

  int *po = &i;    // po是int指针(存放int i的地址)
  po = &i;       // &取地址运算符,所以&i和po代表的都是i的地址(int指针型)
  *po = i;       // i和*po代表的都是i的值(int)

 

2. 用法

  1) 指针可以用于参数,传递变量的地址,就相当于可以在多个函数中操作相同的内存地址。

     pointer.c

1 #include <stdio.h>
  2 
  3 int* foo(void){
  4     static int a=100;
  5     return &a;
  6     }
  7 
  8 int* bar(void){
  9     static int b=200;
 10     return &b;
 11     }
 12 
 13 int add(int a, int b){
 14     int c=a+b;
 15     return c;
 16     }
 17 
 18 int main(){
 19     int a=10;
 20     printf("a's address is %p\n",&a);
 21 
 22     int* p1=&a;
 23     printf("p1 = %p\n",p1);
 24     printf("a = %d\n",*p1);
 25 
 26     ++*p1;
 27     printf("++*p1=%d\n",a);
 28 
 29     char* p2=&a;
 30     a=0x12345678;
 31     printf("%#x\n",*p2);
 32 
 33     int* p3 = NULL;
 34     printf("p3=%p\n",p3);
 35 //  *p3=0;      NOTE: 给空指针赋值 印发错误
 36     p3=foo();
 37     bar();
 38     printf("%d\n",*p3);  //野指针错误 
 39 
 40     p3=0;
 41     p3=NULL;
 42 //  *p3=1;      NOTE: 给空指针赋值印发错误
 43     int c= add(123,456);
 44     printf("C=%d\n",c);
 45     return 0;
 46     }

 

 2).  野指针

  指针在使用时需要小心野指针,就是地址没有分配上,比如:

  int *p1;                // 无确定地址,野指针(不要使用)
  int *p2 = &变量;         //变量的地址,可用指针

  指针用为NULL 比较安全, 任何用过的pointer都赋值成NULL    任何时候非NULL指针都可以用

 

3) 指针做返回值

  但不要返回自动变量(必然是局部的)的指针。因为自动变量在函数结束时,会把地址释放掉。

  1 #include <stdio.h>
  2 int* foo(void)
  3 {
  4     int a=100;
  5     int* p=&a;
  6     printf("address of a is:%p\naddress of p is:%p\n",&a, &p);
  7     return p;
  8     }
  9 
 10 void bar(void)
 11 {
 12     int arr[]={200,300,400,500};
 13     }
 14 
 15 int main(void)
 16 {
 17     int* p=foo();
 18     bar();
 19     printf("%d\n",*p);    //不要返回局部变量指针 !!!
 20     return 0;
 21     }

 

4). 指针支持加整数、减整数、指针的比较和相减,但运算的单位由指针的类型决定:
   指针做运算的时候其实是: p+1*sizeof(*p)

  1 #include <stdio.h>
  2 
  3 int main(){
  4     char* p1=0;
  5     printf("char: %d %d\n",p1,p1+1);
  6 
  7     short* p2=0;
  8     printf("short: %d %d\n",p2,p2+1);
  9 
 10     int* p3=0;
 11     printf("int: %d %d\n",p3,p3+1);
 12     return 0;
 13 }
 

  int型指针+1 = 地址+4
  char型指针+1 = 地址+1

  指针可以参与的计算规则如下
        指针 + 整数   指针 - 整数   指针 - 指针
  在进行指针加减整数计算时整数1代表  一个捆绑存储区的大小
  指针变量的类型和目标的类型不一定严格一样。

 

字符串

1. 字符串指针和字符数组区别

 8 int main()
  9 {
 10 
 11     char *str1="hello";        //core dump  属于字符串常量, 存储在只读区域 不能修改
 12     *str1='H';
 13     printf("%s\n",str1);
 14 
 15 
 16     char str2[]="hello";    //数组在内存栈上分配, 栈的属性可读可写
 17     *str2='H';
 18     printf("%s\n",str2);
 19 
 20     return 0;
 21 }
~      

 

2. strchr()

 

3. strtok()

函数strtok将字符串分解为一系列标记(token),标记就是一系列用分隔符(delimiting chracter,通常是空格或标点符号)分开的字符。注意,此的标记是由delim分割符分割的字符串

例如,在一行文本中,每个单词可以作为标记,空格是分隔符。

  需要多次调用strtok才能将字符串分解为标记(假设字符串中包含多个标记)。
    -->第一次调用strtok包含两个参数,即要标记化的字符串和包含用来分隔标记的字符的字符串(即分隔符):
    -->下列语句: tokenPtr = Strtok(string, " "): 将tokenPtr赋给string中第一个标记的指针。strtok的第二个参数””表示string中的标记用空格分开。

  函数strtok搜索string中不是分隔符(空格)的第一个字符,这是第一个标记的开头。然后函数寻找字符串中的下一个分隔符,将其换成null(, w,)字符,这是当前标记的终点。注意标记的开始于结束。

  函数strtok保存string中标记后面的下一个字符的指针,并返回当前标记的指针。


  后面再调用strtok时,第一个参数为NULL,继续将string标记化。NULL参数表示调用strtok继续从string中上次调用 strtok时保存的位置开始标记化。

  如果调用strtok时已经没有标记,则strtok返回NULL。注意strtok修改输入字符串,因此,如果调用strtok之后还要在程序中使用这个字符串,则应复制这个字 符串。

  1 #include <stdio.h>
  2 #include <string.h>
  3 
  4 int main()
  5 {
  6     char str[]="hello, world, itcast, good";
  7     char tmp[]="I am a student\n";
  8     char *p;
  9     char *dlim=" ";
 10 
 11     p=strtok(str,dlim);
 12     printf("%s\n", p);
 13 
 14     p=strtok(tmp,dlim);        //内部指针药保存在下一次药strtok的地址
 15     printf("%s\n",dlim);
 16 
 17     while((p=strtok(NULL, dlim))!=NULL)
 18     {
 19         printf("%s\n", p);
 20     }
 21 
 22 
 23     printf("str= %s\n",str);
 24     return 0;
 25 }

 

 

 

 

posted @ 2014-09-04 17:49  wg934  阅读(149)  评论(0)    收藏  举报