473,396 Members | 1,872 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,396 software developers and data experts.

zero-based array, in function confucsion

Hai,
When I declare a zero-based array I get a compile time error stating
that zero based array is not possible. But if I declare the same in
the function argument I don't get any error. Is related with the
function in C in which the argument's references are passed in the
stack during the runtime. I am confused.
Nov 13 '05 #1
14 2452
da***********@yahoo.com wrote:
When I declare a zero-based array I get a compile time error stating
that zero based array is not possible. But if I declare the same in
the function argument I don't get any error. Is related with the
function in C in which the argument's references are passed in the
stack during the runtime. I am confused.


What do you mean by "zero-based array" and how do you declare it? The
last time I checked all C arrays where zero-based in the sense that
the index of the first element is 0 (and not 1 like in several other
languages).
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| Je***********@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
Nov 13 '05 #2
Please post your code so we can see what you are trying to do. Thanks.
<da***********@yahoo.com> wrote in message
news:a3*************************@posting.google.co m...
Hai,
When I declare a zero-based array I get a compile time error stating
that zero based array is not possible. But if I declare the same in
the function argument I don't get any error. Is related with the
function in C in which the argument's references are passed in the
stack during the runtime. I am confused.

Nov 13 '05 #3

<da***********@yahoo.com> wrote in message
news:a3*************************@posting.google.co m...
Hai,
When I declare a zero-based array I get a compile time error stating
that zero based array is not possible. But if I declare the same in
the function argument I don't get any error. Is related with the
function in C in which the argument's references are passed in the
stack during the runtime. I am confused.


What you're describing is not very clear. Show us
the code that you're having trouble with.

-Mike
Nov 13 '05 #4
"Kris Wempa" <calmincents(NO_SPAM)@yahoo.com> wrote in message news:<bl*********@kcweb01.netnews.att.com>...
Please post your code so we can see what you are trying to do. Thanks.


Sorry,
void foo(int a[]); /*a[] possible*/
int main(void)
{
unsigned int y[]; /*y[] *not possible*/
return 0;
}
Nov 13 '05 #5
<da***********@yahoo.com> wrote in message
news:a3**************************@posting.google.c om...
"Kris Wempa" <calmincents(NO_SPAM)@yahoo.com> wrote in message news:<bl*********@kcweb01.netnews.att.com>...
Please post your code so we can see what you are trying to do. Thanks.


Sorry,


So it looks like you're trying to define an array of
unspecified size (what you called 'zero based').
Correct terminology is important. You probably
see that now, from the replies you've got so far.
void foo(int a[]); /*a[] possible*/
Here, 'a' is a function parameter. While it *looks* like
an array, it is not, it's a pointer. [1]

(An array cannot be passed to or returned from functions,
only a pointer to one of its elements can (typically the
first). The [] form is just an alternate syntax.

int main(void)
{
unsigned int y[]; /*y[] *not possible*/
No, not possible. Your definition says 'make me an array'
An array is a sequence of *one or more* contigous objects of
the same type. But you did not tell it how many. How can
the compiler make the array? :-) How many 'unsigned ints'
do you want/need? Just say so, e.g.

unsigned int y[10]; /* array of ten unsigned ints */
return 0;
}


May I ask which textbook(s) you're learning from?
[1]

void foo(int a[]);

vs.

void foo(int *a);

Both of these declare that the parame ter'a' is of type
'pointer to int' (int*). Some prefer the first form, others
the latter, a case can be made for each.

When such a function is intended to operate upon an
array, some say that the a[] form conveys this intent,
and that the *a form should only be used when passing
the address of a single object.

Others say that the a[] only unnecessarily 'disguises'
the fact that the parameter is really a pointer.

