zoukankan      html  css  js  c++  java
  • C: const and static keywords

    原文:http://www.noxeos.com/2011/07/29/c-const-static-keywords/

    C: const and static keywords

    Ok, once and for all, I’ll try to clarify to meaning of the ‘const’ and ‘static’ keywords in C (it applies to Objective-C and C++ too).

    I’m just tired of questions about this on StackOverflow.

    Objective-C programmers usually don’t think about C code while coding. I personally think this is a big mistake (it can sometimes apply to C++ programmers too).

    Objective-C is just a layer over C. So in order to be a good Objective-C programmer, you HAVE to know at least a few things about the C language.

    I don’t know why C has such a bad reputation for Objective-C coders, especially on iOS. And it’s very surprising to see the lack of knowledge of some Objective-C developers.

    So once and for all:

    If you want to code in Objective-C, learn at least the following C topics:

    • Language keywords and their meanings
    • Pointers and pointer arithmetic
    • C standard library

    Those are the (very) basics.

    Objective-C is a very nice object-oriented language, with amazing runtime capabilities. That’s true.
    But it doesn’t mean you can bypass completely the C language.
    A lot of time, you’ll save a lot of processor time and memory, just by knowing a few things about the C language, rather than relying on the apparent simplicity of the Objective-C language.

    But that’s a different story. Now back on our keywords…

    const

    First of all, the ‘const’ keyword.

    Ok, it means ‘constant’… So:

    const int x = 42;

    declares a constant integer variable. It means it’s value can’t be modified. It’s value is initially assigned to 42.
    If you try to change its value later, the compiler will issue a warning, or an error, depending on your compiler settings.
    So the following statement is invalid:

    const int x = 42;
    x = 43;

    That’s pretty easy to understand.
    The problem comes with pointers.

    Let’s take a look at the following code:

    char * str = "hello, world";

    It declares a ‘char’ pointer. Ok… But then what about this:

    char * const str = "hello, world";

    or

    const char * str = "hello, world";

    Now read carefully.
    The first one declares a constant pointer to a char.

    It means the the characters of the string can be modified, but not the pointer value.
    So the variable ‘str’ cannot be assigned to another pointer.

    For instance, this is invalid:

    char * hello = "hello, universe";
    char * const str = "hello, world";
    str = hello;

    as your a modifying the pointer value (not the string value).

    This is valid:

    char * const str = strdup( "hello, world" );
    str[ 0 ] = 'a';

    The ‘str’ variable will then contain ‘hello, world’. Remember: the pointer can’t be modified, the value that is pointed can be.

    It’s the exact opposite with the following notation:

    const char * str = "hello, world";

    Here, you can assign the pointer to another variable, but you can’t change the value.

    The ‘const’ keyword is contextual, in a way, when using pointers. It can apply to the pointer itself, or to the value pointed.

    So, in order to resume:

    const int * x;

    A modifiable pointer to a constant integer.

    int * const x;

    A constant pointer to an modifiable integer.

    const int * const x;

    A constant pointer to a constant integer.

    static

    The static keyword can have two meanings.

    First of all, it can be declared inside a function.
    Let's take a look at this example:

    #include <stdio.h>

    void foo( void );
    void bar( void );

    void foo( void )
    {
    int x = 0;

    printf( "X - foo: %i ", x );

    x++;
    }

    void bar( void )
    {
    static int x = 0;

    printf( "X - bar: %i ", x );

    x++;
    }

    int main( void )
    {
    foo();
    foo();
    foo();
    bar();
    bar();
    bar();

    return 0;
    }

    The output will be:

    X - foo: 0
    X - foo: 0
    X - foo: 0
    X - bar: 0
    X - bar: 1
    X - bar: 2

    Because a simple local variable, as in the 'foo' function, only exists when the function is called. It's destroyed (to be simple) when the function exits.

    So for the 'foo' function, the variable is created each time the function is called, with a value of '0'. The value is printed, then incremented.
    The function then exit, and the variable is destroyed.

    But in the 'bar' function, the variable is declared as static. It means the value will persist across function calls.
    It's initialized the first time the function is called, but only at that time. Once it has been initialized, it just exist, so its value will be taken for the next function calls.

    Now the 'static' keyword as a completely different meaning when used in a variable declared outside of a function (in the global scope).

    It means that the variable will be «file scoped». In other words, the variable, which is global, will be accessible only from the scope of the file which declared it. It won't be accessible from other files.

    It's just a way to create global private variable.

    For instance, imagine a file called 'foo.c':

    int x = 42;
    static int y = 42;

    From a 'bar.c' file, you'll be able to access the 'x' symbol, if both files are linked together. But you won't be able to access the 'y' symbol, as it's decaled as 'static'.
    It means that the symbol for the 'y' variable won't be exported by the linker, when the symbol for the 'x' variable will be.

    In other words, you'll be able to access the 'y' global variable only from function declared in the 'foo.c' file. The 'x' variable will be also accessible from other files.

    Of course, the 'static' keyword can be combined with const.
    For instance:

    static const int * const y;

    A constant pointer to a constant integer, that will be accessible only from the file which declared it.

  • 相关阅读:
    Asp.Net Core 2.0 项目实战(11) 基于OnActionExecuting全局过滤器,页面操作权限过滤控制到按钮级 郑州
    NET项目反编译+VS解决方案整理流程 郑州
    iis 目录枚举文件枚举解决方案 郑州
    Asp.Net Core 2.0 项目实战(10) 基于cookie登录授权认证并实现前台会员、后台管理员同时登录 郑州
    SqlServer mssql 按月统计所有部门 郑州
    无法获得数据库 'model' 上的排他锁 解决方法 郑州
    Asp.Net Core 2.0 项目实战(5)Memcached踩坑,基于EnyimMemcachedCore整理MemcachedHelper帮助类。 郑州
    谈谈前端怎样布局切图,程序可以方便快捷添加程序 郑州
    各种UIColor颜色的设置
    iOS
  • 原文地址:https://www.cnblogs.com/jay-dong/p/3152472.html
Copyright © 2011-2022 走看看