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
 
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?
 
Back
Top Bottom