c++ - 程序中常量的定义
PHP中文网
PHP中文网 2017-04-17 13:54:08
0
4
559
#define SIZE 100;
const int size = 100;

他们有什么区别

    char b[4] ;
    char (*pb)[4] = &b;//正常

b 是一个指针常量 ,也可以说是一个整形常量,可不可以理解是第二种情况,可以取地址,但不能赋值

b在内存中,有没有自己的空间

PHP中文网
PHP中文网

认证0级讲师

reply all(4)
巴扎黑
  1. define macro is expanded during the preprocessing stage, const is expanded during compilation and runtime

  2. define does not have a type and does not perform security checks, const does have a type, and security checks will be performed during the compilation phase

  3. define occupies code space; const is a variable definition, which occupies data segment space and is more efficient

洪涛

define macro definition is just a simple text replacement in the preprocessing stage of the compiler. That is, when compilation officially starts, SIZE in your code will be replaced with 100. If there is a compilation error or a problem occurs during runtime, you can only know that there is a problem with 100, and SIZE is invisible to you, which is not conducive to debugging. Using constants will not have such problems.

Your code below may look awkward, but it’s fine. b is a variable allocated on the stack, which naturally occupies memory space.

迷茫

@老老哥不了Assignment uses lea
to open up 16 bytes on the stack, 8 for arrays, 4 for pa, and 4 unused

左手右手慢动作

I’m not very good at learning C language. Please correct me if I’m wrong^_^

char b[4] ;
char (*pb)[4] = &b;//正常

b is an array of 4B;
pb is an array pointer pointing to the memory space of b, so you can get the address of pb by reading the content of b. But pb+1 points to after the last byte of b.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template