Forum: PC-Programmierung Pointer als parameter von Funktion zurück


von Johannes (Gast)


Lesenswert?

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.

von Oliver S. (oliverso)


Lesenswert?

Parameter werden in C grundsätzlich und ohne Ausnahme als Kopie 
übergeben.

Willst du einen übergebene Variable in einer Funktion ändern, musst du 
also einen Pointer drauf übergeben, und über den auf die Variable 
außerhalb der Funktion zugreifen. Dabei ist es egal, was für ein Typ das 
ist.

Oliver

von Teo D. (teoderix)


Lesenswert?

Johannes schrieb:
> work(&a[0], sizeof(a), c, &numOfDoubled);

&a[0] ist quatsch, da a[0] bereits die Adresse ist.

von A. S. (Gast)


Lesenswert?

Teo D. schrieb:
> &a[0] ist quatsch, da a[0] bereits die Adresse ist.

&a[0] ist quatsch, da a (ohne & und [0]) bereits die Adresse ist.

von Oliver S. (oliverso)


Lesenswert?

A. S. schrieb:
> &a[0]

ist unnötig, da a (ohne & und [0]) bereits die Adresse ist, aber kein 
Quatsch.

Oliver

von 🐧 DPA 🐧 (Gast)


Lesenswert?


von xy (Gast)


Lesenswert?

Das
1
 for(uint8_t x=x; x<numOfA; x++)
müsste
1
 for(uint8_t x=0; x<numOfA; x++)
sein.

von Johannes (Gast)


Lesenswert?

Habe es jetzt doch lösen können
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* numOfElRes)
26
{
27
  uint8_t counter = 0;
28
29
  *resArray = malloc(numOfA * sizeof(resArray));
30
31
32
  for(uint8_t x=0; x<numOfA; x++)
33
  {
34
    if(a[x] <= 100)
35
    {
36
      (*resArray)[x] = a[x] * 2;
37
      counter++;
38
    }
39
    else
40
    {
41
      (*resArray)[x] = a[x];
42
    }
43
  }
44
45
  *numOfElRes = counter;
46
}

wichtig ist, dass
1
(*resArray)[x] = a[x] * 2;
2
..
3
(*resArray)[x] = a[x];
eine Klammer um *resArray ist.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.