Dynamic Arrays in C

Associate
Joined
22 Feb 2004
Posts
2,417
Location
Essex
This is driving me nuts!!

i have the following code:

Code:
#include "stdafx.h"
#include "malloc.h"

int **new2d(int n);

int main (){

int **array = NULL;
int n;

do {
	printf("Size of array (>0)?\n");
	scanf("%i", &n);
	} while (n<=0);

array = new2d(n);
}
int **new2d(int n){
	int **array;
	int i;
	
	array=malloc(n * sizeof *array);
	for (i=0; i < n; ++i){
		array[i] = malloc(n * sizeof *array[i]);
	}
	return array;
}

but it will not compile.

error C2440: '=' : cannot convert from 'void *' to 'int **'
on the malloc lines

where am i going wrong? is there another way to make a dynamic 2d array in c?

oh, im using visual c++ - win32 Console application
 
If you say you're using C++, why not use a vector<>?

Really can't see the point reimplementing the wheel...
 
ok, turns out visual c++ is trying to compile it with a c++ compiler, and you cannot do his in c++

Anyone know how to change the compiler to make it compile c only?
 
Right-click your Project in Solution Explorer->Properties->Configuration Properties->C/C++->Advanced->Compile As->Compile as C Code.
 
you still can't compile that in C, malloc returns a pointer to void, you'll need to cast it as a pointer to an int at the very least.

tbh that whole thing looks dubious to me, for an arrray to work like an array it needs to be a single block of memory iirc. Have you considered usign a linked list type of structure instead?

HT
 
Which version of Visual C++ are you using? I've just shoved this through VS 2005 and it compiled (well, had to make a few minor mods but nothing related to the assignments.)

If I had to guess I would suspect you need to put a typecast before your malloc statements.
 
happytechie said:
tbh that whole thing looks dubious to me, for an arrray to work like an array it needs to be a single block of memory iirc. Have you considered usign a linked list type of structure instead?

HT

Yes, though he's actually declaring an array of arrays (which in effect can act like a 2d array, providing all the sub arrays have the same size).
 
MrMatteh said:
using visual c++ express edition 2005

it compiles and runs fine in dev-c++ when making a c-program.

You sure? I built is fine using gcc when named test.c, but when built as test.cpp it complained about the conversion between void and int pointers.

There's a definate divergance between the cade as C and the code as C++.

I agree with happytechie though, those dynamic arrays and pointers bring me out in a cold sweat.....
 
MrMatteh said:
how do you make it compile as a .c instead of .cpp. i cant find it anywhere!

i'd like to use visual c++ as my editor :D

NathanE said:
Right-click your Project in Solution Explorer->Properties->Configuration Properties->C/C++->Advanced->Compile As->Compile as C Code.

?
 
happytechie said:
you still can't compile that in C, malloc returns a pointer to void, you'll need to cast it as a pointer to an int at the very least.
I have to admit, I thought so as well. But from the comp.lang.c FAQ:

Q: Why does some code carefully cast the values returned by malloc
to the pointer type being allocated?

A: Before ANSI/ISO Standard C introduced the void * generic pointer
type, these casts were typically required to silence warnings
(and perhaps induce conversions) when assigning between
incompatible pointer types.

Under ANSI/ISO Standard C, these casts are no longer necessary,
and in fact modern practice discourages them, since they can
camouflage important warnings which would otherwise be generated
if malloc() happened not to be declared correctly; see question
7.6 above. (However, the casts are typically seen in C code
which for one reason or another is intended to be compatible
with C++, where explicit casts from void * are required.)
Dave
 
Back
Top Bottom