| |

VerySource

 Forgot password?
 Register
Search
Author: 海里的浪

How does a function return an array of indefinite length?

[Copy link]

0

Threads

53

Posts

42

Credits

Newbie

Rank: 1

Credits
42

 Invalid IP Address

Post time: 2020-5-15 18:00:01
| Show all posts
void fun (unsigned char * in, vector <unsigned char>&out)
Reply

Use magic Report

1

Threads

30

Posts

20

Credits

Newbie

Rank: 1

Credits
20

 China

Post time: 2020-5-27 12:00:01
| Show all posts
int fun (unsigned char * in, unsigned char *&out)
{
    int size;
    for (...) ... determine the length of In
    out = new unsigned char [size]; // allocate new space, pay attention to the out pointer to pass by reference,
----------------------------------- // To change the pointer outside you
    return size; // Return length
}

Arrays of indefinite length, that is, dynamically allocated memory areas, can be pointed to by pointers.
Indefinite length is not to know the length, you know the length when you allocate, but the calling party does not know the length.
Length, then just pass this length out.
Reply

Use magic Report

0

Threads

38

Posts

27

Credits

Newbie

Rank: 1

Credits
27

 China

Post time: 2020-6-7 18:00:01
| Show all posts
Why not use string
Reply

Use magic Report

3

Threads

10

Posts

10

Credits

Newbie

Rank: 1

Credits
10

 China

Post time: 2020-6-9 22:45:01
| Show all posts
struct pointer{
char * a;
int size;
}

pointer foo{
pointer p;
size = 100;
p.a = new char[size];
return p;
}

=======================================
This would be fine
Reply

Use magic Report

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

Points Rules

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

Quick Reply To Top Return to the list