473,574 Members | 17,882 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

initialising array of unknown size (newbie)

Hi,

Im trying to migrate from programming in Matlab over to C. Im trying
to make a simple function to multiply one matrix by the other. I've
realised that C can't determine the size of a 2d array, so im
inputting the dimensions of those myself.

The problem is that the output array (C=A*B) has as many rows as A and
as many columns as B. I would think of initialising C with:

double C[A_rows][B_cols];

It looks like the compiler doesn't like this at all.

What is the best way to define C? could i use a structure data type
for A and B, or do the dimensions of C need to be defined using
pointers? Perhaps the malloc function comes in somewhere.

I would greatly appreciate any help.

Thanks
Adam
Oct 9 '08 #1
33 7145
Adam Chapman <ad**********@s tudent.manchest er.ac.ukwrites:
Im trying to migrate from programming in Matlab over to C. Im trying
to make a simple function to multiply one matrix by the other. I've
realised that C can't determine the size of a 2d array, so im
inputting the dimensions of those myself.

The problem is that the output array (C=A*B) has as many rows as A and
as many columns as B. I would think of initialising C with:

double C[A_rows][B_cols];

It looks like the compiler doesn't like this at all.
There is nothing obviously wrong with the above. It would help to see
the code or at least the error message. In particular, the "best"
solution will be different if the dimensions are fixed and know at
compile time, are fixed but known only at run time, or if they are
true arbitrary variables. Also, are the dimensions small?
What is the best way to define C? could i use a structure data type
for A and B, or do the dimensions of C need to be defined using
pointers? Perhaps the malloc function comes in somewhere.
Maybe. It is important to know if you can use C99. C99 (the current
standard) has some features that would make your task simpler. Rather
the talk about both, it would be better to know what version of C you
are using/obliged to use.

Have you seen the comp.lang.c FAQ? http://c-faq.com/

--
Ben.
Oct 9 '08 #2
On Oct 9, 11:57*am, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
Adam Chapman <adam.chap...@s tudent.manchest er.ac.ukwrites:
Im trying to migrate from programming in Matlab over to C. Im trying
to make a simple function to multiply one matrix by the other. I've
realised that C can't determine the size of a 2d array, so im
inputting the dimensions of those myself.
The problem is that the output array (C=A*B) has as many rows as A and
as many columns as B. I would think of initialising C with:
double C[A_rows][B_cols];
It looks like the compiler doesn't like this at all.

There is nothing obviously wrong with the above. *It would help to see
the code or at least the error message. *In particular, the "best"
solution will be different if the dimensions are fixed and know at
compile time, are fixed but known only at run time, or if they are
true arbitrary variables. *Also, are the dimensions small?
What is the best way to define C? could i use a structure data type
for A and B, or do the dimensions of C need to be defined using
pointers? Perhaps the malloc function comes in somewhere.

Maybe. *It is important to know if you can use C99. *C99 (the current
standard) has some features that would make your task simpler. *Rather
the talk about both, it would be better to know what version of C you
are using/obliged to use.
I'm currently using the visual C++ 2008 compiler, but trying to keep
the code as standard as possible so I can use it on embedded
hardwarew, something like gumstix.
Have you seen the comp.lang.c FAQ? *http://c-faq.com/

--
Ben.
Thanks for te fast reply. Here is my current code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int Arows=3, Acols=3, Brows=3, Bcols=3, Crows, Ccols;

