Hallo,
ich möchte gerne in einer Funktion ein Array erzeugen und diesen als
Parameter wieder zurückgeben.
1 | static void work(uint8_t* a, uint8_t numOfA, uint8_t* resArray, uint8_t* doubled);
|
2 |
|
3 | int main(void) {
|
4 |
|
5 | static uint8_t a[4] = {0};
|
6 | static uint8_t* c = NULL;
|
7 | static uint8_t d;
|
8 | static uint8_t numOfDoubled = 0;
|
9 |
|
10 | for(uint8_t x=0; x<4; x++)
|
11 | {
|
12 | a[x] = 0x10;
|
13 | }
|
14 |
|
15 | work(&a[0], sizeof(a), c, &numOfDoubled);
|
16 |
|
17 | for(uint8_t x=0; x<numOfDoubled; x++)
|
18 | {
|
19 | printf("%02X ", c[x]);
|
20 | }
|
21 |
|
22 | return EXIT_SUCCESS;
|
23 | }
|
24 |
|
25 | static void work(uint8_t* a, uint8_t numOfA, uint8_t* resArray, uint8_t* doubled)
|
26 | {
|
27 | uint8_t* res;
|
28 | uint8_t counter = 0;
|
29 |
|
30 | res = malloc(numOfA * sizeof(*res));
|
31 |
|
32 | for(uint8_t x=x; x<numOfA; x++)
|
33 | {
|
34 | if(a[x] <= 100)
|
35 | {
|
36 | res[x] = a[x] * 2;
|
37 | counter++;
|
38 | }
|
39 | else
|
40 | {
|
41 | res[x] = a[x];
|
42 | }
|
43 | }
|
44 |
|
45 | *doubled= counter;
|
46 | resArray = res;
|
47 | }
|
Allerdings ist c nach aufruf der Funktion immer noch NULL.
Wenn ich die Funktion allerdings wie folgt definiere
1 | static uint8_t* work(uint8_t* a, uint8_t numOfA, uint8_t* doubled)
|
und wie folgt aufrufe
1 | c = addition(&a[0], sizeof(a), &numOfDoubled);
|
ist c danach als array vorhanden.
Gerne würde ich aber später als returnwerte parameter übergeben, wo man
ermitteln kann, ob die Funktion richtig durchgelaufen ist.
Die funktion work wird dann noch erweitert. Aber erst wenn ich dieses
Problem gelöst habe.