|
|
||||
|
||||
|
|
|
|||
|
On 8/16/2012 9:25 AM, Why Tea wrote:
> Could anyone please explain the differences between > the following array passing? Thanks! > > 1) int get_data(char d[100]) {...} > > 2) int get_data(char *d) {...} In (1), the function gets passed a pointer to an array of 100 chars. In (2), the function gets passed a "generic" pointer to char. I expected some differences in the compiler's treatment of the code, but a test here shows none. Which brings me to a related question... The following gives me no errors or warnings, even with warnings set to max. Shouldn't it at least warn me, if not downright fail? extern void foo(char array[100]); void bar(void) { char array[50]; foo(array); } Also, this gives no warnings: void foo(char array[100]) { array++; } (Note that "array" is incremented by 1, not 100.) -- Kenneth Brody |
|
|||
|
On 16/08/2012 16:02, Kenneth Brody wrote:
> On 8/16/2012 9:25 AM, Why Tea wrote: >> Could anyone please explain the differences between >> the following array passing? Thanks! >> >> 1) int get_data(char d[100]) {...} >> >> 2) int get_data(char *d) {...} > > In (1), the function gets passed a pointer to an array of 100 chars. In > (2), the function gets passed a "generic" pointer to char. Not quite. In 1) the function is passed a pointer to the first char of an array of 100 chars which is not the same as a pointer to an array of 100 chars. FAQ 6.4 is probably a good start point for Why Tea... The FAQs are at c-faq.com. |
|
|||
|
On 8/16/2012 11:02 AM, Kenneth Brody wrote:
> On 8/16/2012 9:25 AM, Why Tea wrote: >> Could anyone please explain the differences between >> the following array passing? Thanks! >> >> 1) int get_data(char d[100]) {...} >> >> 2) int get_data(char *d) {...} > > In (1), the function gets passed a pointer to an array of 100 chars. In > (2), the function gets passed a "generic" pointer to char. (Counter-trolling, Kenneth? If so, I apoligize for spoiling the fun -- But there's enough confusion on this point that it seems dangerous to leave things alone.) There's no difference. See Question 6.21 in the comp.lang.c Frequently Asked Questions (FAQ) page, <http://www.c-faq.com/>. -- Eric Sosman esosman@ieee-dot-org.invalid |
|
|||
|
On 8/16/2012 11:02 AM, Kenneth Brody wrote:
> On 8/16/2012 9:25 AM, Why Tea wrote: >> Could anyone please explain the differences between >> the following array passing? Thanks! >> >> 1) int get_data(char d[100]) {...} >> >> 2) int get_data(char *d) {...} > > In (1), the function gets passed a pointer to an array of 100 chars. In > (2), the function gets passed a "generic" pointer to char. (Counter-trolling, Kenneth? If so, I apoligize for spoiling the fun -- But there's enough confusion on this point that it seems dangerous to leave things alone.) There's no difference. See Question 6.21 in the comp.lang.c Frequently Asked Questions (FAQ) page, <http://www.c-faq.com/>. -- Eric Sosman esosman@ieee-dot-org.invalid |
|
|||
|
On 8/16/2012 11:02 AM, Kenneth Brody wrote:
> On 8/16/2012 9:25 AM, Why Tea wrote: >> Could anyone please explain the differences between >> the following array passing? Thanks! >> >> 1) int get_data(char d[100]) {...} >> >> 2) int get_data(char *d) {...} > > In (1), the function gets passed a pointer to an array of 100 chars. In > (2), the function gets passed a "generic" pointer to char. (Counter-trolling, Kenneth? If so, I apoligize for spoiling the fun -- But there's enough confusion on this point that it seems dangerous to leave things alone.) There's no difference. See Question 6.21 in the comp.lang.c Frequently Asked Questions (FAQ) page, <http://www.c-faq.com/>. -- Eric Sosman esosman@ieee-dot-org.invalid |
|
|||
|
On 8/16/2012 11:02 AM, Kenneth Brody wrote:
> On 8/16/2012 9:25 AM, Why Tea wrote: >> Could anyone please explain the differences between >> the following array passing? Thanks! >> >> 1) int get_data(char d[100]) {...} >> >> 2) int get_data(char *d) {...} > > In (1), the function gets passed a pointer to an array of 100 chars. In > (2), the function gets passed a "generic" pointer to char. (Counter-trolling, Kenneth? If so, I apoligize for spoiling the fun -- But there's enough confusion on this point that it seems dangerous to leave things alone.) There's no difference. See Question 6.21 in the comp.lang.c Frequently Asked Questions (FAQ) page, <http://www.c-faq.com/>. -- Eric Sosman esosman@ieee-dot-org.invalid |
|
|||
|
On 8/16/2012 11:02 AM, Kenneth Brody wrote:
> On 8/16/2012 9:25 AM, Why Tea wrote: >> Could anyone please explain the differences between >> the following array passing? Thanks! >> >> 1) int get_data(char d[100]) {...} >> >> 2) int get_data(char *d) {...} > > In (1), the function gets passed a pointer to an array of 100 chars. In > (2), the function gets passed a "generic" pointer to char. (Counter-trolling, Kenneth? If so, I apoligize for spoiling the fun -- But there's enough confusion on this point that it seems dangerous to leave things alone.) There's no difference. See Question 6.21 in the comp.lang.c Frequently Asked Questions (FAQ) page, <http://www.c-faq.com/>. -- Eric Sosman esosman@ieee-dot-org.invalid |
|
|||
|
On 8/16/2012 11:02 AM, Kenneth Brody wrote:
> On 8/16/2012 9:25 AM, Why Tea wrote: >> Could anyone please explain the differences between >> the following array passing? Thanks! >> >> 1) int get_data(char d[100]) {...} >> >> 2) int get_data(char *d) {...} > > In (1), the function gets passed a pointer to an array of 100 chars. In > (2), the function gets passed a "generic" pointer to char. (Counter-trolling, Kenneth? If so, I apoligize for spoiling the fun -- But there's enough confusion on this point that it seems dangerous to leave things alone.) There's no difference. See Question 6.21 in the comp.lang.c Frequently Asked Questions (FAQ) page, <http://www.c-faq.com/>. -- Eric Sosman esosman@ieee-dot-org.invalid |
|
|||
|
On 8/16/2012 11:02 AM, Kenneth Brody wrote:
> On 8/16/2012 9:25 AM, Why Tea wrote: >> Could anyone please explain the differences between >> the following array passing? Thanks! >> >> 1) int get_data(char d[100]) {...} >> >> 2) int get_data(char *d) {...} > > In (1), the function gets passed a pointer to an array of 100 chars. In > (2), the function gets passed a "generic" pointer to char. (Counter-trolling, Kenneth? If so, I apoligize for spoiling the fun -- But there's enough confusion on this point that it seems dangerous to leave things alone.) There's no difference. See Question 6.21 in the comp.lang.c Frequently Asked Questions (FAQ) page, <http://www.c-faq.com/>. -- Eric Sosman esosman@ieee-dot-org.invalid |
|
|||
|
On 8/16/2012 11:02 AM, Kenneth Brody wrote:
> On 8/16/2012 9:25 AM, Why Tea wrote: >> Could anyone please explain the differences between >> the following array passing? Thanks! >> >> 1) int get_data(char d[100]) {...} >> >> 2) int get_data(char *d) {...} > > In (1), the function gets passed a pointer to an array of 100 chars. In > (2), the function gets passed a "generic" pointer to char. (Counter-trolling, Kenneth? If so, I apoligize for spoiling the fun -- But there's enough confusion on this point that it seems dangerous to leave things alone.) There's no difference. See Question 6.21 in the comp.lang.c Frequently Asked Questions (FAQ) page, <http://www.c-faq.com/>. -- Eric Sosman esosman@ieee-dot-org.invalid |
|
|||
|
On 8/16/2012 11:02 AM, Kenneth Brody wrote:
> On 8/16/2012 9:25 AM, Why Tea wrote: >> Could anyone please explain the differences between >> the following array passing? Thanks! >> >> 1) int get_data(char d[100]) {...} >> >> 2) int get_data(char *d) {...} > > In (1), the function gets passed a pointer to an array of 100 chars. In > (2), the function gets passed a "generic" pointer to char. (Counter-trolling, Kenneth? If so, I apoligize for spoiling the fun -- But there's enough confusion on this point that it seems dangerous to leave things alone.) There's no difference. See Question 6.21 in the comp.lang.c Frequently Asked Questions (FAQ) page, <http://www.c-faq.com/>. -- Eric Sosman esosman@ieee-dot-org.invalid |
|
|||
|
On Thursday, August 16, 2012 8:25:57 AM UTC-5, Why Tea wrote:
> Could anyone please explain the differences between > > the following array passing? Thanks! > > > > 1) int get_data(char d[100]) {...} > > > > 2) int get_data(char *d) {...} > > > > /WT Both are treated the same; in the context of an array parameter declaration, "T a[N]" and "T a[]" are both interpreted as "T *a"; all three declare "a" as a pointer to "T" (this is *only* true for function parameter declarations, though). Except when it is the operand of the sizeof or unary "&" operators, or is astring literal being used to initialize another array in a declaration, anexpression of type "N-element array of T" will be converted or "decay" to an expression of type "pointer to T", and the value of the converted expression will be the address of the first element in the array. So, assuming code like int arr[20]; foo(arr); the expression "arr" in the call to "foo" will be converted from type "20-element array of int" to "pointer to int", and the value will be the same as"&arr[0]"; thus, "foo" receives a pointer value as its argument, not an array. You can declare "foo" as either void foo(int a[20]) or void foo (int a[]) or void foo (int *a) and all will be interpreted the same way. |
|
|||
|
On Thursday, August 16, 2012 8:25:57 AM UTC-5, Why Tea wrote:
> Could anyone please explain the differences between > > the following array passing? Thanks! > > > > 1) int get_data(char d[100]) {...} > > > > 2) int get_data(char *d) {...} > > > > /WT Both are treated the same; in the context of an array parameter declaration, "T a[N]" and "T a[]" are both interpreted as "T *a"; all three declare "a" as a pointer to "T" (this is *only* true for function parameter declarations, though). Except when it is the operand of the sizeof or unary "&" operators, or is astring literal being used to initialize another array in a declaration, anexpression of type "N-element array of T" will be converted or "decay" to an expression of type "pointer to T", and the value of the converted expression will be the address of the first element in the array. So, assuming code like int arr[20]; foo(arr); the expression "arr" in the call to "foo" will be converted from type "20-element array of int" to "pointer to int", and the value will be the same as"&arr[0]"; thus, "foo" receives a pointer value as its argument, not an array. You can declare "foo" as either void foo(int a[20]) or void foo (int a[]) or void foo (int *a) and all will be interpreted the same way. |
|
|||
|
On Thursday, August 16, 2012 8:25:57 AM UTC-5, Why Tea wrote:
> Could anyone please explain the differences between > > the following array passing? Thanks! > > > > 1) int get_data(char d[100]) {...} > > > > 2) int get_data(char *d) {...} > > > > /WT Both are treated the same; in the context of an array parameter declaration, "T a[N]" and "T a[]" are both interpreted as "T *a"; all three declare "a" as a pointer to "T" (this is *only* true for function parameter declarations, though). Except when it is the operand of the sizeof or unary "&" operators, or is astring literal being used to initialize another array in a declaration, anexpression of type "N-element array of T" will be converted or "decay" to an expression of type "pointer to T", and the value of the converted expression will be the address of the first element in the array. So, assuming code like int arr[20]; foo(arr); the expression "arr" in the call to "foo" will be converted from type "20-element array of int" to "pointer to int", and the value will be the same as"&arr[0]"; thus, "foo" receives a pointer value as its argument, not an array. You can declare "foo" as either void foo(int a[20]) or void foo (int a[]) or void foo (int *a) and all will be interpreted the same way. |
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|