Converting char* to char**

This is a discussion on Converting char* to char** within the C Programming forums, part of the General Programming Boards category; I have a .so lib that requires a char** to be passed to it. For my benefit while working with ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    5

    Converting char* to char**

    I have a .so lib that requires a char** to be passed to it.

    For my benefit while working with the string I prefer working it as a char *.

    So when I go to pass it I am passing &myvar. But am not getting te results I am expecting.

    Would:

    char *myvar;
    somefunc(&myvar);

    Be the same as:

    char **myvar;
    somefunc(myvar);


    From the results I am getting they are not the same...

    So what do I need to to to convert a char * to a char **?


    Thanks in advance,
    Mike

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    Whilst they may satisfy the type system, you have to look at the context of what the code is actually going to do with that pointer.
    If it's expecting to write some value (to a char*), then the &myvar call is correct.

    Eg.
    Code:
    void foo ( char **p ) {
      *p = malloc(10);
    }
    int main ( void ) {
      char *q;
      char **bad;  // not pointing anywhere
      foo ( &q );
      // q now points to 10 bytes of memory (or NULL)
      foo ( bad );  // type is OK, but the pointer gets written to somewhere you don't want it
      return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    5
    Well I don't have access to the code. But basically it is using the value from the char** (the actual string) to calculate a hash key.

    Right now when I pass it the &myvar the returned hash key is different almost evertime I call the function. So I know it is not getting what it expects.


    The header file defines the function as:

    Code:
    extern CcExport char *GenerateHash( char **params, const char *key );

    So &myvar does not seem to be the right way to convert my char * to a char ** to pass to this function.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    If you don't have the code, then read the manual page on how to use the function.
    If you don't have that, then you're stuck.

    Like I said, there are several syntactically valid ways of passing a parameter, but unless you know which one you're supposed to use, it's just not going to work.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    5
    There is no man page... It is a proprietary encrytion algorythm from HSBC.

    Well generally it expects "char **params" to be passed from argv from standard input.

    But in this case I'm using the lib to create a PHP extension. So it is actually coming in as a PHP proprietary type (zval).

    That zval contains an array which I convert to a char *.

    Now I just need to get this char * into a char **.
    I've tried several things but hasn't worked yet.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    Are you suggesting that
    char *foo = GenerateHash( argv, "hello" );
    is an acceptable way of calling the function?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    5
    more like:

    Code:
    
    int main( int argc, char **argv )
    {
        char *strEncryptedKey;
        char **ppHashElements;
        char *strHashValue;
        int rc = 0;
    
        if ( argc < 3 )
        {
            fprintf( stderr, "Usage:  TestHash encryptedKey hashElement...\n" );
            return 1;
        }
    
        strEncryptedKey = argv[ 1 ];
        ppHashElements = &argv[ 2 ];
    
        strHashValue = GenerateHash( ppHashElements, strEncryptedKey );
    That works perfectly...


    And here what I'm tryin to do:

    Code:
    PHP_FUNCTION(hsbc_cpi_make_hash)
    {
    
        char *strEncryptedKey;
        int  strEncryptedKey_len;
        zval *arr, **data;
        HashTable *arr_hash;
        HashPosition pointer;
        char *ppHashElements;
        char *strHashValue;
        int  rc=0;
        int array_count;
        int count = 0;
        int bcount = 0;
    
       if ( zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "as", &arr, &strEncryptedKey, &strEncryptedKey_len) == FAILURE ) {
    	RETURN_NULL();
         }
    
        arr_hash = Z_ARRVAL_P(arr);
        array_count = zend_hash_num_elements(arr_hash);
    
        ppHashElements = malloc(2048);
        if(!ppHashElements){
    	RETURN_NULL();
        }
    
       memset(ppHashElements, 0, 2048);
    
       for(zend_hash_internal_pointer_reset_ex(arr_hash, &pointer);
       zend_hash_get_current_data_ex(arr_hash, (void**) &data, &pointer) == SUCCESS;
       zend_hash_move_forward_ex(arr_hash, &pointer)){
    	
    	count++;
    	zval temp;
    	temp = **data;
    	zval_copy_ctor(&temp);
    	convert_to_string(&temp);
            strcat(ppHashElements, Z_STRVAL(temp));
            if(count < array_count){
    	    strcat(ppHashElements, ", ");
    	}
    	zval_dtor(&temp);
        }
    
        strHashValue = GenerateHash(&ppHashElements, strEncryptedKey );
    
        php_printf("%s\n", strEncryptedKey);
        php_printf("%s\n", strHashValue);
        php_printf("%s\n", ppHashElements);
    
        free(ppHashElements);
    
        if(!strHashValue){
            RETURN_FALSE;
        }
     
        RETURN_STRING(strHashValue, 1);
    
    }
    Last edited by mikep; 07-22-2005 at 10:03 AM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    char *args[2];
    args[0] = yourstring;
    args[1] = NULL;
    
    GenerateHash( args, foo );
    Now you have in effect a duplicate of whatever you say "works perfectly".


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Jul 2005
    Posts
    5
    Quote Originally Posted by quzah
    Code:
    char *args[2];
    args[0] = yourstring;
    args[1] = NULL;
    
    GenerateHash( args, foo );
    Now you have in effect a duplicate of whatever you say "works perfectly".

    Quzah.


    Thanks Quzah... I tried that earlier but with only 1 element in the char * array (char *args[1]). I did not think about needing a null element to terminate the array.

    You led me onto the right path... Very appreciated!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help converting a C function to PHP
    By soadlink in forum C Programming
    Replies: 3
    Last Post: 10-04-2007, 03:51 PM
  2. converting char to int
    By saphiroth in forum C Programming
    Replies: 10
    Last Post: 02-01-2005, 07:48 AM
  3. Converting Sign Magnitude Integer Binary Files
    By Arthur Dent in forum C Programming
    Replies: 7
    Last Post: 09-13-2004, 10:07 AM
  4. Converting from Screen to World Coordinates
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 05-11-2004, 12:51 PM
  5. converting string to float
    By twhubs in forum C Programming
    Replies: 1
    Last Post: 09-16-2001, 09:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21