initialization - cppreference

Upload: ioakeimtziakos

Post on 22-Feb-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/24/2019 Initialization - Cppreference

    1/2

    InitializationA declaraton of an object may provide its initial value through the process known as initialization.

    For each declarator, the initializer, if not omitted, may be one of the following:

    =expression (1)

    ={ initializer-list} (2)

    where initializer-listis a non-empty comma-separated list of initializers (with an optional trailing comma),where each initializer has one of three possible forms:

    expression (1)

    { initializer-list} (2)

    designator-list=initializer (3)

    where designator-listis a list of either array designators of the form [constant-expression]or

    struct/union member designators of the form .identifier; see array initialization and struct initialization.

    Explanation

    The initializer s pecifies the initial value stored in an object.

    Explicit initialization

    If an initializer is provided, see

    scalar initialization for the initialization of s calar types

    array initialization for the initialization of array types

    struct initialization for the initialization of struct and union types.

    Implicit initialization

    If an initializer is not provided:

    objects with automatic storage duration are initialized to indeterminate values (which may be traprepresentations)

    objects with static and thread-local storage duration are initialized as follows

    pointers are initialized to null pointer values of their types

    objects of integral types are initialized to unsigned zero

    objects of floating types are initialized to pos itive zero

    members of arrays, structs, and unions are initialized as described above, recursively, plus

    all padding bits are initialized to zero

    (on platforms where null pointers and floating zeroes have all-bit-zero representations, this form ofinitialization for statics is normally implemented by allocating them in the .bss section of theprogram image)

    Notes

    When initializing an object of s tatic or thread-local storage duration, every expressionin the initializermust be a constant expression or string literal.

    Initializers cannot be used in declarations of objects of incomplete type, VLAs, and block-scope objectswith linkage.

    The initial values of function parameters are es tablished as if by assignment from the arguments of afunction call, rather than by initialization (until the post-C11 defect report DR 427, which changes thewording to use initialization).

    If an indeterminate value is used as an argument to any standard library call, the behavior is undefined.Otherwise, the result of any expression involving indeterminate values is an indeterminate value (e.g. intn; may not compare equal to itself and it may appear to change its value on subsequent reads)

    http://en.cppreference.com/w/c/language/operator_other#Function_callhttp://en.cppreference.com/w/c/language/constant_expressionhttp://en.cppreference.com/w/c/language/constant_expressionhttp://en.cppreference.com/w/c/language/constant_expressionhttp://en.cppreference.com/w/c/language/constant_expressionhttp://en.cppreference.com/w/c/language/objecthttp://en.cppreference.com/w/c/language/objecthttp://en.cppreference.com/w/c/language/objecthttp://en.cppreference.com/w/c/language/objecthttp://en.cppreference.com/w/c/language/objecthttp://en.cppreference.com/w/c/language/struct_initializationhttp://en.cppreference.com/w/c/language/scalar_initializationhttp://en.cppreference.com/w/c/language/declarationshttp://en.cppreference.com/w/c/language/operator_other#Function_callhttp://en.cppreference.com/w/c/language/string_literalhttp://en.cppreference.com/w/c/language/constant_expressionhttp://en.cppreference.com/w/c/language/storage_durationhttp://en.cppreference.com/w/c/language/storage_durationhttp://en.cppreference.com/w/c/language/objecthttp://en.cppreference.com/w/c/language/storage_durationhttp://en.cppreference.com/w/c/language/struct_initializationhttp://en.cppreference.com/w/c/language/array_initializationhttp://en.cppreference.com/w/c/language/scalar_initializationhttp://en.cppreference.com/w/c/language/struct_initializationhttp://en.cppreference.com/w/c/language/array_initializationhttp://en.cppreference.com/w/c/language/declarationshttp://en.cppreference.com/w/c/language/declarations
  • 7/24/2019 Initialization - Cppreference

    2/2

    Example

    #include inta[2];// initializes a to {0, 0}intmain(void){ inti; // initializes i to an indeterminate value

    staticintj; // initializes j to 0 intk =1; // initializes k to 1

    // initializes int x[3] to 1,3,5 // initializes int* p to &x[0] intx[]={1, 3, 5}, *p =x;

    // initializes w (an array of two structs) to // { { {1,0,0}, 0}, { {2,0,0}, 0} } struct{inta[3], b;}w[]={[0].a ={1}, [1].a[0]=2};

    // function call expression can be used for a local variable char*ptr =malloc(10); free(ptr);

    // Error: objects with static storage duration require constant initializers// static char* ptr = malloc(10);// Error: VLA cannot be initialized// int vla[n] = {0};}

    References

    C11 standard (ISO/IEC 9899:2011):

    6.7.9 Initialization (p: 139-144)

    C99 standard (ISO/IEC 9899:1999):

    6.7.8 Initialization (p: 125-130)

    C89/C90 standard (ISO/IEC 9899:1990):

    3.5.7 Initialization

    See also

    C++ documentationfor Initialization

    Retrieved from "http://en.cppreference.com/mwiki/index.php?title=c /language/initializ ation&oldid=83077"

    Run this code

    http://en.cppreference.com/mwiki/index.php?title=c/language/initialization&oldid=83077http://en.cppreference.com/w/cpp/language/initializationhttp://en.cppreference.com/w/c/memory/freehttp://en.cppreference.com/w/c/memory/malloc