-1

I'm trying to test the function below:

uint32_t atoi_len(uint8_t **buf, uint8_t len) {
    uint32_t value = 0;

do {
    if ((**buf < '0') || (**buf > '9')) return 0; 
    value *= 10;
    value += *((*buf)++) - '0';
} while (--len);

return value;
}

I have redefined uint32_t and uint8_t as int and char, just to make it work on my pc (Codeblocks).

What I'm trying to do is define a 2 dimensional array and pass this to this function as first argument (uint8_t **), but nothing works.

uint8_t buffer[10][10] = {"test", "test2", "stuff", "something"};

but i can't call:

atoi(buffer, len); 

not even:

atoi_len(&buffer[0][0], len);
atoi_len(&buffer[0], len);

How can be initialized and accessed by a pointer? How do I pass the pointer to the function?

How can I build a 2 dimensional array, and pass the pointer to this to the function? I'm able to do it in one dimension easily:

char array[30] = "testof the test";
char* str = array; //or char* str = &array[0];

I would like to have this simple method to create a multidimensional array and assign a pointer to it, so that I can access array by pointer and even pass this pointer to a function.

Thank you very much, S

rusty81
  • 39
  • 2
  • 9
  • Thank you. I have problems declaring an array like char array[10][10] = {"df","dsf","det","ewr"}; but then I want a pointer char** buf; to point to array. char** buf = &array[0] OR &array doesn't work. Can you suggest something? thanks – rusty81 Feb 09 '19 at 23:22
  • How much of [this](https://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function)' accepted answer also applies to C? – Phil M Feb 09 '19 at 23:30
  • Are you trying to call `atoi` or `atoi_len`? `atoi` is a library function that takes only one argument. It looks like you intended to call `atoi_len` instead. – Tom Karzes Feb 09 '19 at 23:41
  • Tom yes, just a typo. Thanks – rusty81 Feb 10 '19 at 00:45
  • 1
    Why do you want to pass a 2D-array? The code you show does only operate on the the 1st string. – alk Feb 10 '19 at 09:23

2 Answers2

1

char array[][10] can be passed to a function as char (*buf)[10].
There seemed to be not much point of passing a two dimensional array to a function that only processed the first element, so this takes the argument to indicate the element to process.

#include <stdio.h>
#include <inttypes.h>

uint32_t atoi_len( char (*buf)[10], size_t each) {
    uint32_t value = 0;
    size_t len = 0;

    printf ( "buf[%zu] is %-12s", each, buf[each]);

    while ( buf[each][len]) {//not the zero terminator
        if ((buf[each][len] < '0') || (buf[each][len] > '9')) {
            return 0;
        }
        value *= 10;
        value += buf[each][len] - '0';
        len++;
    }

    return value;
}

int main( void) {
    char array[][10] = { "33", "-43,6,1", "8", "98", "12.3"};

    printf ( "result is %"PRIu32"\n", atoi_len ( array, 0));
    printf ( "result is %"PRIu32"\n", atoi_len ( array, 1));//has - and ,
    printf ( "result is %"PRIu32"\n", atoi_len ( array, 2));
    printf ( "result is %"PRIu32"\n", atoi_len ( array, 3));
    printf ( "result is %"PRIu32"\n", atoi_len ( array, 4));//has .
    return 0;
}
xing
  • 1,924
  • 2
  • 11
  • 10
0

I have solved for the moment with the following code:

Declaring a pointer to array

char *array[] = {"-43,6,-45,1","8","98"};

Passing this to the function in either of the two following ways

atoi_len(array, len);
atoi_len(&(*array), len);

I'm now starting to undestand, I've followed adresses in memory of every variable using Debug and Watch (CodeBlocks).

The last piece is to know if there is a simple/elegant way to do this. Can't use malloc because on embedded hardware i'm not allow to :).

Thanks to everyone.

rusty81
  • 39
  • 2
  • 9