国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 PHP開發(fā) Character arrays and character pointers

Character arrays and character pointers

Dec 12, 2016 pm 05:16 PM
string array

問題1:

字符數(shù)組名可以作為左值嗎?當然不行

比如?

char str[20] = {'h','e','l','l','o',' ','w','o','r','l','d'};

str++;

不可以這么干,因為字符數(shù)組名是一個常量指針,也就是是一個const char*

#include <stdio.h>

int main()
{
    char str[20] = {&#39;h&#39;,&#39;e&#39;,&#39;l&#39;,&#39;l&#39;,&#39;o&#39;,&#39; &#39;,&#39;w&#39;,&#39;o&#39;,&#39;r&#39;,&#39;l&#39;,&#39;d&#39;};
    printf("sizeof(str): %d\n",sizeof(str));
    printf("str: %s\n",str);
    str = str + 1;  //error
    return 0;
}

運行結(jié)果如下:

Character arrays and character pointers

當數(shù)組名為左值時,它的類型是字符數(shù)組;當數(shù)組名為右值時,它的數(shù)據(jù)類型是字符指針。

#include <stdio.h>
#include <string.h>
int main(void)
{
    char buf[10];
    char *p= “afdal”;
    buf = p;  //將一個char* 賦給一個char[10],類型不一樣,必然不成功
    printf(“the buf is:%s\n”,buf);
    return 0;
}

問題2:

字符數(shù)組如何進行初始化?

#include <stdio.h>

int main()
{
    char ptr[20] = "hello world";  //success
    char str[20] = {&#39;h&#39;,&#39;e&#39;,&#39;l&#39;,&#39;l&#39;,&#39;o&#39;,&#39; &#39;,&#39;w&#39;,&#39;o&#39;,&#39;r&#39;,&#39;l&#39;,&#39;d&#39;};  //success
    char ctr[20];
    ctr = "hello world";  // error: incompatible types when assigning to type ‘char[20]’ from type ‘char *’
    return 0;
}

在給字符數(shù)組初始化的時候,會自動在字符數(shù)組的結(jié)尾加上'\0'

#include <stdio.h>

int main()
{
    char str[20] = {&#39;h&#39;,&#39;e&#39;,&#39;l&#39;,&#39;l&#39;,&#39;o&#39;,&#39; &#39;,&#39;w&#39;,&#39;o&#39;,&#39;r&#39;,&#39;l&#39;,&#39;d&#39;};
    printf("sizeof(str): %d\n",sizeof(str));
    printf("str: %s\n",str);
    int i = 0;
    while(*(str + i) != &#39;\0&#39;)  //判斷是否加上&#39;\0&#39;
    {
        printf("%c\n",*( str + i++));
    }
    return 0;
}

運行結(jié)果如下:

Character arrays and character pointers

問題3:

字符數(shù)組越界訪問能編譯通過嗎?

字符數(shù)組越界訪問編譯可以通過,沒有報錯,這樣會出現(xiàn)很多的問題

#include <stdio.h>

int main()
{
    char str[12] = {&#39;h&#39;,&#39;e&#39;,&#39;l&#39;,&#39;l&#39;,&#39;o&#39;,&#39; &#39;,&#39;w&#39;,&#39;o&#39;,&#39;r&#39;,&#39;l&#39;,&#39;d&#39;};
    printf("%c\n",str[100]);
    return 0;
}

Character arrays and character pointers

打印為空

問題4:

字符數(shù)組和字符指針又有什么區(qū)別呢?

首先在內(nèi)存的中位置不同,字符數(shù)組保存的字符串存放在內(nèi)存的棧中,而字符指針指向的字符串保存在內(nèi)存的靜態(tài)存儲區(qū)中。

其次字符數(shù)組保存的字符串屬于字符串變量,可以被修改,而字符指針指向的字符串是屬于字符串常量,不能被修改。

#include <stdio.h>

int main()
{
    char str[12] = {&#39;h&#39;,&#39;e&#39;,&#39;l&#39;,&#39;l&#39;,&#39;o&#39;,&#39; &#39;,&#39;w&#39;,&#39;o&#39;,&#39;r&#39;,&#39;l&#39;,&#39;d&#39;};
    char* ptr = "hello world";
    str[0] = &#39;f&#39;;
    ptr[0] = &#39;f&#39;;  //將字符指針指向的字符串修改,將出現(xiàn)段錯誤,因為該內(nèi)存地址只讀,因為該字符串屬于常量
    return 0;
}

運行結(jié)果:

Character arrays and character pointers

段錯誤是指訪問的內(nèi)存超出了系統(tǒng)給這個程序所設(shè)定的內(nèi)存空間,例如訪問了不存在的內(nèi)存地址、訪問了系統(tǒng)保護的內(nèi)存地址、訪問了只讀的內(nèi)存地址等等情況。

#include <stdio.h>

int main()
{
    char* ptr = "hello world";
    ptr[11] = &#39;!&#39;;  //往常量字符串末尾添加字符,相當于連接兩個字符串,出錯
    ptr[12] = &#39;\0&#39;;
    return 0;
}

這樣也會出現(xiàn)段錯誤。

問題5:

字符指針是不是和字符數(shù)組名都是指針常量呢?

不是,字符指針,可以改變它的指向,也就是可以為左值??梢詫⒁粋€字符指針指向一個字符串常量后又指向另外一個字符串常量。字符指針在為初始化之前,他是一個未定義的值,將指向任何可能的地方,所以在使用字符指針時一定要注意初始化。

#include <stdio.h>

int main()
{
    char* ptr;
    printf("ptr: %c\n",*ptr);
    return 0;
}

運行結(jié)果:

編譯可以通過,但是ptr指向的內(nèi)存地址是任意的

當然也可以將一個字符指針指向一個字符數(shù)組。

#include <stdio.h>

int main()
{
    char str[12] = {&#39;h&#39;,&#39;e&#39;,&#39;l&#39;,&#39;l&#39;,&#39;o&#39;,&#39; &#39;,&#39;w&#39;,&#39;o&#39;,&#39;r&#39;,&#39;l&#39;,&#39;d&#39;};
    char* ptr = str;
    printf("ptr: %s\n",ptr);
    return 0;
}

運行結(jié)果:

Character arrays and character pointers

問題6:

如果一個字符指針指向一個堆中動態(tài)內(nèi)存。那么我們?nèi)绾纬跏蓟搩?nèi)存中的值?

#include <stdio.h>
#include <stdlib.h>

int main()
{ 
    char* str = (char*)malloc(sizeof(char)*20);
    printf("str:%p\n",str);     //str在堆中內(nèi)存首地址
    str = "hello world";
    printf("str:%p\n",str);    //str在靜態(tài)存儲區(qū)內(nèi)存首地址
    char* ptr = "I love you";
    printf("ptr:%p\n",ptr);    //str在靜態(tài)存儲區(qū)內(nèi)存首地址
    return 0;
}

運行結(jié)果如下:

Character arrays and character pointers

很明顯前后的地址不一樣,前一個指向堆中的內(nèi)存的地址,而后一個指向了靜態(tài)存儲區(qū)中的內(nèi)存的地址。我本以為我通過上述方式進行初始化str的時候,我可以將堆中內(nèi)存進行初始化,我發(fā)現(xiàn)我錯了,字符指針將被重新指向。但是我想如果我們將str聲明為const呢?那將會出現(xiàn)什么樣的結(jié)果呢?

#include <stdio.h>
#include <stdlib.h>

int main()
{ 
    const char* str = (char*)malloc(sizeof(char)*20);  //我以為const將修飾str,使得str不能再作為左值而出現(xiàn),我錯了,const修飾的是由str指向的字符串,
    printf("str:%p\n",str);                            //該字符串不能再被修改
    str = "hello world";
    printf("str:%p\n",str);  
    return 0;
}

運行結(jié)果:

Character arrays and character pointers

如果我們將上述的代碼做如下修改,程序還能編譯通過嗎?

#include <stdio.h>
#include <stdlib.h>

int main()
{ 
    const char* str = (char*)malloc(sizeof(char)*20);
    printf("str:%p\n",str);
    str[0] = &#39;h&#39;;    //這種賦值才是對str原來所指向的堆中的內(nèi)存的賦值,編譯不過,因為str指向的堆中的內(nèi)存是只讀的。
    str = "hello world";     
    printf("str:%p\n",str);
    return 0;
}

運行結(jié)果如下:

Character arrays and character pointers

上述就引發(fā)了我思考為什么const修飾的是str指向的字符串,而不是str本身呢?

原來,char* const str才是真正的修飾str本身,使得str為只讀的,不能再被修改,也就是重新指向其他內(nèi)存地址。而const char* str 和 char const* str 的意義是一致的,都是修飾的是*str。

#include <stdio.h>
#include <stdlib.h>

int main()
{ 
    char* const str = (char*)malloc(sizeof(char)*20);
    printf("str:%p\n",str);
    str[0] = &#39;h&#39;; 
    str = "hello world";      //變量str被重新指向,error,編譯不過
    printf("str:%p\n",str);
    return 0;
}

運行結(jié)果如下:

Character arrays and character pointers

那么我們又該如何初始化一個字符指針指向的內(nèi)存呢?

其實我們可以很清楚的分析,如果str是一個字符指針,也就是一個變量,變量是可以被重新賦值的,而每一個字符串本身是有一個地址的,str = “hello world”,必然就是改變了str的值,使得str保存著"hello world"的內(nèi)存首地址,而一旦你將str = "I love you",必然str將保存這"I love you"的內(nèi)存首地址,這都是毋庸置疑的。

#include <stdio.h>

int main()
{
    printf("hello world:%p\n","hello world");   //將打印出"hello world"的內(nèi)存地址
    printf("I love you:%p\n","I love you");    //將打印出"I love you"的內(nèi)存地址
    return 0; 
}

運行如下:

Character arrays and character pointers

下面我得回到這樣一個問題?

str 和 *str的區(qū)別,很明顯,str是一個指針,而*str是str指向的內(nèi)存的存放的值,既然我們想改變str指向內(nèi)存中的值,既*str的值,那么為何不將*str作為左值,來初始化str所指向內(nèi)存的值呢?而*str 不就是 str[0]嗎?所以很明顯了。上述問題:那么我們又該如何初始化一個字符指針指向的內(nèi)存呢?當然這個僅限于字符指針指向的是由mlloc分配的堆中的內(nèi)存以及字符數(shù)組指向的棧中的內(nèi)存。而字符指針如果指向字符常量,不可修改字符指針指向的內(nèi)存的值,因為字符常量是只讀的。

#include <stdio.h>
#include <stdlib.h>

int main()
{ 
    char* const str = (char*)malloc(sizeof(char)*20);
    printf("str:%p\n",str);
    str[0] = &#39;A&#39;;     //數(shù)組下標型賦值
    *(str + 1) = &#39;l&#39;;  //指針++型賦值
    str[2] = &#39;e&#39;,
    *(str + 3) = &#39;x&#39;;
    printf("str:%s\n",str);  
    printf("str:%p\n",str);
    return 0;
}

運行如下:

Character arrays and character pointers

#include <stdio.h>
#include <stdlib.h>
int main()
{ 
    char str[20]; 
    printf("str:%p\n",str);
    str[0] = &#39;A&#39;; 
    *(str + 1) = &#39;l&#39;;
    str[2] = &#39;e&#39;,
    *(str + 3) = &#39;x&#39;;
    //str[4] = &#39;\0&#39;;  //當我們沒有手動給字符串結(jié)尾附上&#39;\0&#39;
    printf("str:%s\n",str);
    printf("str:%p\n",str);
    return 0; 
}

運行如下:

Character arrays and character pointers

上述的運行結(jié)果說明上述程序并沒有自動給字符串結(jié)尾附上'\0',對于字符數(shù)組除非這樣賦值,str[20] = "hello world",不然你就得手動給字符串附上字符串結(jié)尾'\0'。

#include <stdio.h>
#include <stdlib.h>

int main()
{ 
    char str[20]; 
    printf("str:%p\n",str);
    str[0] = &#39;A&#39;; 
    *(str + 1) = &#39;l&#39;;
    str[2] = &#39;e&#39;,
    *(str + 3) = &#39;x&#39;;
    str[4] = &#39;\0&#39;;
    printf("str:%s\n",str);
    printf("str:%p\n",str);
    return 0; 
}   
~

運行結(jié)果:

Character arrays and character pointers

附上一段我關(guān)于memcpy()和memmove()的代碼的實現(xiàn)。

#include <stdio.h>
#include <stdlib.h>

void mem_copy(char* const dest, const char* const src)
{
    for(int i = 0; *(src + i) != &#39;\0&#39;; i++)
        *(dest + i) = *(src + i);
}

void mem_move(char* const dest, const char* const src)
{
    int i = 0;
    while(*(src + i) != &#39;\0&#39;)
        i++;
    for(; i != -1; i--)
        *(dest + i) = *(src + i);
}

int main()
{
    char* src = "hello world";
    char* const dest1 = (char*)malloc(sizeof(char) * 20);
    char dest2[20];
    mem_copy(dest1,src);
    mem_move(dest2,src);
    printf("dest1:%s\n",dest1);
    printf("dest2:%s\n",dest2);
    return 0;
}

當我們需要將內(nèi)存的中的某一地址段的內(nèi)容拷貝到內(nèi)存中另一地址段中,以上是將字符串常量區(qū)的內(nèi)容拷貝到堆中和棧中,我們可以看到我分別使用了malloc和字符數(shù)組,當然我們得考慮可能在拷貝的過程中會有地址段重疊的問題,重疊該怎么拷貝,解決方案很簡單,就是從尾向前拷貝,即可。

運行結(jié)果如下:

Character arrays and character pointers

以上是我對c語言中字符串操作的一些理解

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276
How to use split() function in oracle How to use split() function in oracle May 07, 2024 pm 01:06 PM

The SPLIT() function splits a string into an array by a specified delimiter, returning a string array where each element is a delimiter-separated portion of the original string. Usage includes: splitting a comma-separated list of values ??into an array, extracting filenames from paths, and splitting email addresses into usernames and domains.

How to sort strings in java How to sort strings in java Apr 02, 2024 am 02:18 AM

Ways to sort strings in Java: Use the Arrays.sort() method to sort an array of strings in ascending order. Use the Collections.sort() method to sort a list of strings in ascending order. Use the Comparator interface for custom sorting of strings.

How to sort Chinese characters in C language environment? How to sort Chinese characters in C language environment? Feb 18, 2024 pm 02:10 PM

How to implement Chinese character sorting function in C language programming software? In modern society, the Chinese character sorting function is one of the essential functions in many software. Whether in word processing software, search engines or database systems, Chinese characters need to be sorted to better display and process Chinese text data. In C language programming, how to implement the Chinese character sorting function? One method is briefly introduced below. First of all, in order to implement the Chinese character sorting function in C language, we need to use the string comparison function. Ran

What does args mean in java What does args mean in java Apr 25, 2024 pm 10:15 PM

args stands for command line arguments in Java and is an array of strings containing the list of arguments passed to the program when it is started. It is only available in the main method, and its default value is an empty array, with each parameter accessible by index. args is used to receive and process command line arguments to configure or provide input data when a program starts.

What does \0 mean in c language What does \0 mean in c language Apr 27, 2024 pm 10:54 PM

In C language, \0 is the end mark of a string, called the null character or terminator. Since strings are stored in memory as byte arrays, the compiler recognizes the end of the string via \0, ensuring that strings are handled correctly. \0 How it works: The compiler stops reading characters when it encounters \0, and subsequent characters are ignored. \0 itself does not occupy storage space. Benefits include reliable string handling, improved efficiency (no need to scan the entire array to find the end), and ease of comparison and manipulation.

Application of artificial intelligence technology in PHP functions Application of artificial intelligence technology in PHP functions May 01, 2024 pm 01:15 PM

AI technology has been combined with PHP functions to enhance the functionality of the application. Specific AI applications include: using machine learning algorithms to classify text, such as Naive Bayes. Perform in-depth text analysis using natural language processing techniques such as word segmentation and stemming.

What does args mean in java What does args mean in java May 07, 2024 am 02:24 AM

args is a special parameter array of the main method in Java, used to obtain a string array of command line parameters or external input. By accessing the args array, the program can read these arguments and process them as needed.

What impact do C++ functions have on program performance? What impact do C++ functions have on program performance? Apr 12, 2024 am 09:39 AM

The impact of functions on C++ program performance includes function call overhead, local variable and object allocation overhead: Function call overhead: including stack frame allocation, parameter transfer and control transfer, which has a significant impact on small functions. Local variable and object allocation overhead: A large number of local variable or object creation and destruction can cause stack overflow and performance degradation.

See all articles