Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.c

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 08-16-2012, 01:25 PM
Why Tea
Guest
 
Posts: n/a
Default Array passed as function parameters

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
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 08-16-2012, 03:02 PM
Kenneth Brody
Guest
 
Posts: n/a
Default Re: Array passed as function parameters

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
Reply With Quote
  #3 (permalink)  
Old 08-16-2012, 03:21 PM
Mark Bluemel
Guest
 
Posts: n/a
Default Re: Array passed as function parameters

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.

Reply With Quote
  #4 (permalink)  
Old 08-16-2012, 03:41 PM
Eric Sosman
Guest
 
Posts: n/a
Default Re: Array passed as function parameters

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
Reply With Quote
  #5 (permalink)  
Old 08-16-2012, 03:41 PM
Eric Sosman
Guest
 
Posts: n/a
Default Re: Array passed as function parameters

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
Reply With Quote
  #6 (permalink)  
Old 08-16-2012, 03:41 PM
Eric Sosman
Guest
 
Posts: n/a
Default Re: Array passed as function parameters

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
Reply With Quote
  #7 (permalink)  
Old 08-16-2012, 03:41 PM
Eric Sosman
Guest
 
Posts: n/a
Default Re: Array passed as function parameters

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
Reply With Quote
  #8 (permalink)  
Old 08-16-2012, 03:41 PM
Eric Sosman
Guest
 
Posts: n/a
Default Re: Array passed as function parameters

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
Reply With Quote
  #9 (permalink)  
Old 08-16-2012, 03:41 PM
Eric Sosman
Guest
 
Posts: n/a
Default Re: Array passed as function parameters

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
Reply With Quote
  #10 (permalink)  
Old 08-16-2012, 03:41 PM
Eric Sosman
Guest
 
Posts: n/a
Default Re: Array passed as function parameters

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
Reply With Quote
  #11 (permalink)  
Old 08-16-2012, 03:41 PM
Eric Sosman
Guest
 
Posts: n/a
Default Re: Array passed as function parameters

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
Reply With Quote
  #12 (permalink)  
Old 08-16-2012, 03:41 PM
Eric Sosman
Guest
 
Posts: n/a
Default Re: Array passed as function parameters

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
Reply With Quote
  #13 (permalink)  
Old 08-16-2012, 04:43 PM
John Bode
Guest
 
Posts: n/a
Default Re: Array passed as function parameters

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.
Reply With Quote
  #14 (permalink)  
Old 08-16-2012, 04:43 PM
John Bode
Guest
 
Posts: n/a
Default Re: Array passed as function parameters

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.
Reply With Quote
  #15 (permalink)  
Old 08-16-2012, 04:43 PM
John Bode
Guest
 
Posts: n/a
Default Re: Array passed as function parameters

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.
Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 12:01 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.