c99 array size Q

P

pemo

In c99 we can write this:

void func(int size, double arr[*size*])
{
return;
}

int main(void)
{
double arr[10] = {0.0};

func(sizeof(arr) / sizeof(double), arr);

return 0;
}

The question is, in func(), does using arr[size] serve any purpose apart
from being semantically useful - regarding the size of arr [semantially, we
can say that the int size parameter *encodes* the size of arr and is in fact
not some other unrelated info], and/or, possibly helping the compiler
optimise the code - and if the latter, in what ways might this be done?
 
M

Mark McIntyre

In c99 we can write this:

void func(int size, double arr[*size*])

presumably without the asterisks.
The question is, in func(), does using arr[size] serve any purpose

AFAIK in all cases such arrays are converted into a pointer anyway, so
including the size in the definition has no meaning.

Mark McIntyre
 
M

Marc Boyer

Le 29-03-2006 said:
In c99 we can write this:

void func(int size, double arr[*size*])
{
return;
}

int main(void)
{
double arr[10] = {0.0};

func(sizeof(arr) / sizeof(double), arr);

return 0;
}

The question is, in func(), does using arr[size] serve any purpose apart
from being semantically useful - regarding the size of arr [semantially, we
can say that the int size parameter *encodes* the size of arr and is in fact
not some other unrelated info], and/or, possibly helping the compiler
optimise the code - and if the latter, in what ways might this be done?

For a one dimension array,, it does not make any difference. But just
try this:

#include <stdio.h>
void foo(int size_x, int size_y, int tab[size_x][size_y]){
printf("tab[1][1]= %d\n", tab[1][1] );
}
int main(){
int tab[9] = { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 };
foo(2,2,tab);
foo(3,3,tab);
return 0;
}

news> ./a.out
tab[1][1]= 3
tab[1][1]= 4

Marc Boyer
 
L

lovecreatesbeauty

Marc said:
For a one dimension array,, it does not make any difference. But just
try this:

#include <stdio.h>
void foo(int size_x, int size_y, int tab[size_x][size_y]){
printf("tab[1][1]= %d\n", tab[1][1] );
}
int main(){
int tab[9] = { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 };
foo(2,2,tab);
foo(3,3,tab);
return 0;
}

news> ./a.out
tab[1][1]= 3
tab[1][1]= 4

It is strange. I think `foo(3,3,tab);' should refer to the value out of
the array boundary.

I even can not get myself understand the foo(2,2,tab); thing. But you
are right, as I get the same answer from the GCC3.3.5 (even lack of
variable length array for GCC4.1) compiler with yours.

Even I can not figure out following the sample. I think

tab[0][0] == 0
tab[1][1] == 4
tab[2][2] == 8

are true in the following sample, but the compiler gives the different
result. I am so grateful, if you can give more hints on this problem
for me. Thank you in advance.

#include <stdio.h>

void
foo(int size_x, int size_y, int tab[size_x][size_y])
{
printf("tab[%i][%i] = %d\n", size_x, size_y, tab[size_x][size_y]);
}

int
main()
{
int tab[9] = {0, 1, 2,
3, 4, 5,
6, 7, 8};

foo(0, 0, tab);
foo(1, 1, tab);
foo(2, 2, tab);
foo(3, 3, tab); /* goes out of array boundary */

return 0;
}

/*
tab[0][0] = 0
tab[1][1] = 2
tab[2][2] = 6
tab[3][3] = 1073833280
*/
 
M

Marc Boyer

lovecreatesbeauty said:
I even can not get myself understand the foo(2,2,tab); thing. But you
are right, as I get the same answer from the GCC3.3.5 (even lack of
variable length array for GCC4.1) compiler with yours.

Even I can not figure out following the sample. I think

tab[0][0] == 0
tab[1][1] == 4
tab[2][2] == 8

are true in the following sample, but the compiler gives the different
result. I am so grateful, if you can give more hints on this problem
for me. Thank you in advance.

#include <stdio.h>

void
foo(int size_x, int size_y, int tab[size_x][size_y])
{
printf("tab[%i][%i] = %d\n", size_x, size_y, tab[size_x][size_y]);
}

This code make an acces out ouf bounds. With
int tab[5];
then
tab[5]
is out-of-bounds. The only right indexes are 0,1,2,3,4.
The same
int tab[3][3];
then
tab[3][3]
is out-of-bounds.

Marc Boyer
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top