0

I'm currently writing an application where I need a C-function to return an array. I've read that C-functions directly can't return arrays but pointers to arrays, anyhow I still don't get it to work. I'm sending a string with several numerical values that I have to put into an array.

My code looks like this, where the main function is:

int main() {
    char arr[3] = {0};
    char *str = "yaw22test242test232";
    foo(arr,3,str);

    printf("%d\n",arr[0]);

    return 0;
}

Where I want the foo function to return an array with the numbers 22, 242 and 232 on array positions 0, 1 and 2 respectively. The algorithm in the foo function works properly when used in the main program but not this way. Is there any way to work around this? What am I doing wrong? The foo function looks as follows:

void foo(char *buf, int count, char *str) {
    char *p = str;
    int k = 0;

    while (*p) { // While there are more characters to process...
        if (isdigit(*p)) { // Upon finding a digit, ...
            double val = strtod(p, &p); // Read a number, ...
            //printf("%f\n", val); // and print it.
            buf[1-count-k] = val;
            k++;
        } else { // Otherwise, move on to the next character.
            p++;
        }
    }
}
3
  • Removed the c++ flasg, sorry! Commented Apr 28, 2015 at 11:20
  • Please clarify how it is not working. What is the desired behaviour, and what does it do instead? Are there any error messages? Commented Apr 28, 2015 at 11:38
  • All it returned was zeroes but it was because I declared the arr variable as a char instead of a double. Now it's working. Commented Apr 28, 2015 at 11:44

3 Answers 3

2

Well you are going out of bounds here:

buf[1-count-k] = val;

perhaps you mean something like buf[k] = val; and a check if( k >= count ) to end the loop.

Since char *buf usually isn't able to represent values larger than 127, you should use an integer type large enough, or a double, otherwise the assignment buf[*] = val; from a type double to a type char, will cause undefined behavior.

Sign up to request clarification or add additional context in comments.

2 Comments

Please note that buf is an array of char, and val is double. So k can't be possible used as a valid index, and buf must be of an apt size, taking into account size difference.
@rubikonx9 k is fine( assuming correct index ), the assignment ( usually ) isn't.
0

It looks like you want to extract the numbers in the string as doubles, but you're trying to store them in a char array. This doesn't even compile.

So, first, use a proper buffer:

int main() {
    double arr[3] = {0};
    /* ... */
}

And update the parameter declaration in foo():

void foo(double *buf, int count,char *str) { ... }

And then fix this:

buf[1-count-k] = val;

You probably want something as simple as:

buf[k++] = val;

Finally, you may want to return k so that the caller has a chance to know how many numbers were written into the array. So, foo would look like this:

size_t foo(double *buf, int count,char *str) {
    char *p = str;
    size_t k = 0;

    while (*p) { // While there are more characters to process...
        if (isdigit(*p)) { // Upon finding a digit, ...
            double val = strtod(p, &p); // Read a number, ...
            //printf("%f\n", val); // and print it.
            buf[k++] = val;
        } else { // Otherwise, move on to the next character.
            p++;
        }
    }
    return k;
}

Note that the correct type to index an array is size_t, and not int. size_t is guaranteed to be wide enough to hold the size of any array, so if you want your code to work with arbitrarily long arrays, size_t should be used to index the array.

1 Comment

I'm gonna try this! Thanks for all the help!
0

I would recommend using a vector-like structure instead of an array. There are many implementations of this already (see GLib lists for C). But if you want to 'roll your own', try something similar to this:

typedef struct
{
    char** data;
    int size;

} str_vector;

Where you can dynamically allocate a str_vector and its data member, and return this. I won't go into too much more detail as there is quite a few tutorials on this on the internet, which I am sure you can bring up in Google/Bing/Whatever in a manner of seconds :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.