Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 536875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:52:04+00:00 2026-05-13T09:52:04+00:00

i have a string in C which i got from some algorithm. it has

  • 0

i have a string in C which i got from some algorithm. it has numeric values in a format like this

0.100
0.840
0.030
0.460
0.760
-0.090

and so on

in need to store each of these numeric values into a float array for numeric processing. i am new to C and found string handling in C to be complex. can anyone tell me how i might implement this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:52:04+00:00Added an answer on May 13, 2026 at 9:52 am

    Are all the values in a single string, like “0.100 0.840 0.030 …”, or do you have a bunch of separate strings, like “0.100”, “0.840”, “0.030”, etc.? If they’re in a single string, are they separated by whitespace (tabs, spaces, newlines) or by printing characters (comma, semicolon)? Do you know how many values you have ahead of time (i.e., how big your float array needs to be)?

    To convert a string representing a single floating point value to double, use strtod() as follows:

    char valueText[] = "123.45";
    char *unconverted;
    double value;
    
    value = strtod(valueText, &unconverted);
    if (!isspace(*unconverted) && *unconverted!= 0)
    {
      /**
       * Input string contains a character that's not valid
       * in a floating point constant
       */
    }
    

    Read up on strtod() for details. unconverted will point to the first character in the string that wasn’t converted by strtod(); if it isn’t whitespace or 0, then your string isn’t properly formatted for a floating point value and should be rejected.

    If all the values are in a single string, you’re going to have to break the string into distinct tokens. The easy (if somewhat unsafe) way to do this is to use strtok():

    char input[] = "1.2 2.3 3.4 4.5 5.6 6.7 7.8";
    char *delim = " "; // input separated by spaces
    char *token = NULL;
    
    for (token = strtok(input, delim); token != NULL; token = strtok(NULL, delim))
    {
      char *unconverted;
      double value = strtod(token, &unconverted);
      if (!isspace(*unconverted) && *unconverted != 0)
      {
        /**
         * Input string contains a character that's not valid
         * in a floating point constant
         */
      }
    }
    

    Read up on strtok() for details.

    If you don’t know how many values you have up front, you’ll need to do some memory management. You can dynamically allocate an array of float of some initial size using malloc() or realloc(), and then extend it periodically using realloc():

    #define INITIAL_EXTENT 10
    
    double *array = NULL;
    size_t arraySize = 0;
    size_t arrayIdx = 0;
    
    char input[] = ...; // some long input string
    char *delim = ...; // whatever the delimiter set is
    char *token;
    
    /**
     * Create the float array at some initial size
     */
    array = malloc(sizeof *array * INITIAL_EXTENT));
    if (array)
    {
      arraySize = INITIAL_EXTENT;
    }
    /**
     * Loop through your input string
     */
    for (token = strtok(input, delim); token != NULL; token = strtok(NULL, delim))
    {
      double val;
      char *unconverted;
      if (arrayIdx == arraySize)
      {
        /**
         * We've reached the end of the array, so we need to extend it.  
         * A popular approach is to double the array size instead of
         * using a fixed extent; that way we minimize the number
         * of calls to realloc(), which is relatively expensive.  
         *
         * Use a temporary variable to receive the result; that way,
         * if the realloc operation fails, we don't lose our
         * original pointer.
         */
        double *tmp = realloc(array, sizeof *array * (arraySize * 2));
        if (tmp != NULL)
        {
          array = tmp;
          arraySize *= 2;
        }
        else
        {
          /**
           * Memory allocation failed; for this example, we just exit the loop
           */
          fprintf(stderr, "Memory allocation failed; exiting loop\n");
          break;
        }
      }
      /**
       * Convert the next token to a float value
       */
      val = strtod(token, &unconverted);
      if (!isspace(*unconverted) && *unconverted != 0)
      {
        /**
         * Bad input string.  Again, we just bail.
         */
        fprintf(stderr, "\"%s\" is not a valid floating-point number\n", token);
        break;
      }
      else
      {
        array[arrayIdx++] = val;
      }
    }
    

    Don’t forget to free() array when you’re done with it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string which looks like this a 3e,6s,1d,3g,22r,7c 3g,5r,9c 19.3 , how
I've got an object[] array which contains some mixture of builtin types, like Int/Byte/String,
I have this code with the switch statement which I got from this post
Would like some advice from this. I got a table where I want to
I have a string which is in the following format, When there is a
I have a string which typically is in the format : 0xFF . I'll
I have a string which looks like a hash: { :key_a => { :key_1a
I have a string which has the below content String msg = The variables
I have just written some code, which as i was writing i thought, this
I have a complex structure xml from which I need to read some element

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.