C style multidimentionel arrays

N

none

I currently building a library that I would like to use in
both C++ and C projects. I want to use a NxM multidimentional
array, so I'm trying to make it in C. I tried this code:

int **array=(int **)malloc( 3 * sizeof(int*));

for (int M=0; M < 3; M++) {
array[M]=(int*)malloc(3 * sizeof(int));

}

array [0][1]= 1;
array [1][2]= 2;

printf ("[%2i][%2i]",array[0][1],array[1][2]);

but it doesn't work in my cpp visual studio project, should it
work in c++? or am I doing something wrong? the values
printed on screen are not 1 and 2.

thanks in advance.
 
N

none

I just try to compile it with the C compiler and
it doesn't even work in C, so I guess I'm making
something wrong, anyone has any idea? I saw this
kind of code in many places.
 
R

Ravishankar S

none said:
I currently building a library that I would like to use in
both C++ and C projects. I want to use a NxM multidimentional
array, so I'm trying to make it in C. I tried this code:

int **array=(int **)malloc( 3 * sizeof(int*));

for (int M=0; M < 3; M++) {
array[M]=(int*)malloc(3 * sizeof(int));

}

array [0][1]= 1;
array [1][2]= 2;

printf ("[%2i][%2i]",array[0][1],array[1][2]);

but it doesn't work in my cpp visual studio project, should it
work in c++? or am I doing something wrong? the values
printed on screen are not 1 and 2.

thanks in advance.

this code worked file for me:

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

int main(void) {

int M;
int **array=(int **)malloc( 3 * sizeof(int*));


for (M=0; M < 3; M++) {
array[M]=(int*)malloc(3 * sizeof(int));

}

array [0][1]= 1;
array [1][2]= 2;
printf ("[%2i][%2i]",array[0][1],array[1][2]);
return 0;
}
 
R

Ravishankar S

none said:
I currently building a library that I would like to use in
both C++ and C projects. I want to use a NxM multidimentional
array, so I'm trying to make it in C. I tried this code:

int **array=(int **)malloc( 3 * sizeof(int*));

for (int M=0; M < 3; M++) {
array[M]=(int*)malloc(3 * sizeof(int));

}

array [0][1]= 1;
array [1][2]= 2;

printf ("[%2i][%2i]",array[0][1],array[1][2]);

but it doesn't work in my cpp visual studio project, should it
work in c++? or am I doing something wrong? the values
printed on screen are not 1 and 2.

thanks in advance.

I guess for such a library one has to write in C becasue C is a subset of
C++. The "for(int M" you have used in C++ not C (atleast C89).
 
N

none

I currently building a library that I would like to use in
both C++ and C projects. I want to use a NxM multidimentional
array, so I'm trying to make it in C. I tried this code:
int **array=(int **)malloc( 3 * sizeof(int*));
for (int M=0; M < 3; M++) {
   array[M]=(int*)malloc(3 * sizeof(int));

array [0][1]= 1;
array [1][2]= 2;
printf ("[%2i][%2i]",array[0][1],array[1][2]);
but it doesn't work in my cpp visual studio project, should it
work in c++? or am I doing something wrong? the values
printed on screen are not 1 and 2.
thanks in advance.

this code worked file for me:

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

int main(void) {

   int M;
   int **array=(int **)malloc( 3 * sizeof(int*));

   for (M=0; M < 3; M++) {
      array[M]=(int*)malloc(3 * sizeof(int));

   }

   array [0][1]= 1;
   array [1][2]= 2;
   printf ("[%2i][%2i]",array[0][1],array[1][2]);
   return 0;



}- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -

I found my problem, it was a stupid error in my coding.

thanks for your help.
 
C

Chris Dollin

none said:
I just try to compile it with the C compiler and
it doesn't even work in C, so I guess I'm making
something wrong, anyone has any idea? I saw this
kind of code in many places.

If you don't show us your code, and you don't show
us the compiler diagnostics, why do you think we can
know what's wrong with your code?

Next time /show, don't tell/.
 
K

Keith Thompson

Ravishankar S said:
none said:
I currently building a library that I would like to use in
both C++ and C projects. I want to use a NxM multidimentional
array, so I'm trying to make it in C. I tried this code:
[snip]

I guess for such a library one has to write in C becasue C is a subset of
C++. The "for(int M" you have used in C++ not C (atleast C89).

C is not *exactly* a subset of C++; there's plenty of C code that
either isnt' valid C++, or is valid C++ with a different meaning.
With care, it's possible to write code that's valid C and valid C++,
but it's rarely necessary.

It's possible to call C functions from C++, and vice versa. See the
C++ FAQ (not the C FAQ) for details.
 
C

CBFalconer

none said:
I just try to compile it with the C compiler and it doesn't even
work in C, so I guess I'm making something wrong, anyone has any
idea? I saw this kind of code in many places.

It is obviously the missing parentheses in line 42.

How do you expect any sane answers to the lack of information in
your post? What is 'it'? Why should it work in C? What is 'this
kind of code'?
 
S

santosh

Ravishankar said:
news:1d0c9f5d-e2f7-47aa-b12a-1d9962b54459@h11g2000prf.googlegroups.com...
I currently building a library that I would like to use in
both C++ and C projects. I want to use a NxM multidimentional
array, so I'm trying to make it in C. I tried this code:

int **array=(int **)malloc( 3 * sizeof(int*));

for (int M=0; M < 3; M++) {
array[M]=(int*)malloc(3 * sizeof(int));

}

array [0][1]= 1;
array [1][2]= 2;

printf ("[%2i][%2i]",array[0][1],array[1][2]);

but it doesn't work in my cpp visual studio project, should it
work in c++? or am I doing something wrong? the values
printed on screen are not 1 and 2.

thanks in advance.

I guess for such a library one has to write in C becasue C is a subset
of C++. The "for(int M" you have used in C++ not C (atleast C89).

It would be more precise to say that Standard C and C++ are intersecting
sets, rather than say C is a subset of C++.
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top