7.28 指標 字串 字串庫函數

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   ar   for   2014   div   

驗證大小端儲存 :
  1. int main()
  2. {
  3. int a = 0x12345678;
  4. char *p = (char *)&a;
  5. printf("%x\n", *p); // 78 表示這是小端儲存
  6. return 0;
  7. }

字元數組:
char name[] = { ‘h‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘ };//錯誤的 因為後面沒有\0char name[] = "liuwei";
char name[100] = {‘h‘,‘e‘,‘l‘,‘l‘,‘o‘};char name[100] = { "hello" };char name[100] = "hello";

字串也可以這樣:char *p = "hello world";printf("%s",p); //hello world   p指向字串首地址,但是一直取到 \0 為止
和字元數組的區別:
char *str = "hello";         //str只有四個位元組,儲存的只是hello的首地址,而hello存在 代碼區*str = ‘H‘;                        //這種是錯誤的
char str[] = "hello";  // hello存在棧區*str = ‘H‘;                   //輸入Hello

擷取一個字串的長度,自己實現strlen:                                                             
  1. int str_len(char * p)
  2. {
  3. int len = 0;
  4. while (*p++) // 相當於while(*p++ != ‘\0‘)
  5. len++;
  6. return len;
  7. }
  8. int main()
  9. {
  10. char a[100];
  11. gets(a);
  12. printf("%d\n", strlen(a));
  13. return 0;
  14. }

strlen sizeof區別 ://strlen函數在 <string.h>中
char str[100];gets(str);                              //假設輸入 helloprintf("%d",strlen(str));        //5        字串有效長度printf("%d",sizeof(str));        //100    整個字串數組有多大
strcpy :char src[100];char dest[100];gets[src];strcpy(dest,src); //dest裡面就有了src的值了,都是char *類型                           //把src拷貝到dest  這個函數的傳回值也是 dest 
其他字串函數:strcmp    以字典序比較兩個字串大小strcat       兩個字元相串連
strchr (char * s,int c)    在字串s尋找c 如果有,返回c的地址,否則返回null                                          printf列印的話 是c後面的字串
  hello , e       // printf  --->   ello


strstr (char *,char * )         在字串中找子串,找到返回位置的地址,否則返回null                                            //和strchr極其相似  只是字元和字串的區別

strtok(char * str,char * delim)    :
  1. #include <stdio.h>
  2. #include <string.h>
  3. int main()
  4. {
  5. char str[] = "hello world ni hao";
  6. char * p = strtok(str," ");
  7. while( p != NULL)
  8. {
  9. printf("%s\n",p);
  10. p = strtok(NULL , " ");
  11. }
  12. return 0;
  13. }
 
另外一種寫法
  1. for(p = strtok( buf , " "); p != NULL ; p = strtok(NULL , ""))
  2. {
  3.     printf("%s\n",p);
  4. }


memset ://函數在 <string.h>中
  1. #include <stdio.h>
  2. #include <string.h>
  3. int main()
  4. {
  5. char s[3];
  6. memset(s,‘A‘,sizeof(s)); //會把s指標前sizeof(s)個位元組以參數‘A‘填入
  7. s[3] = ‘\0‘;
  8. printf("%s\n",s); //AAA
  9. return 0;
  10. }


來自為知筆記(Wiz)

7.28 指標 字串 字串庫函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.