sending address of multidimensional char array to a function

Reply

Join Date: Oct 2006
Posts: 60
Reputation: asilter is an unknown quantity at this point 
Solved Threads: 0
asilter asilter is offline Offline
Junior Poster in Training

sending address of multidimensional char array to a function

 
0
  #1
Sep 6th, 2007
how do i send a multidimensional char array to a function.

i try assigning values like this :
  1. char ChrArrayAttributes[2][20];
  2.  
  3. strcpy(ChrArrayAttributes[0],"TIME");
  4. strcpy(ChrArrayAttributes[1],"IDENT");

and then sending it like this:
  1. WriteValues("/pp1/reb","RADR",ChrArrayAttributes);

when i compile it gives a warning:
  1. Test.c:115: warning: passing arg 3 of `WriteValues' from incompatible pointer type
  2.  

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:
  1. void WriteValues(char * sDBName, char * sTableName, char * ChrArrayAttr[])
  2. {
  3. int i = 0;
  4. /* Trying to write values TIME and IDENT to the screen */
  5. for(i=0; i<2; i++)
  6. {
  7. printf("ChrArrayAttr[%d]=%s\n",i,ChrArrayAttr[i]);
  8. }
  9. }

thanx.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: sending address of multidimensional char array to a function

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

  1. void WriteValues(char *sDBName, char *sTableName, char ChrArrayAttributes[2][20] )
  2.  
  3. void WriteValues(char *sDBName, char *sTableName, char ChrArrayAttributes[][20] )
  4.  
  5. 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).
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC