LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Passing 2D Array of char (1D string) out of a function

Hi,

 

I have made a function to get list of name in array but i don t to get it out of the function.

See under:

 

In the main program :

 

int iAdjustTableRows()

{

char **szArrayRes; 

 

GetDataFromDB_OneCol  ("Group_Instrument","Group_Name",szArrayRes)

...

 

Im not abble to retrieve my array of string szArrayRes !!! Why ??

 

return 0;

}

 

Function

int GetDataFromDB_OneCol ( char *szTableName, char *szColuName,char **szArrayResults)

 

 char szValue [50]=""; char szQuery [512]="";
 int  resCode,  iValueStatus =0,numLine=0;
    int  hmap =0,  i=0,u=0;
    int  hstmt;  

 

 //*** Get number of line (record)  
   // Formating of query
   sprintf(szQuery, "SELECT %s FROM %s ",szColuName,szTableName);
   
   // Select statement
   hstmt = DBActivateSQL (hdbc, szQuery);
   if (hstmt<= DB_SUCCESS) {ShowError(); goto Error;}
   
   // Get Number of Column
   numLine =  DBNumberOfRecords (hstmt);
   
   resCode = DBDeactivateSQL (hstmt);
   if (resCode != DB_SUCCESS) {ShowError(); goto Error;}  
 
  //*** Dimension of 2D array of char accrding to number of line  
   
   szArrayResults = malloc(sizeof(char*)*numLine);
   
   //memory allocaring for array
   for(u = 0; u <  numLine; ++u)
    {
     szArrayResults[u] = malloc(sizeof(char)*300);
     Fmt(szArrayResults[u],"");
    }
   
  //*** Begin map for constructed SQL statement */
      hmap = DBBeginMap (hdbc);
      if (hmap <= 0) {ShowError(); goto Error;}

         // Specify the columns to be selected and the variables where column values will be placed. //
                    resCode = DBMapColumnToChar (hmap , szColuName, 50, &szValue, &iValueStatus, "");
                     if (resCode != DB_SUCCESS) {ShowError(); goto Error;}
   
           hstmt = DBActivateMap (hmap, szTableName);
                 if (hstmt <= 0) {ShowError(); goto Error;}
   
   //*** Fetch Data until end and fill out array of string
          while (DBFetchNext (hstmt) == 0) {
              if (iValueStatus != DB_NULL_DATA){
                    Fmt(szArrayResults[i],"%s",szValue);
                    i++;
               }
        }

 

At this point the table of string is fill out correctly .

    
   //*** Free Mapping SQL
   resCode = DBDeactivateMap(hmap);hmap =0; 
   if (resCode != DB_SUCCESS) {ShowError(); goto Error;}  
 
 
 //*** Error handling  
 Error:
   if (hmap)
       DBDeactivateMap(hmap);
   hstmt = 0;
     

 return 0;
}

 

 

 

 

 

 

 

0 Kudos
Message 1 of 2
(4,175 Views)

Hi,

 

try

 

char **szArrayRes; 

 

GetDataFromDB_OneCol  ("Group_Instrument","Group_Name", &szArrayRes)

 

You might want to return a value with the number of strings

 

 

Curt

0 Kudos
Message 2 of 2
(4,139 Views)