in most contexts in later use, a mention of the variable array is converted to a pointer to the...

Upload: vonaderty

Post on 30-May-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 In Most Contexts in Later Use, A Mention of the Variable Array is Converted to a Pointer to the First Item in the Arra

    1/23

    which defines an array named array to hold 100 values of the primitive type int. If declared within a

    function, the array dimension may also be a non-constant expression, in which case memory for the

    specified number of elements will be allocated. In most contexts in later use, a mention of the variable

    array is converted to a pointer to the first item in the array. The sizeof operator is an exception: sizeofarray yields the size of the entire array (that is, 100 times the size of an int). Another exception is the &

    (address-of) operator, which yields a pointer to the entire array (e.g. int (*ptr_to_array)[100] =

    &array;).[edit] Accessing elements

    The primary facility for accessing the values of the elements of an array is the array subscript operator.To access the i-indexed element of array, the syntax would be array[i], which refers to the value stored

    in that array element.

    Array subscript numbering begins at 0. The largest allowed array subscript is therefore equal to thenumber of elements in the array minus 1. To illustrate this, consider an array a declared as having 10

    elements; the first element would be a[0] and the last element would be a[9]. C provides no facility for

    automatic bounds checking for array usage. Though logically the last subscript in an array of 10elements would be 9, subscripts 10, 11, and so forth could accidentally be specified, with undefined

    results.

    Due to arraypointer interchangeability, the addresses of each of the array elements can be expressed

    in equivalent pointer arithmetic. The following table illustrates both methods for the existing array:

    Array subscripts vs. pointer arithmetic Element index 1 2 3 nArray subscript array[0] array[1] array[2] array[n-1]

    Dereferenced pointer *array *(array + 1) *(array + 2) *(array + n-1)

    Similarly, since the expression a[i] is semantically equivalent to *(a+i), which in turn is equivalent to*(i+a), the expression can also be written as i[a] (although this form is rarely used).

    [edit] Dynamic arrays

    A constant value is required for the dimension in a declaration of a static array. A desired feature is the

    ability to set the length of an array dynamically at run-time instead:

    int n = ...;

    int a[n];

    a[3] = 10;

  • 8/9/2019 In Most Contexts in Later Use, A Mention of the Variable Array is Converted to a Pointer to the First Item in the Arra

    2/23

    This behavior can be simulated with the help of the C standard library. The malloc function provides asimple method for allocating memory. It takes one parameter: the amount of memory to allocate in

    bytes. Upon successful allocation, malloc returns a generic (void *) pointer value, pointing to the

    beginning of the allocated space. The pointer value returned is converted to an appropriate typeimplicitly by assignment. If the allocation could not be completed, malloc returns a null pointer. The

    following segment is therefore similar in function to the above desired declaration:

    #include /* declares malloc */

    int *a;

    a = malloc(n * sizeof(int));a[3] = 10;

    The result is a "pointer to int" variable (a) that points to the first of n contiguous int objects; due toarraypointer equivalence this can be used in place of an actual array name, as shown in the last line.

    The advantage in using this dynamic allocation is that the amount of memory that is allocated to it can

    be limited to what is actually needed at run time, and this can be changed as needed (using the standardlibrary function realloc).

    When the dynamically-allocated memory is no longer needed, it should be released back to the run-

    time system. This is done with a call to the free function. It takes a single parameter: a pointer topreviously allocated memory. This is the value that was returned by a previous call to malloc. It is

  • 8/9/2019 In Most Contexts in Later Use, A Mention of the Variable Array is Converted to a Pointer to the First Item in the Arra

    3/23

    considered good practice to then set the pointer variable to NULL so that further attempts to access the

    memory to which it points will fail. If this is not done, the variable becomes a dangling pointer, and

    such errors in the code (or manipulations by an attacker) might be very hard to detect and lead to

    obscure and potentially dangerous malfunction caused by memory corruption.

    free(a);

    a = NULL;

    Standard C-99 also supports variable-length arrays (VLAs) within block scope. Such array variables

    are allocated based on the value of an integer value at runtime upon entry to a block, and aredeallocated at the end of the block.

    float read_and_process(int sz)

    {float vals[sz]; // VLA, size determined at runtime

    for (int i = 0; i < sz; i++)vals[i] = read_value();

    return process(vals, sz);

    }

    [edit] Multidimensional arrays

  • 8/9/2019 In Most Contexts in Later Use, A Mention of the Variable Array is Converted to a Pointer to the First Item in the Arra

    4/23

    In addition, C supports arrays of multiple dimensions, which are stored in row-major order.

    Technically, C multidimensional arrays are just one-dimensional arrays whose elements are arrays. The

    syntax for declaring multidimensional arrays is as follows:

    int array2d[ROWS][COLUMNS];

  • 8/9/2019 In Most Contexts in Later Use, A Mention of the Variable Array is Converted to a Pointer to the First Item in the Arra

    5/23

    (where ROWS and COLUMNS are constants); this defines a two-dimensional array. Reading the

    subscripts from left to right, array2d is an array of length ROWS, each element of which is an array of

    COLUMNS ints.

    To access an integer element in this multidimensional array, one would use

    array2d[4][3]

    Again, reading from left to right, this accesses the 5th row, 4th element in that row (array2d[4] is an

    array, which we are then subscripting with the [3] to access the fourth integer).

    Higher-dimensional arrays can be declared in a similar manner.

    A multidimensional array should not be confused with an array of references to arrays (also known as

    Iliffe vectors or sometimes array of arrays). The former is always rectangular (all subarrays must be the

    same size), and occupies a contiguous region of memory. The latter is a one-dimensional array ofpointers, each of which may point to the first element of a subarray in a different place in memory, and

    the sub-arrays do not have to be the same size. The latter can be created by multiple use of malloc.

    [edit] Strings

    In C, string literals (constants) are surrounded by double quotes ("), e.g. "Hello world!" and are

    compiled to an array of the specified char values with an additional null terminating character (0-

    valued) code to mark the end of the string.

    String literals may not contain embedded newlines; this proscription somewhat simplifies parsing of

    the language. To include a newline in a string, the backslash escape \n may be used, as below.

    There are several standard library functions for operating with string data (not necessarily constant)organized as array of char using this null-terminated format; see below.

    C's string-literal syntax has been very influential, and has made its way into many other languages,such as C++, Perl, Python, PHP, Java, Javascript, C#, Ruby. Nowadays, almost all new languages adoptor build upon C-style string syntax. Languages that lack this syntax tend to precede C.

    [edit] Backslash escapes

    If you wish to include a double quote inside the string, that can be done by escaping it with a backslash

    (\), for example, "This string contains \"double quotes\".". To insert a literal backslash, one must double

    it, e.g. "A backslash looks like this: \\".

  • 8/9/2019 In Most Contexts in Later Use, A Mention of the Variable Array is Converted to a Pointer to the First Item in the Arra

    6/23

    Backslashes may be used to enter control characters, etc., into a string:

    Escape Meaning

    \\ Literal backslash

    \" Double quote\' Single quote

    \n Newline (line feed)

    \r Carriage return\b Backspace

    \t Horizontal tab

    \f Form feed\a Alert (bell)

    \v Vertical tab

    \? Question mark (used to escape trigraphs)

    \nnn Character with octal value nnn\xhh Character with hexadecimal value hh

  • 8/9/2019 In Most Contexts in Later Use, A Mention of the Variable Array is Converted to a Pointer to the First Item in the Arra

    7/23

  • 8/9/2019 In Most Contexts in Later Use, A Mention of the Variable Array is Converted to a Pointer to the First Item in the Arra

    8/23

  • 8/9/2019 In Most Contexts in Later Use, A Mention of the Variable Array is Converted to a Pointer to the First Item in the Arra

    9/23

    Abram

    medan

    nias

  • 8/9/2019 In Most Contexts in Later Use, A Mention of the Variable Array is Converted to a Pointer to the First Item in the Arra

    10/23

    Abram

    medan

    nias

  • 8/9/2019 In Most Contexts in Later Use, A Mention of the Variable Array is Converted to a Pointer to the First Item in the Arra

    11/23

  • 8/9/2019 In Most Contexts in Later Use, A Mention of the Variable Array is Converted to a Pointer to the First Item in the Arra

    12/23

    Abram

    medan

    nias

  • 8/9/2019 In Most Contexts in Later Use, A Mention of the Variable Array is Converted to a Pointer to the First Item in the Arra

    13/23

  • 8/9/2019 In Most Contexts in Later Use, A Mention of the Variable Array is Converted to a Pointer to the First Item in the Arra

    14/23

  • 8/9/2019 In Most Contexts in Later Use, A Mention of the Variable Array is Converted to a Pointer to the First Item in the Arra

    15/23

    RT RT PARMAN DARMO

    abram adi angga weding

    with zoo

    RT RT PARMAN DARMO

    abram adi angga weding

    with zoo

    RT RT PARMAN DARMO

    abram adi angga weding

    with zoo

    RT RT PARMAN DARMO

    abram adi angga weding

  • 8/9/2019 In Most Contexts in Later Use, A Mention of the Variable Array is Converted to a Pointer to the First Item in the Arra

    16/23

    with zoo

  • 8/9/2019 In Most Contexts in Later Use, A Mention of the Variable Array is Converted to a Pointer to the First Item in the Arra

    17/23

  • 8/9/2019 In Most Contexts in Later Use, A Mention of the Variable Array is Converted to a Pointer to the First Item in the Arra

    18/23

    RT RT PARMAN DARMOabram adi angga weding

    with zoo

  • 8/9/2019 In Most Contexts in Later Use, A Mention of the Variable Array is Converted to a Pointer to the First Item in the Arra

    19/23

  • 8/9/2019 In Most Contexts in Later Use, A Mention of the Variable Array is Converted to a Pointer to the First Item in the Arra

    20/23

  • 8/9/2019 In Most Contexts in Later Use, A Mention of the Variable Array is Converted to a Pointer to the First Item in the Arra

    21/23

  • 8/9/2019 In Most Contexts in Later Use, A Mention of the Variable Array is Converted to a Pointer to the First Item in the Arra

    22/23

  • 8/9/2019 In Most Contexts in Later Use, A Mention of the Variable Array is Converted to a Pointer to the First Item in the Arra

    23/23