How are my arrays modified when C passes by value?

后端 未结 4 800
傲寒
傲寒 2021-01-23 10:15

I made a simple program in C to check if two words are anagrams. My question is that if I\'m passing word_one and word_two as parameters, doesn\'t that mean that I\'m not modify

相关标签:
4条回答
  • 2021-01-23 10:38

    Except when it is the operand of the sizeof or unary & operator, or is a string literal used to initialize a character array in a declaration, an expression of type "N-element array of T" will be converted ("decay") to an expression of type "pointer to T", and the value of the expression will be the address of the first element of the array.

    When you call

    read_word(word_one); 
    

    The expression word_one is converted from type "26-element array of int" to "pointer to int", so what actually gets passed to read_word is the address of the first element of word_one.

    In the context of a function parameter declaration, T a[N] and T a[] are both interpreted as T *a - a is declared as a pointer to T, not an array of T. Thus, the prototype

    void read_word(int counts[26])
    

    is interpreted as

    void read_word(int *counts)
    

    Thus, read_word does indeed modify the contents of word_one and word_two.

    You'll want to pass the size of the array as a separate parameter:

    read_word( word_one, sizeof word_one );
    ...
    void read_word( int *counts, size_t size )
    {
      ...
    }
    

    since you won't know how big the target array is from the pointer alone.

    0 讨论(0)
  • 2021-01-23 10:42

    Arrays are reference types in C. If a function takes an array, it will be passed a pointer, and any changes you make to it will also affect the original array.

    0 讨论(0)
  • 2021-01-23 11:01

    C passes only the pointer. Only structs and simple types are passed by the value.

    0 讨论(0)
  • 2021-01-23 11:03

    C indeed always passes by value, but when arrays are passed to functions they always decay to pointers, even when you specify the length in the parameter declaration. What happens is that your declaration of

    void read_word(int counts[26])
    

    is equivalent to

    void read_word(int * counts)
    

    When you pass word_one and word_two to your functions, the value they recieve is the address of the first element of the array. These still point to the original arrays and therefore, when you modify them in the function, the result is also visible in the original array.

    ADDIT

    Interesting, but little-known side note: in C99 and beyond you can declare your functions with the length preceded by static, as follows:

    void read_word(int counts[static 26])
    

    This doesn't change the fact that the function receives a copy of the address and not the whole array, but it does allow the compiler to optimize and detect potential errors. In the case of the read_word above, the compiler can warn if you pass a NULL-pointer to the function, or an array with less than 26 elements. More can be read here.

    0 讨论(0)
提交回复
热议问题