Search code examples
c++c-strings

Using .c_str' with pointers (and also pointers to pointers)


so, I encountered a little problem and I am kinda stuck. Basically I am trying to pass the value of a string** in C-type form to a char* string The code is as follows:

static int BuildDBListSql( std::string **SqlBuf,
                          const char*   ColumnNames,
                          const char* TableNames,
                          const char*   CmdText,
                          sUINT Mode)   
{
   int rtnval = sSUCCESS;

   const char * testSql = ( Mode & 02 ) ? ColumnNames : CmdText;

   if ( SU_DbControl::DbCreateTradeTempTables(testSql) != sSUCCESS )
   {
      sLogMessage("Problem with temporary table results", sLOG_ERROR, 0);
      return( sERROR );
   }
   
   if ( Mode & 02 )     
   {
      *SqlBuf = new std::string[strlen(ColumnNames) + SQL_MAX_SELECT*40];
      *SqlBuf = &std::string(ColumnNames);
 
      if ( !( Mode & 010 ) )
      {
     // Attach State/Retrieval SQL.
        char* SqlBufcopy = (*SqlBuf)->c_str();
        sSQLInsertStateAndRetrieval( sDbConvertMode( Mode ), SqlBufcopy);
      }
   }
   // SQL fragments are being passed:
   else
   {
      size_t sqlBufLength = 0;
      if ( Mode & 010 )
      {
 
         sqlBufLength = strlen(ColumnNames) + strlen(TableNames) + strlen(CmdText) + SQL_MAX_SELECT;
         *SqlBuf = new std::string[ sqlBufLength ];
     //sprintf( *SqlBuf, "SELECT %s FROM %s %s ",
         *SqlBuf = fmt::format("SELECT {} FROM {} {} ", ColumnNames, TableNames, CmdText);  // ColumnNames, TableNames, CmdText );
        
      }
      else
      {
         std::string *sqlPtr = new char[strlen(CmdText) + 2*SQL_MAX_SELECT];
     strcpy( sqlPtr, CmdText );
 
     sSQLSpecializeWhereClause( TableNames, sqlPtr );

         sqlBufLength = strlen(ColumnNames) + strlen(TableNames) + SQL_MAX_SELECT;
         sqlBufLength += strchr(TableNames, ',') ? strlen(CmdText) : strlen(sqlPtr);
         *SqlBuf = new char[ sqlBufLength ];
     sprintf( *SqlBuf, "SELECT %s From %s %s",
                 ColumnNames,
                 TableNames,
                 strchr( TableNames, ',' ) ? CmdText : sqlPtr );
     delete [] sqlPtr;
     // Attach State/Retrieval SQL
     rtnval = sSQLInsertStateAndRetrieval( sDbConvertMode( Mode ),
                          *SqlBuf );
      }
   }
   if (Mode & 0100)
   {
      char * tempBuf = sEntitySQLToDbSQL(*SqlBuf);
      if( tempBuf )
      {
         delete [] *SqlBuf;
         *SqlBuf = new char[ strlen(tempBuf) + 1];
         strcpy(*SqlBuf, tempBuf);
      }
      else
      {
         sLogMessage("Error in sEntitySQLToDbSQL", sLOG_ERROR, 0);
         return( sERROR );
      }
   }

   return rtnval;
}

i get this error when running the solution: related to this line of code char* SqlBufcopy = (*SqlBuf)->c_str();

left of '.c_str' must have class/struct/union, type is std::string**

I kinda understand that the error is there due to me trying to get a c-type string out of a pointer, but I dont know the correct syntax to do what i want to do.

I tried with

 char *SqlBufcopy = *SqlBuf.c_str() 

also with

 char *SqlBufcopy = *SqlBuf->c_str()

and it didnt work, help pls


Solution

  • To fix the error you ask about, change char *SqlBufcopy = *SqlBuf.c_str(); to

    char *SqlBufcopy = (*SqlBuf)->c_str();
    

    Reason: SqlBuf is pointer to pointer (which makes no sense at all), so to get to the actual object, you need to dereference it twice.