Quick C question

Seth Gordon sethg at ropine.com
Wed Nov 16 10:46:53 EST 2005


Ooh!  Ooh!  Ooh!  Mr. Kotter!

> #include <stdio.h>
> 
> #define	D1	4
> #define	D2	2
> 
> int main(){
> 
> 	int *array;

Here you're defining "array" as a pointer to an int.

> 
> 	array = malloc(sizeof(int) * (D1 * D2));
> 
> 	array[1][1] = 32;

Here you're using "array" as a pointer *to a pointer* to an int.

> 
> 	printf("Elem[1][1] = %d\n",array[1][1]);
> 	
> 	free(array);
> 
> 	return 0;
> }

Try changing "int *array" to "int **array".



More information about the Discuss mailing list