🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c.moderated
Article #2189

Re: dynamic creation of multi-dimentional arrays

#2189
From: Donald McLachlan
Date: Thu, 07 Mar 1996 00:26
42 lines
1785 bytes
[Originally bounced because it looked like the FAQ question. -mod]

Mr(s) clc,

I either I wrote my question poorly or you misunderstood it as a trivial
case.  Maybe you can help me write the question in such a way that when I
re-post it to c.l.c.m more people will understand what I'm after.

I might word is like, "Are they any defacto-standard data structures and
functions available to allocate/manipulate N dimensional arrays, with dimension
sizes  N0, N1, N2, ... NN when N, N), N1, N2, ... NN are completely unknown at
compile time?"

[I have read the FAQ, and while 6.13 6.14 6.16 and 6.19 are
all relavent, they do not provide an answer.]


I have a user who basically wants to do something like the following.

	int ndim, *dimsizes, i;

	printf("How many dimensions will the array have? ");
	sscanf("%d", &ndim);
	dimsizes = malloc(ndim * sizeof(int));

	for(i = 0; i < ndim; ++i)
	{
		printf("enter the width of dimension %d: ", i);
		sscanf("%d", &dimsize[i]);
	}

Then the user wants to allocate the required storage, and declare functions
to insert values into the "array".  I declared a pointer and malloc'ed
sizeof(type) * N) ... NN, but accessing this thing became beastly.

The problem is that not knowing the number of array indexes, it gets difficult
to index into the data.

Is there any "common" way of doing this?

Thanks,
Don
Technical Details
Message-ID: <4hlaf4$4eu@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: don@mars.dgrc.doc.ca (Donald McLachlan)
Newsgroups: comp.lang.c.moderated
Subject: Re: dynamic creation of multi-dimentional arrays
Date: 6 Mar 1996 18:26:11 -0600
Organization: Digital Solutions
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4hlaf4$4eu@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com