| |

VerySource

 Forgot password?
 Register
Search
View: 835|Reply: 5

How does a function return an array of indefinite length?

[Copy link]

3

Threads

10

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-2-23 15:00:01
| Show all posts |Read mode
in is an input parameter
out is the output parameter
outlength is also an output parameter and is the length of the out array
void fun (unsigned char * in, unsigned char * out, int outlength)
{


}

Since I don't know the length of out, how do I pass out?
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-5-9 14:45:01
| Show all posts
Use the address!
Reply

Use magic Report

0

Threads

13

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-5-9 17:00:01
| Show all posts
use
void fun (unsigned char * in, unsigned char *&out, int&outlength)
Reply

Use magic Report

3

Threads

10

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

 Author| Post time: 2020-5-10 11:45:01
| Show all posts
void __fastcall TForm1 :: btn4Click (TObject * Sender)
{

char * a = new char [100];
char * b;
fun (a, b);


}
// ------------------------------------------------ ---------------------------
void TForm1 :: fun (char * src, char *&dest)
{
   int n = 3;
   char * a = new char [n]; // When will it be released?
   a [0] = '1';
   a [1] = '2';
   a [2] = '3';
   dest = a;
     
}
Reply

Use magic Report

0

Threads

45

Posts

32.00

Credits

Newbie

Rank: 1

Credits
32.00

 China

Post time: 2020-5-26 12:00:01
| Show all posts
It will be released when exiting the function,
dest should not return the correct value
Just operate dest directly in the function
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-5-28 19:15:01
| Show all posts
Two parameters are returned, one is the address of the array and one is the length of the array.
Or use std :: vector to return your vector, just call size () to get the data length
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list