int A[3][3]={1,2,3,4,5,6,7 ,8,9};
int B[3][3]={1,0,0,0,1,0,0 ,0,1};
int C = malloc(Arows * Bcols * sizeof(int));
main()
{
int row, col, i;

if (Acols!=Brows){
printf("Cannot compute; the number of columns of A
equals the number of rows of B");
for(;;);
return 0;
}
// Calculate Product
for(row=0; row<Arows; row++)
{
for(col=0; col<Bcols; col++)
{
C[row][col]=0;
for(i=0; i<Acols; i++)
{
*C[row][col]=C[row][col] + ((A[row][i])*(B[i]
[col])); }
}
}

for(row=0; row<Arows; row++)
{
for(col=0; col<Bcols; col++)
{
// printf("%1d ", C[row][col]);
}
printf("\n");
}
for(;;);
return 0;
}

I'm getting "error C2099: initializer is not a constant" from the line
"int C = malloc(Arows * Bcols * sizeof(int));"

As for the size of an array, I'd like to make my function capable of
any size (but not huge enough to fill all the memory), this is
requirement for my overall project. I know I haven't made the function
take inputs properly yet, I'm just getting it to work first.

Thanks
Adam
Oct 9 '08 #3
Adam Chapman <ad**********@s tudent.manchest er.ac.ukwrites:
On Oct 9, 11:57¬*am, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
>Adam Chapman <adam.chap...@s tudent.manchest er.ac.ukwrites:
Im trying to migrate from programming in Matlab over to C. Im trying
to make a simple function to multiply one matrix by the other. I've
realised that C can't determine the size of a 2d array, so im
inputting the dimensions of those myself.
The problem is that the output array (C=A*B) has as many rows as A and
as many columns as B. I would think of initialising C with:
double C[A_rows][B_cols];
It looks like the compiler doesn't like this at all.

There is nothing obviously wrong with the above. ¬*It would help to see
the code or at least the error message. ¬*In particular, the "best"
solution will be different if the dimensions are fixed and know at
compile time, are fixed but known only at run time, or if they are
true arbitrary variables. ¬*Also, are the dimensions small?
What is the best way to define C? could i use a structure data type
for A and B, or do the dimensions of C need to be defined using
pointers? Perhaps the malloc function comes in somewhere.

Maybe. ¬*It is important to know if you can use C99. ¬*C99 (the current
standard) has some features that would make your task simpler. ¬*Rather
the talk about both, it would be better to know what version of C you
are using/obliged to use.
I'm currently using the visual C++ 2008 compiler, but trying to keep
the code as standard as possible so I can use it on embedded
hardwarew, something like gumstix.
OK, then I think C99 is out. The very best portability is to be had
by aiming for the common intersection of C90 and C99. Essentially use
"old C" but avoid anything that interferes with (or is wrong in) the
new standard.
>Have you seen the comp.lang.c FAQ? ¬*http://c-faq.com/

--
Ben.
best not to quote sig blocks. In fact, it is best to interleave your
reply with the quoted test so you can comment on specific parts.
Thanks for te fast reply. Here is my current code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int Arows=3, Acols=3, Brows=3, Bcols=3, Crows, Ccols;
It is not a good idea to duplicate information. It is better to
define these in a manner that can be used to define the arrays.
Maybe:

#define AROWS 3
#define ACOLS 3

int A[AROWS][ACOLS] = {/*...*/};
>
int A[3][3]={1,2,3,4,5,6,7 ,8,9};
int B[3][3]={1,0,0,0,1,0,0 ,0,1};
int C = malloc(Arows * Bcols * sizeof(int));
Well there is no need for malloc here. C can be defined thus:

int C[3][3];

but you knew that! I wonder why you thought malloc was needed (I'll
explain the error later).
main()
This makes main and int rerturning function (correct) but in C99 this
"implicit int" declaration is not permitted. It is much better just
to write:

int main(void)
{
int row, col, i;

if (Acols!=Brows){
printf("Cannot compute; the number of columns of A
equals the number of rows of B");
for(;;);
Eh? If you are having the "my message disappears unless this is
there" then try to find the correct way to run your program so you can
see its output (I can't help with that -- I simply don't know).
return 0;
}
// Calculate Product
for(row=0; row<Arows; row++)
{
for(col=0; col<Bcols; col++)
{
C[row][col]=0;
for(i=0; i<Acols; i++)
{
*C[row][col]=C[row][col] + ((A[row][i])*(B[i]
[col]));
You just want to write C[row][col] = ... (i.e. no *). Also, C has a
+= operator that simplifies this sort of thing.
}
}
}

for(row=0; row<Arows; row++)
{
for(col=0; col<Bcols; col++)
{
// printf("%1d ", C[row][col]);
}
printf("\n");
}
As a general point, I'd break this up into functions. At the very
least, I'd have a print_matrix function, but I would also be tempted
to put the A[X][i] * B[i][X] sum calculation into a function.
for(;;);
return 0;
}

I'm getting "error C2099: initializer is not a constant" from the line
"int C = malloc(Arows * Bcols * sizeof(int));"
This is because, at file scope, an object can only be initialised with
a compiler-time constant -- something that the compiler can work out.
A call to malloc is not such a thing. In fact, for simplicity, C
rules out all function calls in such positions, even those that could
be worked out by the compiler.
As for the size of an array, I'd like to make my function capable of
any size (but not huge enough to fill all the memory), this is
requirement for my overall project. I know I haven't made the function
take inputs properly yet, I'm just getting it to work first.
The "any size" is not really enough. The issue is whether the sizes
can be know at compile time. It you need a set of very general array
manipulation functions, be prepared for a lot of learning. Have you
looked to see if there is some free code available to do what you
need?

If you want fully flexible array sizes and C90 code you may well be
better off using arrays of pointers rather than 2D arrays. If there
is any chance that you might have to change the representation of your
arrays from one form to the other (maybe because of performance
issues) it could well pay to start off to use an opaque type right
from the start:

struct matrix *matrix_create( size_t rows, size_t cols);
element_t matrix_get(stru ct matrix *m, size_t row, size_t col);
void matrix_set(stru ct matrix *m, size_t row, size_t col, element_t e);

so you can change all the details at will later on.

--
Ben.
Oct 9 '08 #4
On Oct 9, 1:49*pm, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
Adam Chapman <adam.chap...@s tudent.manchest er.ac.ukwrites:
On Oct 9, 11:57*am, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
Adam Chapman <adam.chap...@s tudent.manchest er.ac.ukwrites:
Im trying to migrate from programming in Matlab over to C. Im trying
to make a simple function to multiply one matrix by the other. I've
realised that C can't determine the size of a 2d array, so im
inputting the dimensions of those myself.
The problem is that the output array (C=A*B) has as many rows as Aand
as many columns as B. I would think of initialising C with:
double C[A_rows][B_cols];
It looks like the compiler doesn't like this at all.
There is nothing obviously wrong with the above. *It would help to see
the code or at least the error message. *In particular, the "best"
solution will be different if the dimensions are fixed and know at
compile time, are fixed but known only at run time, or if they are
true arbitrary variables. *Also, are the dimensions small?
What is the best way to define C? could i use a structure data type
for A and B, or do the dimensions of C need to be defined using
pointers? Perhaps the malloc function comes in somewhere.
Maybe. *It is important to know if you can use C99. *C99 (the current
standard) has some features that would make your task simpler. *Rather
the talk about both, it would be better to know what version of C you
are using/obliged to use.
I'm currently using the visual C++ 2008 compiler, but trying to keep
the code as standard as possible so I can use it on embedded
hardwarew, something like gumstix.

OK, then I think C99 is out. *The very best portability is to be had
by aiming for the common intersection of C90 and C99. *Essentially use
"old C" but avoid anything that interferes with (or is wrong in) the
new standard.
Have you seen the comp.lang.c FAQ? *http://c-faq.com/
--
Ben.

best not to quote sig blocks. *In fact, it is best to interleave your
reply with the quoted test so you can comment on specific parts.
Thanks for te fast reply. Here is my current code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int Arows=3, Acols=3, Brows=3, Bcols=3, Crows, Ccols;

It is not a good idea to duplicate information. *It is better to
define these in a manner that can be used to define the arrays.
Maybe:

#define AROWS 3
#define ACOLS 3

int A[AROWS][ACOLS] = {/*...*/};
int A[3][3]={1,2,3,4,5,6,7 ,8,9};
int B[3][3]={1,0,0,0,1,0,0 ,0,1};
int C = malloc(Arows ** Bcols * sizeof(int));

Well there is no need for malloc here. *C can be defined thus:

* int C[3][3];

but you knew that! *I wonder why you thought malloc was needed (I'll
explain the error later).
main()

This makes main and int rerturning function (correct) but in C99 this
"implicit int" declaration is not permitted. *It is much better just
to write:

int main(void)
{
* *int row, col, i;
* *if (Acols!=Brows){
* * * * * * * printf("Cannot compute; the number of columns of A
equals the number of rows of B");
* * * * * *for(;;);

Eh? *If you are having the "my message disappears unless this is
there" then try to find the correct way to run your program so you can
see its output (I can't help with that -- I simply don't know).
* * * * * *return 0;
* *}
* * // Calculate Product
* *for(row=0; row<Arows; row++)
* *{
* * * * * *for(col=0; col<Bcols; col++)
* * * * * *{
* * * * * * * * * * * * * *C[row][col]=0;
* * * * * * * * * *for(i=0; i<Acols; i++)
* * * * * * * * * *{
* * * * * * * * * * **C[row][col]=C[row][col] +((A[row][i])*(B[i]
[col]));

You just want to write C[row][col] = ... (i.e. no *). *Also, C has a
+= operator that simplifies this sort of thing.
* * * * * * * * * * * * * * * * * * * * * }
* * * * * *}
* *}
* *for(row=0; row<Arows; row++)
* *{
* * * * * *for(col=0; col<Bcols; col++)
* * * * * *{
* * * * * *// * * *printf("%1d * ", C[row][col]);
* * * * * *}
* * * * * *printf("\n");
* *}

As a general point, I'd break this up into functions. *At the very
least, I'd have a print_matrix function, but I would also be tempted
to put the A[X][i] * B[i][X] sum calculation into a function.
* *for(;;);
* *return 0;
}
I'm getting "error C2099: initializer is not a constant" from the line
"int C = malloc(Arows ** Bcols * sizeof(int));"

This is because, at file scope, an object can only be initialised with
a compiler-time constant -- something that the compiler can work out.
A call to malloc is not such a thing. *In fact, for simplicity, C
rules out all function calls in such positions, even those that could
be worked out by the compiler.
As for the size of an array, I'd like to make my function capable of
any size (but not huge enough to fill all the memory), this is
requirement for my overall project. I know I haven't made the function
take inputs properly yet, I'm just getting it to work first.

The "any size" is not really enough. *The issue is whether the sizes
can be know at compile time. *It you need a set of very general array
manipulation functions, be prepared for a lot of learning. *Have you
looked to see if there is some free code available to do what you
need?

If you want fully flexible array sizes and C90 code you may well be
better off using arrays of pointers rather than 2D arrays. *If there
is any chance that you might have to change the representation of your
arrays from one form to the other (maybe because of performance
issues) it could well pay to start off to use an opaque type right
from the start:

* struct matrix *matrix_create( size_t rows, size_t cols);
* element_t matrix_get(stru ct matrix *m, size_t row, size_t col);
* void matrix_set(stru ct matrix *m, size_t row, size_t col, element_t e);

so you can change all the details at will later on.

--
Ben.- Hide quoted text -

- Show quoted text -
Thanks. I want to eventually put the matrix multiplication function
into a header so it can be called from future scripts. The malloc was
for allocating memory for a matrix of size Arows*BCols, where Arows
and Bcols could be any size and are inputs to the function. Very
general I know, but still be useful when it finally works.
Looking at http://cboard.cprogramming.com/archi...p/t-57095.html
and http://www.hermetic.ch/cfunlib/arrays/arrays.htm, it looks very
hard. I will need to read about pointers over again (and probably
again and again!) but its all a good learnig experience I suppose.

Thanks for your time,
I appreciate it

Adam
Oct 9 '08 #5
Adam Chapman <ad**********@s tudent.manchest er.ac.ukwrites:
On Oct 9, 1:49¬*pm, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
<snip>
>best not to quote sig blocks. ¬*In fact, it is best to interleave your
reply with the quoted test so you can comment on specific parts.
If you intend to post to technical groups like this it will really
help you if you copy the style of posting that is favoured here: snip
lots (but keep enough context so your message makes sense) and
interleave your reply with the quoted text you are replying to.

<snip>
>--
Ben.- Hide quoted text -
It will also help if it looks like you are listening to the advice!
Thanks. I want to eventually put the matrix multiplication function
into a header so it can be called from future scripts.
One usually puts function prototypes in header files. The code itself
is usually elsewhere and is said to be "linked" with the code that
calls it. For this sort of program it would be normal to write a
library of matrix functions to link with future programs.
The malloc was
for allocating memory for a matrix of size Arows*BCols, where Arows
and Bcols could be any size and are inputs to the function. Very
general I know, but still be useful when it finally works.
And it would have worked (nearly) if it had been in a function. In C
it does not help to rough things out one way and then wrap it all up
in a function because C has so many special rules. What can be done
in one place is often not allowed elsewhere. It *really* helps to
choose your functions first and sketch them out. If you don't, you
will probably have to rewrite everything when you come to "wrap it up
in a function" later.
Looking at http://cboard.cprogramming.com/archi...p/t-57095.html
and http://www.hermetic.ch/cfunlib/arrays/arrays.htm, it looks very
hard.
Yes it would. That is not good code. The variable names are hopeless
and there are no re-usable functions even for the most obviously
re-used code. The worst fault, though, is that that code does not
check any of the allocations and if you plan to write code for an
embedded device (I think you said this was your eventual target) then
this simply can't ignored.

Try reading:

http://www.eskimo.com/~scs/cclass/int/top.html

and look at Chapter 23. If you need some more basic stuff, try the
introduction to C:

http://www.eskimo.com/~scs/cclass/notes/top.html
I will need to read about pointers over again (and probably
again and again!) but its all a good learnig experience I suppose.
Note also that the code you cited does not really do 2D arrays. It
does arrays of pointers to arrays. This is just one way to represent
your data in "old C". The other is to pretend the array is flat (as
you seemed to be doing with your malloc call) and using a
multiplication to access the elements. Both have advantages and
disadvantages. It is not clear which will be better for you.

--
Ben.
Oct 9 '08 #6
On Oct 9, 3:32*pm, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
Adam Chapman <adam.chap...@s tudent.manchest er.ac.ukwrites:
On Oct 9, 1:49*pm, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
<snip>
best not to quote sig blocks. *In fact, it is best to interleave your
reply with the quoted test so you can comment on specific parts.

If you intend to post to technical groups like this it will really
help you if you copy the style of posting that is favoured here: snip
lots (but keep enough context so your message makes sense) and
interleave your reply with the quoted text you are replying to.

<snip>
--
Ben.- Hide quoted text -

It will also help if it looks like you are listening to the advice!
Thanks. I want to eventually put the matrix multiplication function
into a header so it can be called from future scripts.

One usually puts function prototypes in header files. *The code itself
is usually elsewhere and is said to be "linked" with the code that
calls it. *For this sort of program it would be normal to write a
library of matrix functions to link with future programs.
The malloc was
for allocating memory for a matrix of size Arows*BCols, where Arows
and Bcols could be any size and are inputs to the function. Very
general I know, but still be useful when it finally works.

And it would have worked (nearly) if it had been in a function. *In C
it does not help to rough things out one way and then wrap it all up
in a function because C has so many special rules. *What can be done
in one place is often not allowed elsewhere. *It *really* helps to
choose your functions first and sketch them out. *If you don't, you
will probably have to rewrite everything when you come to "wrap it up
in a function" later.
Looking athttp://cboard.cprogram ming.com/archive/index.php/t-57095.html
andhttp://www.hermetic.ch/cfunlib/arrays/arrays.htm, it looks very
hard.

Yes it would. *That is not good code. *The variable names are hopeless
and there are no re-usable functions even for the most obviously
re-used code. *The worst fault, though, is that that code does not
check any of the allocations and if you plan to write code for an
embedded device (I think you said this was your eventual target) then
this simply can't ignored.

Try reading:

*http://www.eskimo.com/~scs/cclass/int/top.html

and look at Chapter 23. *If you need some more basic stuff, try the
introduction to C:

*http://www.eskimo.com/~scs/cclass/notes/top.html
I will need to read about pointers over again (and probably
again and again!) but its all a good learnig experience I suppose.

Note also that the code you cited does not really do 2D arrays. *It
does arrays of pointers to arrays. *This is just one way to represent
your data in "old C". *The other is to pretend the array is flat (as
you seemed to be doing with your malloc call) and using a
multiplication to access the elements. *Both have advantages and
disadvantages. *It is not clear which will be better for you.

--
Ben.
Done it!
For anyone looking this up, here is the code i ended up with:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

//double A[9], B[9];
int Arows=3, Acols=3, Brows=3, Bcols=3;
int A[3][3]={1,2,3,4,5,6,7 ,8,10};
int B[3][3]={9,8,7,6,5,4,3 ,2,1};
int **C;
int i, j, k, row, col;
main()
{
C=malloc(Arows * sizeof(int *));

for(i=0; i<Arows; i++)
{
C[i]=malloc(Bcols * sizeof(int));
for(j=0; j<Bcols; j++)
{
C[i][j]=0;

for(k=0; k<Acols; k++)
{
C[i][j]+=A[i][k]*B[k][j];

}
printf("%0d ", C[i][j]);
}
printf("\n");
}
for(;;);
return C;
}
Now I've got to set it up to take A and B as inputs and make the
function return C. Will post when I've got it

Adam
Oct 9 '08 #7
Adam Chapman <ad**********@s tudent.manchest er.ac.ukwrites:
[...]
Done it!
For anyone looking this up, here is the code i ended up with:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

//double A[9], B[9];
int Arows=3, Acols=3, Brows=3, Bcols=3;
int A[3][3]={1,2,3,4,5,6,7 ,8,10};
int B[3][3]={9,8,7,6,5,4,3 ,2,1};
You have 8 different occurences of the number 3 (not counting the two
in the initializers), and the relationship among the various 3s is not
sufficiently obvious. During maintenance, you could easily change one
of them and forget to change another one.

Also, Arows et al should really be constants. As written, you could
legally change their values; to understand your code, a reader has to
verify that you haven't done so.

Here's how I'd write the above. (A side note: "const" doesn't mean
"constant", it means read-only; "const int Arows = 3;" doesn't make
Arows a constant.)

#define AROWS 3
#define ACOLS 3
#define BROWS 3
#define BCOLS 3

int A[AROWS][ACOLS] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
int B[BROWS][BCOLS] = { {9, 8, 7}, {6, 5, 4}, {3, 2, 1} };

Note that the extra braces explicitly show the individual rows of the
array.

(Actually, I might replace the #define's with this:
enum { AROWS=3, ACOLS=3, BROWS=3, BCOLS=3 };
but some consider that an abuse of the enum feature.)
>
int **C;
int i, j, k, row, col;
Why are all these global? Why not declare them inside main?
main()
Better: int main(void)
{
C=malloc(Arows * sizeof(int *));
malloc() can fail; it indicates this by returning a null pointer. You
should always check for this. If nothing else, you can just abort the
program.

And here's a useful idiom:

C = malloc(Arows * sizeof *C);

Even if the type of C changes (say, if you want arrays of doubles
rather than of ints), the call remains correct.
for(i=0; i<Arows; i++)
{
C[i]=malloc(Bcols * sizeof(int));
Again, you don't check whether the malloc call succeded.

C[i] = malloc(Bcols * sizeof *C[i]);
for(j=0; j<Bcols; j++)
{
C[i][j]=0;

for(k=0; k<Acols; k++)
{
C[i][j]+=A[i][k]*B[k][j];

}
printf("%0d ", C[i][j]);
}
printf("\n");
}
for(;;);
You know this is an infinite loop, right? Which means ...
return C;
.... that this return statement will never be executed.
}
Now I've got to set it up to take A and B as inputs and make the
function return C. Will post when I've got it
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 9 '08 #8
Thanks Keith, although your suggestions have brought up a lot of
errors.
Here is my current code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define Arows 3;
#define Acols 3;
#define Brows 3;
#define Bcols 3;
float A[Arows][Acols]={{1,2,3},{4,5, 6},{7,8,9}};
float B[Brows][Bcols]={{9,8,7},{6,5, 4},{3,2,1}};
float C;
main(){
C=mat_mult();
for(;;); // to stop display closing down (needs proper fix)

}
mat_mult()
{
float **C;
int i, j, k;
C = malloc(Arows * sizeof *C);

for(i=0; i<Arows; i++)
{
C[i] = malloc(Bcols * sizeof *C[i]);
for(j=0; j<Bcols; j++)
{
C[i][j]=0;

for(k=0; k<Acols; k++)
{
C[i][j]+=A[i][k]*B[k][j];

}
printf("%0f ", C[i][j]);
}
printf("\n");
}

return C;
}

#define AROWS 3
#define ACOLS 3
#define BROWS 3
#define BCOLS 3

int A[AROWS][ACOLS] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
int B[BROWS][BCOLS] = { {9, 8, 7}, {6, 5, 4}, {3, 2, 1} };
gives "error C2143: syntax error : missing ']' before ';' "

Also, I keep getting this warning: "warning C4013: 'mat_mult'
undefined; assuming extern returning int" from the line
C=mat_mult();
Sorry if I sound stupid. This is quite difficult for me to get my head
around

Adam
Oct 9 '08 #9
On October 9, 2008 15:11, in comp.lang.c, Adam Chapman
(ad**********@s tudent.manchest er.ac.uk) wrote:
Thanks Keith, although your suggestions have brought up a lot of
errors.
Here is my current code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define Arows 3;
Note what the macro Arows expands to
#define Acols 3;
Note what the macro Acols expands to
#define Brows 3;
Note what the macro Brows expands to
#define Bcols 3;
Note what the macro Bcols expands to
>

float A[Arows][Acols]={{1,2,3},{4,5, 6},{7,8,9}};
Hand expanding the macros, this line reads
float A[3;][3;]={{1,2,3},{4,5, 6},{7,8,9}};
Are you sure that you want this?
float B[Brows][Bcols]={{9,8,7},{6,5, 4},{3,2,1}};
Hand expanding the macros, this line reads
float B[3;][3;]={{9,8,7},{6,5, 4},{3,2,1}};
Again, are you certain that this is what you want?
float C;
main(){
At this point, the function mat_mult() has not been declared or defined. The
compiler will /assume/ that mat_mult() takes an unknown set of arguments,
and returns an integer. Is this what you want?
C=mat_mult();
OK, the integer returned by mat_mult() is now converted to a float.
for(;;); // to stop display closing down (needs proper fix)

}
mat_mult()
This is an error in C99 - you need to tell the compiler what the type is of
the return value. In previous levels of C, the default is an integer. So,
either you have a syntax error (in C99) or you are returning an integer (in
prior levels of C).
{
float **C;
Here, you define an automatic variable with the same name as a file-duration
static variable. /This/ local variable is the one that the function will
use.
int i, j, k;
C = malloc(Arows * sizeof *C);
Hmmmmm.....

I'll leave it to others to comment on the rest of /this code. There's
something very suspicious here, but I don't feel qualified enough to give
it the proper analysis.
>
for(i=0; i<Arows; i++)
{
C[i] = malloc(Bcols * sizeof *C[i]);
for(j=0; j<Bcols; j++)
{
C[i][j]=0;

for(k=0; k<Acols; k++)
{
C[i][j]+=A[i][k]*B[k][j];

}
printf("%0f ", C[i][j]);
}
printf("\n");
}

return C;
You defined C as a pointer to a pointer to a float.
However,
a) you defined the function as returning an integer (assuming < C99)
b) you convert the integer to a float in your main() function
Oops. A pointer is neither an integer, nor a float.
}

> #define AROWS 3
#define ACOLS 3
#define BROWS 3
#define BCOLS 3

int A[AROWS][ACOLS] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
int B[BROWS][BCOLS] = { {9, 8, 7}, {6, 5, 4}, {3, 2, 1} };

gives "error C2143: syntax error : missing ']' before ';' "

Also, I keep getting this warning: "warning C4013: 'mat_mult'
undefined; assuming extern returning int" from the line
>C=mat_mult() ;

Sorry if I sound stupid. This is quite difficult for me to get my head
around

Adam
--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
Oct 9 '08 #10

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

Similar topics

10
9131
by: nospam | last post by:
Hello! I can pass a "pointer to a double" to a function that accepts double*, like this: int func(double* var) { *var=1.0; ... }
9
24380
by: bela | last post by:
hello, may I know the method to create an array with unknown size? in C , one can go for malloc. what is the method in java? regards, bela
0
7820
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main...
0
7738
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8081
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
6486
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, and deploymentówithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5635
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Duprť who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3759
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3777
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2255
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1084
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.