🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

(double **) void **

#2530
From: Arun M. Thomas
Date: Sat, 06 Apr 1996 14:58
55 lines
2397 bytes
Greetings.  

I'm trying to make a function to generate 2D arrays.  One of the arguments to
my function is the size of the datatype to be stored as one element in the 
array.  The function is listed below, and I'd like to know if it is legal.
I'm afraid it isn't (I've commented at the lines where I'm doubtful.)  

Note:
I did check the FAQ, and didn't find anything specifically mentioning this.
If I still seem to have overlooked something, a pointer to the appropriate 
item will be enough as a response, though more is always welcome.

Thanks in advance, 
-Arun Thomas

/* ========================================================================= */


void **matrix(long len_d1, long len_d2, const size_t tsize)
{ /* allocate a matrix with subscript range [0..len_d1-1][0..len_d2-1] */

    void **m;

    m = (void **) calloc(len_d1, sizeof(void *));        /* Is (void **) a   */
    if(!m) nrerror("allocation failure 1 in matrix()");  /*  valid date type?*/

    for(len_d1--; 0 <= len_d1; len_d1--) {
	m[len_d1] = (void *) calloc(len_d2, tsize); 
	if (!m[len_d1]) nrerror("allocation failure 2 in matrix()");
    }
}

int main(void)
{
   double **m;

   m = (double **) matrix(10, 10, sizeof(double)); 

   /* The above line casts (void **) to (double **) */

   m[5][5] = 1.0; /* Is this a safe command? I'm not sure if it makes sense 
                     to dereference m.  m[5] should point to a (double *), 
                     but when it was assigned, it pointed to a (void *).  Does 
                     the type cast above have an effect at both levels?      */

   return(1);
}

/* ========================================================================= */

Upon re-reading the above, I realize my question may not be very clear.  Yet, 
I'm not sure how to explain it better.  Again, I would appreciate any help, 
even if only a suggestion as what more information might be needed to better
explain this question.  
Technical Details
Message-ID: <4k60po$aic@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: thomas@eel.rdc.toshiba.co.jp (Arun M. Thomas)
Newsgroups: comp.lang.c.moderated
Subject: (double **) void **
Date: 6 Apr 1996 08:58:00 -0600
Organization: Toshiba Corporation, RDC Center, Kawasaki, JAPAN
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4k60po$aic@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com