简体   繁体   中英

double pointers array to function initialization

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

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;
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM