<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語言

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

            時間:2025-03-17 03:53:54 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);//其中n為要開辟空間的大小

              這句話其實相當于:

              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的用法簡介09-29

            C語言中的BYTE和char字符08-06

            C語言中的字符(char)09-24

            C語言的BYTE和char字符09-22

            C語言中static和extern的區別10-01

            c語言中bit的用法11-01

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

            C語言中assert用法09-22

            C語言中的assert用法10-10

            <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>
                      黄色视频在线观看