TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Bo Simonsen
from: Jasen Betts
date: 2003-09-02 06:34:12
subject: Pointers

Hi Bo.

31-Aug-03 23:49:32, Bo Simonsen wrote to All


 BS>         tmp = (char *) malloc(80);
 BS>         sprintf(tmp, "test");

could be wise to  do

 if (NULL != (tmp=(char*) malloc(80)) // check for available memory
   sprintf(tmp,"%.79S","test");   // protect against
overrun (not needed here)



 BS> int main()  {   printf("%s", test()); }

 BS> How do i free the memory i allocated for tmp?

 int main()
 {
         char *ptr;
         ptr=test();
         printf("%s",ptr);
         free(ptr);
 return 0;
 }


you can do
  char *ptr=test();
 or
  printf("%s",ptr=test());

to reduce the number of lines needed but you will need to use a variable
to hold the resuult from test() so you can both use it and free it.

 -=> Bye <=-

---
* Origin: I smell a rat. Did you bake it or fry it? (3:640/1042)
SEEN-BY: 633/267 270
@PATH: 640/1042 531 954 774/605 123/500 106/2000 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.