954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

sending address of multidimensional char array to a function

how do i send a multidimensional char array to a function.

i try assigning values like this :

char ChrArrayAttributes[2][20];
 
strcpy(ChrArrayAttributes[0],"TIME");                                        
strcpy(ChrArrayAttributes[1],"IDENT");


and then sending it like this:

WriteValues("/pp1/reb","RADR",ChrArrayAttributes);


when i compile it gives a warning:

Test.c:115: warning: passing arg 3 of `WriteValues' from incompatible pointer type


and when i debug with gdb debugger, just before entering into WriteValues function, it gives segmentation fault. It never enters in.

why?

WriteValues function is here:

void WriteValues(char * sDBName, char * sTableName, char * ChrArrayAttr[])
{
      int i = 0;
      /* Trying to write values TIME and IDENT to the screen */
      for(i=0; i<2; i++)
      {
             printf("ChrArrayAttr[%d]=%s\n",i,ChrArrayAttr[i]);
      }
}


thanx.

asilter
Junior Poster in Training
60 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

> void WriteValues(char * sDBName, char * sTableName, char * ChrArrayAttr[])
There are several compatible declarations for passing a 2D array - one of them is really easy.

void WriteValues(char *sDBName, char *sTableName, char ChrArrayAttributes[2][20] )

void WriteValues(char *sDBName, char *sTableName, char ChrArrayAttributes[][20] )

void WriteValues(char *sDBName, char *sTableName, char (*ChrArrayAttributes)[20] )

The first is dead easy, you simply copy/paste the declaration of the array you want to pass, into the parameter position of the function you want to pass it to.

The occasional compiler / tool will fuss over the specification of the major dimension, but it's easy to remove with a quick edit.

As with any other array parameter, you just use the array name (like you do now).

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You