| |

VerySource

 Forgot password?
 Register
Search
Author: 海里的浪

Return a variable like char *?

[Copy link]

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-5-2 13:00:01
| Show all posts
Pass the actual parameter to this n
Reply

Use magic Report

3

Threads

10

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 United States

 Author| Post time: 2020-5-3 15:00:02
| Show all posts
How to pass out this parameter?
Reply

Use magic Report

0

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-5-8 21:00:01
| Show all posts
void fun (char * in, char * out),
If you want to allocate space for out in fun, it is very dangerous, because you don't know when to release him.

It depends on what function your fun wants to achieve.
You can do this if you just want to add the most characters.

void fun (char * in, char * out)
{
  int i = strlen (in);
  int i1 = strlen (out);
  char * szBuff = new char [i1 + 1];
  strcpy (szBuff, out);
  if (out) {delete out; out = NULL;}
  out = new char [i + i1 + 1];
  sprintf (out, "% s% s", szBuff, in);
  delete [] szBuff;
}

void main ()
{
  char * out = new char [6];
  strcpy (out, "Hello");
  fun ("LZ", out);
  printf ("% s\n", out);
  delete [] out;
}
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-6-7 10:15:01
| Show all posts
Call a function, pass the address
The pointer is declared before calling the function, and the memory is applied
After calling this function, release it and set it to empty

Otherwise there will be danger
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