473,324 Members | 2,535 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,324 software developers and data experts.

Array and function question

/* My question is why the output of this program is not 2. Thank you in
advance. */

void recieve_array(char a[]);

int main() {
char array[2];
recieve_array(array);
}

void recieve_array(char a[]) {
printf("%d", sizeof(a)); // WTF!?!!? This outputs 4 which is strange.
}
Dec 14 '07 #1
7 1270
"Logan" <Lo*********@uts.edu.auschrieb im Newsbeitrag
news:pa***************************@uts.edu.au...
/* My question is why the output of this program is not 2. Thank you in
advance. */

void recieve_array(char a[]);

int main() {
char array[2];
recieve_array(array);
}

void recieve_array(char a[]) {
printf("%d", sizeof(a)); // WTF!?!!? This outputs 4 which is strange.
}
an array decays to a pointer to it's first element when passed to a
function. So void recieve_array(char a[]) is actually thew same thing as
void recieve_array(char *a)
And a char * on your system apparently is 4 bytes long.

Bye, Jojo
Dec 14 '07 #2
Logan <Lo*********@uts.edu.auwrites:
/* My question is why the output of this program is not 2. Thank you in
advance. */

void recieve_array(char a[]);

int main() {
char array[2];
recieve_array(array);
}

void recieve_array(char a[]) {
printf("%d", sizeof(a)); // WTF!?!!? This outputs 4 which is strange.
}
The comp.lang.c fAQ is at <http://www.c-faq.com/>. You've asked
question 6.21.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Dec 14 '07 #3
Logan wrote:
/* My question is why the output of this program is not 2. Thank you in
advance. */

void recieve_array(char a[]);
`receive_array` takes a pointer-to-char: in an argument declaration,
an argument of type T[] is rewritten as an argument of type T*. This
is for compatibility with an actual array value being converted
to a pointer to it's first element when the array appears in a
[non-sizeof, non-&] expression.
int main() {
char array[2];
recieve_array(array);
}

void recieve_array(char a[]) {
printf("%d", sizeof(a)); // WTF!?!!? This outputs 4 which is strange.
}
On your machine, `sizeof (char*)` is 4, and hence so is `sizeof(a)`.

--
Sighs Hedgehog
"Who do you serve, and who do you trust?" /Crusade/

Dec 14 '07 #4
On Dec 14, 11:44 am, Logan <Logan.W....@uts.edu.auwrote:
/* My question is why the output of this program is not 2. Thank you in
advance. */

void recieve_array(char a[]);

int main() {
char array[2];
recieve_array(array);

}

void recieve_array(char a[]) {
printf("%d", sizeof(a)); // WTF!?!!? This outputs 4 which is strange.

}- Hide quoted text -

- Show quoted text -
1.receive_array takes a pointer to char in an argument declaration

2.void receive _array (char a[]) is same thing as void
receive_array(char*a[]).

3 the sizeof (char*) is 4, and hence so is `sizeof(a)`.

Dec 14 '07 #5
In article <Hg*******************@fe2.news.blueyonder.co.uk >,
Chris Dollin <eh@electrichedgehog.netwrote:
>void recieve_array(char a[]) {
>On your machine, `sizeof (char*)` is 4, and hence so is `sizeof(a)`.
(Random digression)

I was interested to see, in Dennis Ritchie's "The Development of the C
Language", that in the earliest version of C, pointers were declared as

char cpointer[];

instead of

char *cpointer;

and that array variables were just compiler-initialised pointers.

-- Richard
--
:wq
Dec 14 '07 #6
ka*************@gmail.com writes:
On Dec 14, 11:44 am, Logan <Logan.W....@uts.edu.auwrote:
>/* My question is why the output of this program is not 2. Thank you in
advance. */

void recieve_array(char a[]);
<snip>
1.receive_array takes a pointer to char in an argument declaration

2.void receive _array (char a[]) is same thing as void
receive_array(char*a[]).
You probably meant to type "receive_array(char *a)". A "char *a[]"
parameter is quite a different beast.

--
Ben.
Dec 14 '07 #7
Barry Schwarz wrote:
>
Declarations and definitions must precede statements.
Not according to the current C standard...

--
Ian Collins.
Dec 18 '07 #8

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

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
3
by: SilverWolf | last post by:
I need some help with sorting and shuffling array of strings. I can't seem to get qsort working, and I don't even know how to start to shuffle the array. Here is what I have for now: #include...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
9
by: buda | last post by:
Hi, I've been wondering for a while now (and always forgot to ask :) what is the exact quote from the Standard that forbids the use of (&array) (when x >= number_of_columns) as stated in the FAQ...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
7
by: Jim Carlock | last post by:
Looking for suggestions on how to handle bad words that might get passed in through $_GET variables. My first thoughts included using str_replace() to strip out such content, but then one ends...
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
7
by: Franky | last post by:
Following discussion in a previous post, i have used an array inside a loop to gather results: //Function called from another page function displayQuestions($ModNum){ //modnum is used...
4
by: mab464 | last post by:
I have this code on my WAMP server running on my XP machine if ( isset( $_POST ) ) { for($i=0; $i<count($_POST);$i++) { if ($ans != NULL ) $ans .= ", " . $_POST ; // Not the first...
5
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
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...
1
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....

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.