Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PASS double** using stdarg

Status
Not open for further replies.

gismo

Technical User
Jul 24, 2002
35
US
I am trying to write a function that receives a varying number of double ** as args. when I try to acces the double ** I get an unhandled exception access violation. Can someone tell me whats up.

//num is the number of arguements being passed. arguements will be double**
void allocate_matrix(int num, ...)
{
va_list ap;
int Inc=0;
extern int nrow,ncol;

va_start(ap,num);
do
{
va_arg(ap, double **) = get_matrix_space(nrow,ncol);

}while(++Inc<num);
va_end(ap);
}

extern double **get_matrix_space(int r, int c)
{
int i;
double **a;

a = calloc(r, sizeof(double *));
--a;
for(i = 1; i<= r; ++i)
{
a= calloc(c,sizeof(double));
--a;
}
return a;
}
 
This doesn't answer your question, but the reason your code listing goes italic half way through is because you are using an &quot;[tt]i[/tt]&quot; for an array index. The string &quot;
Code:
[i]
&quot; is a TGML tag for putting it into italics. If you click the link below the &quot;Your Reply&quot; box that says &quot;Process TGML&quot;, it will show you the different tags you can put in your post.

If you put the tags [tt][ignore]
Code:
[/ignore][/tt] and [tt][ignore]
[/ignore][/tt] before and after your code, it won't do that and your
Code:
a[i]
won't get messed up (it loses the
Code:
[i]
).

Hope this helps.

 
I believe your problem is in the --a, where you decrement
the allocated pointer to an undefined value.
I don't understand how this is supposed to work.

Something like this:
Code:
extern double  **get_matrix_space(int r, int c)
{
    int i,p;
    double **a;
    
    a = malloc(r * sizeof(double *));
    
  if (a) {
     for(i = 0; i < r; ++i) {
        a[i]  = malloc(sizeof(double));
        if (!a[i]) goto fail;
     }
   return a;
   }

fail:
 if (i > 0) {
    for (p=0 ; p <= i ; p++) {
         free(a[p]);
    }
  free(a);
  }
printf(&quot;ERR: malloc failed!!\n&quot;);
return NULL;
}

Also from man va_args() {
The va_arg macro expands to an expression that has the
type and value of the next argument in the call. The
parameter ap is the va_list ap initialized by va_start.
Each call to va_arg modifies ap so that the next call
returns the next argument. The parameter type is a type
name specified so that the type of a pointer to an object
that has the specified type can be obtained simply by
adding a * to type.


Here is some example code for this that works for me:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

#define asize(x) (sizeof(x) / sizeof(x[0])

void printArray(void **,char,int,int);
void handleArgs(int,int,...);


int main(void) {
int a1[] = {12,32,4,56,7,89};
int a2[] = {32,3,21,45,6,12};
int **amaster = malloc(2 * sizeof(int *));
amaster[0] = a1; ;amaster[1] = a2;

            handleArgs(1,6,amaster);
return 0;
}


void printArray(void **arr, char fmt,int x, int asz) {
int p, cnt = 0;
char *str;
int *ptr;

  switch ( fmt ) {

          case 'c':
                str = (char *)arr[x];
              
                while (*str != '\0') {
                      printf(&quot;%c\n&quot;, *str);
                      *str++;
                }
                break;

           case 'i':
                 
                 ptr = (int *)arr[x];
                 printf(&quot;Array element:%d has %d elements\n&quot;, x,asz);
                  while (cnt < asz) {
                        printf(&quot;%d\n&quot;, ptr[cnt]);
                        cnt++;
                  }
                 break;

    }
}       
                  

void handleArgs(int n,int asz,...) {
int a = 0;
void **pt;

va_list ap;
   va_start(ap,asz);
   pt = va_arg(ap,void *);
    
    while (a <= n) {
           printArray(pt,'i',a, asz);
           a++;
    }
    va_end(ap);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top