指標的編程藝術(二)

來源:互聯網
上載者:User

標籤:c   指標   malloc   字串   

指標的編程藝術(二)之指標與字串


指標變數大小為4個位元組,看一個例子:

#include<stdio.h>int main(){    double i = 10, j = 100;    double * p = &i;    double * q = &j;    printf("%p\n",p);    printf("%p\n",q);    return 0;}



發現兩個資料的地址距離是8,正好是double類型資料的大小,(地址的差距為指標變數所指向資料類型的大小)但是它不是指標類型變數的大小,其實看這個地址也可以發現它由8位十六進位資料群組成,正好需要4個位元組大小,沒錯 這就是指標變數的大小。


進入正題,指標與字串:

    char * p = "Apple iPod";
p指向第一個字元‘A’的地址,也就是該字串的首地址。

A

P

P

L

E

 

I

P

O

D

\0

注意:字串的末尾都有一個Null 字元作為字串的結束標誌,如果缺少它,將會出現錯誤。

遍曆該字串中的字元:

#include<stdio.h>#include<conio.h>int main(){    char * p = "Apple iPod";    printf("字串為:");    for(int i=0; *p!='\0'; i++)    {        printf("%c",*p);        p++;    }        printf("\n");    printf("%s",p);//直接將字串輸出    return 0;}

下面一個缺少結束符的例子:

#include<stdio.h>#include<stdlib.h>int main(){    char * str1 = "Apple iPod";    char str2[10] = "Apple iPod";    printf("%s",str1);    printf("%s",str2);    return 0;}
有的編譯器在編譯的時候會識別出來str2這個錯誤 ,但是有的識別不出來,輸出的結果是字串後面多了好多字元,類似於“燙”。

常用的字串庫函數

通常談到字串的操作,不外乎計算字串長度(string length)、字串拷貝(string copy)、字串串連(string concatenate)、字串比較(string compare)。

下面給出庫函數的使用和自己編寫代碼模仿庫函數。


計算字串的長度:strlen(str)

#include<stdio.h>#include<stdlib.h>#include<string.h>int stringlength(char * p);int main(){    /*使用庫函數*/    char * str = "Apple iPod";    printf("長度為:%d\n",strlen(str));    /*自己編寫*/    int length = 0;    length = stringlength(str);    printf("長度為:%d\n",length);    return 0;}int stringlength(char * p){    int s = 0;    while(*p!='\0')//直到找到結束符為止    {        s++;        p++;    }    return s;}

字串的拷貝:strcpy(str1,str2);作用:把str2中的字元逐一複製到str1中。

#include<stdio.h>#include<stdlib.h>#include<string.h>void stringcopy(char *p, char *q);int main(){    char *str1 = "apple";    char str2[10] = "";    strcpy(str2,str1);    printf("%s\n",str2);    stringcopy(str2,str1);    printf("%s\n",str2);    return 0;}void stringcopy(char *p, char *q){    while((*p=*q)!='\0')    {        p++;        q++;    }}

如果將
char str2[10] = "";
改為 char *str2將會出現錯誤,引文str2沒有指向任何記憶體空間,解決方案是給它分配記憶體。

t = (char *)malloc(10 * sizeof(char));

字串串連:strcat(str1,str2),作用是將str2字串串連到str1的尾端。

#include<stdio.h>#include<stdlib.h>#include<string.h>void stringcat(char *p, char *q);int main(){    char * str1 = "apple";    char * str2 = "orange";    char str3[50] = "i want to bay a";    strcat(str3,str1);    printf("%s\n",str3);    stringcat(str3,str2);    printf("%s\n",str3);    return 0;}void stringcat(char *p, char *q){    while(*p!='\0')    {        p++;    }    while((*p=*q)!='\0')    {        p++;        q++;    }}
千萬不要這樣:
strcat(str1,str2);//原因還是沒有分配記憶體空間

字串比較:strcmp(str1,str2),作用是將這兩個字串是否相等。

#include<stdio.h>#include<stdlib.h>#include<string.h>int stringcompare(char *p, char *q);int main(){    char *str1 = "abczx";    char *str2 = "abczc";    int value = strcmp(str1,str2);    if(value==0)    {        printf("str1和str2相等\n");    }    else if(value > 0)    {        printf("str1大於str2\n");    }    else    {        printf("str1小於str2\n");    }    value = stringcompare(str1,str2);    if(value==0)    {        printf("str1和str2相等\n");    }    else if(value > 0)    {        printf("str1大於str2\n");    }    else    {        printf("str1小於str2\n");    }    return 0;}int stringcompare(char *p, char *q){    for(; *p==*q; p++,q++)        if(*p=='\0')            return 0;    return *p-*q;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.