<delect id="sj01t"></delect>
  1. <em id="sj01t"><label id="sj01t"></label></em>
  2. <div id="sj01t"></div>
    1. <em id="sj01t"></em>

            <div id="sj01t"></div>

            C語言中char*和char[]用法區別

            時間:2025-11-17 22:22:36 C語言

            C語言中char*和char[]用法區別

              C語言中char*和char[]用法區別,一般來說,很多人會覺得這兩個定義效果一樣,其實差別很大。下面小編給大家介紹C語言中char*和char[]用法區別,歡迎閱讀!

              C語言中char*和char[]用法區別

              本質上來說,char *s定義了一個char型的指針,它只知道所指向的內存單元,并不知道這個內存單元有多大,所以:

              當char *s = "hello";后,不能使用s[0]='a';語句進行賦值。這是將提示內存不能為"written"。

              當用char s[]="hello";后,完全可以使用s[0]='a';進行賦值,這是常規的數組操作。

              若定義:

              char s[] = "hello";

              char *p = s;

              也可以使用p[0] = 'a';因為這是p ==s,都是指向數組的指針。

              下面看另外一種定義:

              char *s = (char *)malloc(n);/pic/p>

              這句話其實相當于:

              char s[n];

              定義的`也是一個指向數組的指針,便可進行數組的下標操作

              例子

              #include

              int main(int argc, char* argv[]) {

              char* buf1 = "this is a test";

              char buf2[] = "this is a test";

              printf("size of buf1: %d ", sizeof(buf1));

              printf("size of buf2: %d ", sizeof(buf2));

              return 0;

              }

              結果是:

              $ > ./main

              size of buf1: 4

              size of buf2: 15


            【C語言中char*和char[]用法區別】相關文章:

            c語言中char的用法簡介11-29

            C語言中的字符(char)01-04

            C語言中的BYTE和char字符02-19

            C語言的BYTE和char字符01-26

            C語言中static和extern的區別02-27

            c語言中bit和sbit的區別哪些10-27

            c語言中%s的用法01-28

            c語言中多個if的用法02-16

            c語言中邏輯或的用法12-15

            <delect id="sj01t"></delect>
            1. <em id="sj01t"><label id="sj01t"></label></em>
            2. <div id="sj01t"></div>
              1. <em id="sj01t"></em>

                      <div id="sj01t"></div>
                      黄色视频在线观看