473,320 Members | 2,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

passsing &array same as passing array ?

OK, I can't understand this exactly:

I have this function: int howdy(void *)

I first past this to the function &aCharArray
I realized later that this should be aCharArray but...
the former version also worked ?

I can't understand why that worked. The only thing I can come up with
is that the compiler gave me a break (although I did not receive any
warnings) Or perhaps the compiler does something like this :
&(aCharArray[0]) ?

Everything worked & works ok but I'm just currieus why.

Greetings, Hans
Nov 14 '05 #1
5 2995
Oeleboele <hh******@wanadoo.nl> wrote:
OK, I can't understand this exactly: I have this function: int howdy(void *) I first past this to the function &aCharArray
I realized later that this should be aCharArray but...
the former version also worked ? I can't understand why that worked. The only thing I can come up with
is that the compiler gave me a break (although I did not receive any
warnings) Or perhaps the compiler does something like this :
&(aCharArray[0]) ? Everything worked & works ok but I'm just currieus why.


Normally, whenever an array appears in an expression the compiler
implecitely converts this into a pointer to the first element of
the array - but there's an exception, and that's when you take
the address of the array (or use it as the operand of sizeof),
so 'a' and '&a' (assuming a is an array) evaluate to the same
thing. It's mentioned in section 6.3 of the FAQ and specified in
section 3.2.2.1 of the C89 standard and section 6.3.2.1 of C99
(or at least in the drafts):

Except when it is the operand of the sizeof operator or the
unary & operator (...) an lvalue that has type "array of
type" is converted to an expression that has type "pointer
to type" that points to the initial member of the array
object and is not an lvalue.

But most compilers should give you warning if you do that.

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2
>Oeleboele <hh******@wanadoo.nl> wrote:
OK, I can't understand this exactly:
I have this function: int howdy(void *)

I first past this to the function &aCharArray
I realized later that this should be aCharArray but...
the former version also worked ?

I can't understand why that worked. ...


This is expectable, and perhaps even required (it is hard to tell).

In article <2n***********@uni-berlin.de>,
<Je***********@physik.fu-berlin.de> wrote:Normally, whenever an array appears in an expression the compiler
implecitely converts this into a pointer to the first element of
the array - but there's an exception, and that's when you take
the address of the array (or use it as the operand of sizeof),
Or put it in any other "non-value-context" (but there really are
no other useful ones; the useless ones are things like operands of
++ or --, or LHS of assignment, where naming an array object is an
error).
so 'a' and '&a' (assuming a is an array) evaluate to the same
thing.
Not quite: they differ in *type* (much as (int)1 and (double)1.0
differ in type, even though they are both "the number 1").

Since the declaration of howdy() in ">>" above says that it takes
a value of type "void *", and any object-pointer-type can be
converted to "void *" and back, it is unsurprising that passing
&aCharArray[0] and passing &aCharArray give the same result: both
are "arrows" pointing to the same first byte, they just point to
a different total *number* of bytes. Conversion to "void *" throws
away the "number of bytes" information, leaving you with two raw
byte-pointers pointing to the same (single) byte.
It's mentioned in section 6.3 of the FAQ and specified in
section 3.2.2.1 of the C89 standard and section 6.3.2.1 of C99
(or at least in the drafts) ...


See also <http://web.torek.net/torek/c/pa.html>.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #3
Chris Torek <no****@torek.net> wrote:
Oeleboele <hh******@wanadoo.nl> wrote:
OK, I can't understand this exactly:
I have this function: int howdy(void *)

I first past this to the function &aCharArray
I realized later that this should be aCharArray but...
the former version also worked ?

I can't understand why that worked. ...
This is expectable, and perhaps even required (it is hard to tell). In article <2n***********@uni-berlin.de>,
<Je***********@physik.fu-berlin.de> wrote:
Normally, whenever an array appears in an expression the compiler
implecitely converts this into a pointer to the first element of
the array - but there's an exception, and that's when you take
the address of the array (or use it as the operand of sizeof),
Or put it in any other "non-value-context" (but there really are
no other useful ones; the useless ones are things like operands of
++ or --, or LHS of assignment, where naming an array object is an
error). so 'a' and '&a' (assuming a is an array) evaluate to the same
thing.

Not quite: they differ in *type* (much as (int)1 and (double)1.0
differ in type, even though they are both "the number 1").


Thanks, that was something I wasn't sure about - but that explains
the warning one gets (about an "incompatible pointer type" in the
function call) if one doesn't make howdy() to expect a void pointer
but a pointer to the type of the elements of the array.

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #4
On 6 Aug 2004 02:04:47 -0700, hh******@wanadoo.nl (Oeleboele) wrote:
OK, I can't understand this exactly:

I have this function: int howdy(void *)

I first past this to the function &aCharArray
I realized later that this should be aCharArray but...
the former version also worked ?

I can't understand why that worked. The only thing I can come up with
is that the compiler gave me a break (although I did not receive any
warnings) Or perhaps the compiler does something like this :
&(aCharArray[0]) ?

Everything worked & works ok but I'm just currieus why.


Everything worked as expected because void* is compatible with a
pointer of any other type. When you passed &aCharArray, the argument
was of type pointer to array of char and was implicitly converted to
void*. When you passed aCharArray, the argument was of type pointer
to char and was implicitly converted to void*. Since both "types"
referred to the same address (had the same value), when each was
converted to void* the value and type received by the function was the
same.

If your function had been expecting a char*, the & argument would have
required a diagnostic during compilation. This is because pointer to
char and pointer to array of char are not compatible for implicit
conversions.
<<Remove the del for email>>
Nov 14 '05 #5
Thank you all for your help. It makes it all clear.
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Leonard J. Reder | last post by:
Hello, I have been trying to get this simple call to work with a SWIG interface. The problem is I do not know how to pass a const char * or a std::string & in python to SWIG wrapped code. I...
2
by: Nick | last post by:
Is there a way that if I host my remoted object in IIS (not having to mess with encryption & authentication via a custom sink) that the server can raise events and the clients can detect them? If...
1
by: Hans Van den Eynden | last post by:
Hallo Why isn't this working?? char *p="abcd"; p='h'; I want to replace a character in the p string. thx
0
by: Omar K | last post by:
Hi, I am quite new to frontpage and SQL but I have a Stock Control access database / frontpage to set up. My last problem deals with the automatic updating of the total quantity of stock with parts...
1
by: Arcadius A. | last post by:
Hello! I'm trying to bind an array of custom objects to a ASP.NET DataGrid: private void Page_Load(object sender, System.EventArgs e) { DataGrid1= new DataGrid();...
30
by: James Daughtry | last post by:
char array; scanf("%19s", &array); I know this is wrong because it's a type mismatch, where scanf expects a pointer to char and gets a pointer to an array of 20 char. I know that question 6.12...
8
by: tomoko | last post by:
$data = array('id' => 00001,'name'=>asuka); $data = array('id' => 00002,'name' =>ntt); $data = array('id' => 00003,'name' =>docomo); for($i=0; $i<3;$i++) { echo $data." ".$data."<br>"; }...
2
by: Velislav | last post by:
Hi, I've got the following piece of code: string GetPropertyValue(object jobCard, string propertyName, int index) { PropertyInfo property = jobCard.GetType().GetProperty(propertyName); if...
2
by: unclefester | last post by:
I'm confused on how to interpret the following statement: char * varName; Is it a pointer to a char array or an array of char pointers? Advanced thanks.
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.