This seems to me to be a religious issue which I don't
care to debate. I use the *a form, simply because it's
a pointer. But that's just *my* style, it's neither right
nor wrong (unless of course it violates coding standards
to which I've agreed to ahere).

HTH,
-Mike
Nov 13 '05 #6

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:VL*****************@newsread3.news.pas.earthl ink.net...
void foo(int a[]); /*a[] possible*/


Here, 'a' is a function parameter. While it *looks* like
an array, it is not, it's a pointer.


Forgot to add, even with a number inside the [],
it's still not an array. The number will be ignored.
This is a 'special case' for [], when used with a
function parameter.

-Mike
Nov 13 '05 #7
Kris Wempa wrote:
<da***********@yahoo.com> wrote in message
When I declare a zero-based array I get a compile time error
stating that zero based array is not possible. But if I
declare the same in the function argument I don't get any
error. Is related with the function in C in which the
argument's references are passed in the stack during the
runtime. I am confused.


Please post your code so we can see what you are trying to do.
Thanks.


Please refrain from top-posting (fixed) in c.l.c. Among other
things, it is considered rude.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #8
On Tue, 30 Sep 2003 01:26:41 GMT, in comp.lang.c , "Mike Wahler"
<mk******@mkwahler.net> wrote:

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:VL*****************@newsread3.news.pas.earth link.net...
> void foo(int a[]); /*a[] possible*/


Here, 'a' is a function parameter. While it *looks* like
an array, it is not, it's a pointer.


Forgot to add, even with a number inside the [],
it's still not an array. The number will be ignored.


remind me what happens with multidimensional arrays
void munge(int board[8][8]);
as it were.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
Nov 13 '05 #9

"Mark McIntyre" <ma**********@spamcop.net> wrote in message
news:dk********************************@4ax.com...
On Tue, 30 Sep 2003 01:26:41 GMT, in comp.lang.c , "Mike Wahler"
<mk******@mkwahler.net> wrote:

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:VL*****************@newsread3.news.pas.earth link.net...

> void foo(int a[]); /*a[] possible*/

Here, 'a' is a function parameter. While it *looks* like
an array, it is not, it's a pointer.


Forgot to add, even with a number inside the [],
it's still not an array. The number will be ignored.


remind me what happens with multidimensional arrays
void munge(int board[8][8]);


That is equivalent to:

void munge(int board[][8]);

or

void munge(int (*board)[8]);

(param is pointer to array of 8 ints)

Write a function body for that, increment
'board' and compare to the original value
to see what it does.

But this is not what OP asked about.

-Mike
Nov 13 '05 #10
"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<VL*****************@newsread3.news.pas.earth link.net>...
<da***********@yahoo.com> wrote in message
news:a3**************************@posting.google.c om...
"Kris Wempa" <calmincents(NO_SPAM)@yahoo.com> wrote in message news:<bl*********@kcweb01.netnews.att.com>...
Please post your code so we can see what you are trying to do. Thanks.


Sorry,


So it looks like you're trying to define an array of
unspecified size (what you called 'zero based').
Correct terminology is important. You probably
see that now, from the replies you've got so far.
void foo(int a[]); /*a[] possible*/


Here, 'a' is a function parameter. While it *looks* like
an array, it is not, it's a pointer. [1]

(An array cannot be passed to or returned from functions,
only a pointer to one of its elements can (typically the
first). The [] form is just an alternate syntax.

int main(void)
{
unsigned int y[]; /*y[] *not possible*/


No, not possible. Your definition says 'make me an array'
An array is a sequence of *one or more* contigous objects of
the same type. But you did not tell it how many. How can
the compiler make the array? :-) How many 'unsigned ints'
do you want/need? Just say so, e.g.

unsigned int y[10]; /* array of ten unsigned ints */
return 0;
}


May I ask which textbook(s) you're learning from?


Self-learner and some of the notes form net.


[1]

void foo(int a[]);

vs.

void foo(int *a);

Both of these declare that the parame ter'a' is of type
'pointer to int' (int*). Some prefer the first form, others
the latter, a case can be made for each.

When such a function is intended to operate upon an
array, some say that the a[] form conveys this intent,
and that the *a form should only be used when passing
the address of a single object.

Others say that the a[] only unnecessarily 'disguises'
the fact that the parameter is really a pointer.

This seems to me to be a religious issue which I don't
care to debate. I use the *a form, simply because it's
a pointer. But that's just *my* style, it's neither right
nor wrong (unless of course it violates coding standards
to which I've agreed to ahere).

HTH,
-Mike


Thanks for all the answers.
Nov 13 '05 #11

<da***********@yahoo.com> wrote in message
news:a3**************************@posting.google.c om...
"Mike Wahler" <mk******@mkwahler.net> wrote in message

news:<VL*****************@newsread3.news.pas.earth link.net>...
<da***********@yahoo.com> wrote in message
news:a3**************************@posting.google.c om...
"Kris Wempa" <calmincents(NO_SPAM)@yahoo.com> wrote in message

news:<bl*********@kcweb01.netnews.att.com>...
> Please post your code so we can see what you are trying to do. Thanks. >

Sorry,


So it looks like you're trying to define an array of
unspecified size (what you called 'zero based').
Correct terminology is important. You probably
see that now, from the replies you've got so far.
void foo(int a[]); /*a[] possible*/


Here, 'a' is a function parameter. While it *looks* like
an array, it is not, it's a pointer. [1]

(An array cannot be passed to or returned from functions,
only a pointer to one of its elements can (typically the
first). The [] form is just an alternate syntax.

int main(void)
{
unsigned int y[]; /*y[] *not possible*/


No, not possible. Your definition says 'make me an array'
An array is a sequence of *one or more* contigous objects of
the same type. But you did not tell it how many. How can
the compiler make the array? :-) How many 'unsigned ints'
do you want/need? Just say so, e.g.

unsigned int y[10]; /* array of ten unsigned ints */
return 0;
}


May I ask which textbook(s) you're learning from?


Self-learner and some of the notes form net.


Being a "Self-learner" doesn't release you from the
responsibility to yourself to use quality learning
materials. It also does not endow you with knowledge.
Which means you're trying to learn only from 'bits
and pieces' of material from the 'net.

I'll tell you right now that if you don't use textbooks,
you'll not get very far. And much more important, note
that most material on the 'net is simply wrong. You have
no way of knowing this, since you have no quality textbooks
to check the 'net material against.

Your original questions support my claims. :-)

See the book reviews www.accu.org for recommendations
and suggestions from the recognized industry experts.
Nov 13 '05 #12
On Tue, 30 Sep 2003 23:56:07 GMT, in comp.lang.c , "Mike Wahler"
<mk******@mkwahler.net> wrote:
"Mark McIntyre" <ma**********@spamcop.net> wrote in message
news:dk********************************@4ax.com.. .
On Tue, 30 Sep 2003 01:26:41 GMT, in comp.lang.c , "Mike Wahler"
>> > void foo(int a[]); /*a[] possible*/
>>
>> Here, 'a' is a function parameter. While it *looks* like
>> an array, it is not, it's a pointer.
> remind me what happens with multidimensional arrays
void munge(int board[8][8]);


That is equivalent to:

....void munge(int (*board)[8]);
thats what I thought, and it shows that only the 1st dimension of an
array of arrays decays to a pointer upon passing to a fn.
But this is not what OP asked about.
but it /is/ what I wanted to get clear ! Thanks.
-Mike


--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
Nov 13 '05 #13
"Mark McIntyre" <ma**********@spamcop.net> wrote in message
news:ao********************************@4ax.com...
On Tue, 30 Sep 2003 23:56:07 GMT, in comp.lang.c , "Mike Wahler"
<mk******@mkwahler.net> wrote:
"Mark McIntyre" <ma**********@spamcop.net> wrote in message
news:dk********************************@4ax.com.. .
On Tue, 30 Sep 2003 01:26:41 GMT, in comp.lang.c , "Mike Wahler"> > void foo(int a[]); /*a[] possible*/
>>
>> Here, 'a' is a function parameter. While it *looks* like
>> an array, it is not, it's a pointer.
>
remind me what happens with multidimensional arrays
void munge(int board[8][8]);


That is equivalent to:

...
void munge(int (*board)[8]);


thats what I thought, and it shows that only the 1st dimension of an
array of arrays decays to a pointer upon passing to a fn.


The name of an array, when used as a function argument,
always decays to a pointer to its first element.
"Dimensions" are irrelevant to that process. For a
'multidimensional' array, however, the type of a pointer
to an element (which is another array) of that array
necessarily includes the 'dimension' in the syntax which
expresses that pointer type.

E.g. when the name of the an array declared as

'int array[size][size]'

is passed to a function, it is *not* converted to

'int **array',

as many people seem to believe. It is converted to

'int (*array)[size]'
But this is not what OP asked about.


but it /is/ what I wanted to get clear ! Thanks.


OK, I thought you might have believed I had erred
in my post (which I do from time to time), and
your post was a challenge to it.

Thanks for clarifying the purpose of your question,
and you're welcome if I've helped you understand.

And if I've fouled up my above explanation, I'm
sure we'll hear about it. :-)
-Mike
Nov 13 '05 #14
On Thu, 02 Oct 2003 20:37:45 GMT, in comp.lang.c , "Mike Wahler"
<mk******@mkwahler.net> wrote:
"Mark McIntyre" <ma**********@spamcop.net> wrote in message
but it /is/ what I wanted to get clear ! Thanks.
OK, I thought you might have believed I had erred
in my post (which I do from time to time), and
your post was a challenge to it.


not at all.
and you're welcome if I've helped you understand.
FWIW I specifically wanted someone* to clarify for /others/, as I was
a little concerned that there might be a belief that arrays of any
number of dimensions decayed entirely into pointers to whatever their
underlying datatype was.
And if I've fouled up my above explanation, I'm
sure we'll hear about it. :-)


indeedy !

* and I wanted that someone not to be me, as if I'd done it, I'd
surely have made a trivial error and been Popped to death... :-)

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
Nov 13 '05 #15

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

