Thread: strstr and strtod .. cant work together

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    9

    strstr and strtod .. cant work together

    Hi again,


    I'd like to covert a string into double. But I found a problem.

    Code:
    // declaration
    char** end_ptr = NULL;
    char* temp  = " 67890";       // there is a space before the numbers
    double  first_part = 0;
    
    
     first_part  = strtod(temp[1],end_ptr);   
    
    // since I dont want a space, let the string point to second place where there is "6". 
     But there is a warning.
    Code:
    >  warning: passing arg 1 of `strtod' makes pointer from integer without a cast...
    Last edited by galaxy_virus; 10-03-2008 at 12:41 AM.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    first_part = strtod(&temp[1],end_ptr);

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    9
    Hello riches,

    Thank you. If my code was so simple as I mentioned above, it works.
    Here are my some messy codes. I dont know where I was wrong.

    Code:
    int string_length =0;
    char** end_ptr = NULL;
    char* tempString = NULL;
    char* second_part_of_tempString = NULL;
    double x = 0;
    
    fucntion print_stack()
    {
           for ( int i = 0; i < 10 ; i++)
          {
    
            string_length = strlen(stack_arrary[i]);    //   "123 456" in stack_arrary[i]
            tempString = malloc(string_length+1);
            strncpy(tempString,stack_arrary[i],string_length);   //  "123 456" in tempString
            tempString[string_length] = '\0';
            second_part_of_tempString = strstr(tempString," ");   // " 456" in second_part
    
             x  = strtod(&second_part_of_tempString[1],end_ptr);
    }
    Code:
    > error
    
    2108 _cygtls::handle_exceptions: Error while dumping state (probably corrupted stack)
    segmentation fault (core dumped)
    Last edited by galaxy_virus; 10-03-2008 at 12:41 AM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    String literals should be const: http://cpwiki.sourceforge.net/Common...kes_and_errors
    Also, fix your indentation and place your reply outside the code tags.
    The code works as expected. I wonder if you actually have something in stack_array[1] and forward? If not, then you would get a big fat error.
    Last edited by Elysia; 10-02-2008 at 08:37 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Read this before you post any more code
    http://cboard.cprogramming.com/showthread.php?t=25765
    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.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    9

    Unhappy

    Hi,

    Thank you all for kind suggestion. But there are still problems.
    I would like to mention that...

    - I push elements into stack from index 0 to MAX_STACK_DEPTH.
    Memory allocation for every index is in push function..

    - And I tried to pop them out from index 0 to MAX_STACK_DEPTH.
    Free memory for every index.


    Code:
    pop_stack_function ()
    
    int i;
    double data_value = 0; 
    char* end_ptr = NULL;
    {
    
      for(i=0; i<MAX_STACK_DEPTH; i++)
       {
    
        if( Stack[i] != NULL)
          {
             
            data_value = strtod(strstr(Stack[i]," "),end_ptr);
        
           [I]I want  Stack to point the place after space. But I dont know how to do. 
    
             Alternatively, I tried store strstr(Stack," ")  substring to char* tempString. 
             But I need to do malloc for that. Then error occurs.  
    
            .........
                 .........
            free(Stack[i]);
          }
    
       }
    
    }
    Any help would be much appreciated!
    Please help me. It took me two days.
    Last edited by galaxy_virus; 10-03-2008 at 12:38 AM.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    9
    Quote Originally Posted by Salem View Post
    Read this before you post any more code
    http://cboard.cprogramming.com/showthread.php?t=25765
    Is my previous post correct? I put it in code tag. But it is different with others'.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It's an improvement, thanks.
    But putting them around just the code (and not your descriptive text) would be better. And you're allowed to tag independent blocks as well.
    Code:
    some code
    Code:
    some more code
    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.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    9

    Talking

    Quote Originally Posted by Salem View Post
    It's an improvement, thanks.
    But putting them around just the code (and not your descriptive text) would be better. And you're allowed to tag independent blocks as well.
    Thanks for your kindness.
    I feel like I am learning C and HTML at the same time.

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    - I push elements into stack from index 0 to MAX_STACK_DEPTH.
    Memory allocation for every index is in push function..

    - And I tried to pop them out from index 0 to MAX_STACK_DEPTH.
    Free memory for every index.
    Have you considered allocating the entire stack depth at once, and then freeing the whole thing when you are done with the stack? That seems to be the more conventional, simpler method.

    I want Stack to point the place after space. But I dont know how to do.
    There is a trim operation that you can do to get rid of the spaces by pushing the string to the left. Here's a quick hack at it.
    Code:
    #include <string.h>
    #include <ctype.h>
    
    char * trimleft (char * body)
    {
        size_t i = 0, length;
        
        while (body && body[i] != '\0' && isspace(body[i])) i++;
    
        if (body && i != 0) {
            length = strlen(body);
            memmove(body, &body[i], length - i);
            body[length - i] = '\0';
        }
        
        return body;
    }
    Afterwards your strings will be ready for conversion and there is less "voodoo" involved.
    Last edited by whiteflags; 10-03-2008 at 02:15 AM.

Popular pages Recent additions subscribe to a feed