The difference between the transmitting array, a fixed-size array and the base address of the array as a function parameter

I am confused which syntax to use if I want to pass an array of known or unknown size as a parameter to a function.

Suppose I have these options for this purpose:

void func1(char* str) { //print str } void func2(char str[]) { //print str } void func3(char str[10]) { //print str } 

What are the advantages and disadvantages of using each of them?

+48
c ++ arrays pointers
Apr 22 '13 at 10:11
source share
7 answers

All of these options are the same. C simply allows you to use alternate spellings, but even the latter, explicitly annotated with the size of the array, breaks up into a regular pointer.

That is, even with the latest implementation, you can call a function with an array of any size:

 void func3(char str[10]) { } func("test"); // Works. func("let try something longer"); // Not a single f*ck given. 

Needless to say, this cannot be used: it can give the user a false sense of security ("oh, this function only accepts an array of length 10, so I don’t need to check the length myself").

As Henrik said, the correct way in C ++ is to use std::string , std::string& or std::string const& (depending on whether you need to modify the object and whether you want to copy it).

+62
Apr 22 '13 at 10:16
source share

Please note that in C ++, if the length of the array is known at compile time (for example, if you passed a string literal), you can get its size:

 template<unsigned int N> void func(const char(&str)[N]) { // Whatever... } int main() { func("test"); // Works, N is 5 } 
+18
Apr 22 '13 at 13:15
source share

In C ++, use void func4(const std::string& str) .

+11
Apr 22 '13 at 10:13
source share

All of them are functionally identical. When you pass an array to a function in C, the array gets an implicit conversion to a pointer to the first element of the array. Therefore, these three functions will print the same result (i.e. the size of the char pointer).

 void func1(char* str) { printf("sizeof str: %zu\n", sizeof str); } void func2(char str[]) { printf("sizeof str: %zu\n", sizeof str); } void func3(char str[10]) { printf("sizeof str: %zu\n", sizeof str); } 

This conversion applies only to the first size of the array. A char[42][13] converted to char (*)[13] , not char ** .

 void func4(char (*str_array)[13]) { printf("sizeof str_array: %zu\n" "sizeof str_array[0]: %zu\n", sizeof str_array, sizeof str_array[0]); } 

char (*)[13] - type str_array . This is how you write "pointer to an array of 13 char s". It could also be written as void func4(char str_array[42][13]) { ... } , although 42 is functionally pointless, as you can see by experimenting by passing arrays of different sizes to func4 .

In C99 and C11 (but not C89 or C ++), you can pass a pointer to an array of different sizes in a function by passing its size with it and including the size identifier in [square brackets] . For example:

 void func5(size_t size, char (*str_array)[size]) { printf("sizeof str_array: %zu\n" "sizeof str_array[0]: %zu\n", sizeof str_array, sizeof str_array[0]); } 

This declares a pointer to an array of size char s. Note that you must dereference the pointer before accessing the array. In the above example, sizeof str_array[0] evaluates the size of the array, not the size of the first element. As an example, to access the 11th element, use (*str_array)[11] or str_array[0][11] .

+7
Apr 22 '13 at 10:50
source share

In C, the first two definitions are equivalent. The third is essentially the same, but it gives an idea of ​​the size of the array.

If str printing is your goal, then you can safely use any of them. In fact, a parameter of type char* is passed to all three functions, namely that printf() should print a string. And no need you do not know, despite the fact that it may seem, all parameters passing in C are executed in pass-by-value mode.

Edit: It looks like I will be very strict in choosing words on SO henceforth. Well, in the third case, it does not represent the size of the array to the function to which it is transferred, since it ultimately reduces to the char* type in the same way as in the first two cases. I wanted to say that this tells the person reading it that the size of the array is 10. Also, this is not wrong / illegal in C.But for a program, doing it as good as it is useless. This does not give any idea of ​​the size of the array for the function that it passed .Mr.Downvoter, thanks, indicating that randomness and negligence are not allowed on SO.

+1
Apr 22 '13 at 10:16
source share

In a one-dimensional array, they are all processed by the compiler in the same way. However, for two or more dimensional arrays (for example, myArray[10][10] ) this is useful because it can be used to determine the length of a row / column of an array.

+1
Apr 22 '13 at 10:29
source share

An attachment describing in points.

1) As everyone said, this is one and the same.

2) Arrays are decomposed into pointers when they are passed in function arguments.

3) A fundamental problem may be finding the size of the array in the function. For this we can use a macro.

  #define noOfElements(v) sizeof(v)/sizeof(0[v]) int arr[100] myfunction ( arr, noOfElements(arr)) 

either 0 [v] or v [0] can be used in a macro, where the first is used to avoid using the user-defined data type in noOfElements.

Hope this helps.

+1
Apr 22 '13 at 11:01
source share



All Articles