Softwareentwicklung
›
Thread
Pointer als parameter von Funktion zurück
J
Johannes
Gast
16.10.2020 14:19
#6443160
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.
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
Johannes schrieb:
> work(&a[0], sizeof(a), c, &numOfDoubled);
&a[0] ist quatsch, da a[0] bereits die Adresse ist.
AS
A. S.
Gast
16.10.2020 14:38
#6443198
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.
A. S. schrieb:
> &a[0]
ist unnötig, da a (ohne & und [0]) bereits die Adresse ist, aber kein
Quatsch.
Oliver
🐧D
🐧 DPA 🐧
Gast
16.10.2020 14:53
#6443221
X
xy
Gast
16.10.2020 15:00
#6443227
Das 1 for ( uint8_t x = x ; x < numOfA ; x ++ )
müsste 1 for ( uint8_t x = 0 ; x < numOfA ; x ++ )
sein.
J
Johannes
Gast
16.10.2020 15:24
#6443249
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.
↑ Nach oben
← Zurück zu Softwareentwicklung
Antwort schreiben
Eine eigene Frage?
Dieser Thread ist über 6 Jahre alt.
Stell sie in einem eigenen Thread – dort lesen mehr Leute mit, und sie wird öfter und schneller beantwortet.
Neuen Thread starten
Eine Lösung, Ergänzung oder Rückmeldung zu diesem Thread? Dann antworte hier.