When to use const char * and when to use const char []

前端 未结 6 1607
梦毁少年i
梦毁少年i 2020-12-04 08:39

I know they are different, I know how they are different and I read all questions I could find regarding char* vs char[]

But all those answ

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-04 09:12

    I was helped a lot by Ulrich Drepper's blog-entries a couple of years ago:

    so close but no cigar and more array fun

    The gist of the blog is that const char[] should be preferred but only as global or static variable.

    Using a pointer const char* has as disadvantages:

    1. An additional variable
    2. pointer is writable
    3. an extra indirection
    4. accessing the string through the pointer require 2 memory-loads

提交回复
热议问题