Similar topics

6
by: Andrey | last post by:
What does the standard say about zero-initializing of static structures/classes, specifically: is it guaranteed by the standard that the alignment fillers between the members will also be...
17
by: matthias_k | last post by:
Hi, I am currently implementing a solver for linear optimization problems using the revised simplex method and I stumbled accross some strange behavior regarding the treatment of the number 0....
28
by: Andreas Prilop | last post by:
Jukka reports on http://www.cs.tut.fi/~jkorpela/chars/spaces.html that Internet Explorer 6 fails on the "zero width space" U+200B ​ Is this observation still valid? For which versions of MS...
25
by: Mantorok Redgormor | last post by:
Finally, control is returned to the host environment. If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. beyond this...
53
by: Zhiqiang Ye | last post by:
Hi, All I am reading FAQ of this group. I have a question about this: http://www.eskimo.com/~scs/C-faq/q7.31.html It says: " p = malloc(m * n); memset(p, 0, m * n); The zero fill is...
25
by: pm940 | last post by:
Hello. I've been reading some past discussions on the NULL vs. zero. References are always made to systems or machienes that use values other than zero to represent the NULL pointer. Although...
0
by: LJ72 | last post by:
Hi I have taken over someone elses work and dont know VB at all. I am trying to work out percentage changes between two quarters data and am using the following formula. 'Box 4072 Vet...
4
by: H.S. | last post by:
Hello, I am trying out a few methods with which to test of a given number is practically zero. as an example, does the following test correctly if a given number is zero within machine...
23
by: Hallvard B Furuseth | last post by:
As far as I can tell, (x & -1) is nonzero if the integer x is negative zero. So for signed types, x == 0 does not guarantee (x & foo) == 0. Is that right? (Not that I expect to ever encounter a...
5
by: DBC User | last post by:
How would you develop a zero footprint application, is the smart client application a zero footprint application? Thanks.
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.