zero-based array, in function confucsion

D

dam_fool_2003

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.
 
J

Jens.Toerring

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
--
_ _____ _____
| ||_ _||_ _| (e-mail address removed)-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
 
M

Mike Wahler

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
 
D

dam_fool_2003

Kris Wempa said:
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;
}
 
M

Mike Wahler

"Kris Wempa" <calmincents(NO_SPAM)@yahoo.com> wrote in message

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
 
M

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.

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
 
C

CBFalconer

Kris said:
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.
 
M

Mark McIntyre

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.
 
M

Mike Wahler

Mark McIntyre said:
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
 
D

dam_fool_2003

Mike Wahler said:
"Kris Wempa" <calmincents(NO_SPAM)@yahoo.com> wrote in message

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.
 
M

Mike Wahler

"Mike Wahler" <[email protected]> wrote in message
"Kris Wempa" <calmincents(NO_SPAM)@yahoo.com> wrote in message
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.
 
M

Mark McIntyre

Mark McIntyre said:
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.
 
M

Mike Wahler

Mark McIntyre said:
Mark McIntyre said:
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 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
 
M

Mark McIntyre

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... :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top