delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/09/30/14:10:14

From: Joe Wright <conserv3 AT infi DOT net>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: help with size of trees, and big arrays..
Date: Wed, 30 Sep 1998 13:52:57 -0400
Organization: PC Access
Lines: 43
Message-ID: <36126FF9.4094@infi.net>
References: <004101bdec94$27fdc7a0$0f1919c8 AT mpereze>
Reply-To: conserv3 AT infi DOT net
NNTP-Posting-Host: pm4-1-s5-78.orf.infi.net
Mime-Version: 1.0
X-Mailer: Mozilla 3.03Gold (Win95; I)
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Miguel A Pérez E wrote:
> 
> Hi, how can I do program that manages an array of user
> defined size?
> The following gives me an error
> 
> void main (void){
>     int Tamano;
>     scanf("%d", &Tamano);
>     int Arreglo[Tamano][Tamano];
> }
> 
> It says that a constant is expected..
> 

Something like this:

#include <stdlib.h>

int main(void) {
	int i;
	int Tamano;
	int **Arreglo;
	scanf("%d", %Tamano);
	Arreglo = malloc(Tamano * sizeof (char *));
	for (i = 0; i < Tamano; ++i)
		Arreglo[i] = malloc(Tamano * sizeof(int));
	....
	return 0;
}

Of course, malloc() should always be checked for errors.  This will give
you ability to access Arreglo as if it were an array.  From
Arreglo[0][0] to Arreglo[Tamano-1][Tamano-1].

> I have two more questions:

I'll give someone else a chance at these..

-- 
Joe Wright                                 mailto:conserv3 AT infi DOT net
"Everything should be made as simple as possible, but not simpler."
                    --- Albert Einstein ---

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019