TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Bo Simonsen
from: Darin McBride
date: 2003-09-01 14:01:48
subject: Pointers

Hello Bo!

Replying to a message of Bo Simonsen to tom_torfs:

 BS> Can't i avoid to have a extra pointer?

Yes.  But not in C.  Okay, that's not entirely true - you'd have to make
significant changes, which are not always an option.  If you wrote both the
caller and callee, then these changes are what I'd suggest anyway, even in
C++.

 KK>> #include 
 KK>> #include 

char* test(char** buf, size_t buflen)

 KK>> {
  /* char* tmp = (char*)malloc(80); */
  /* if(tmp) sprintf(tmp, "test"); */

char* source = "test";
if (buflen > strlen(source))
  sprintf(*buf, source);

  /* return tmp; */
  return *buf;
 KK>> }
 KK>> int main()
 KK>> {

char buf[16];
printf("%s\n", test(buf, sizeof(buf)));

 KK>>    fgets(buf, 16, stdin);
 KK>>    return 0;
 KK>> }

No malloc/free's here.

Darin

---
* Origin: Tanktalus' Tower BBS (1:250/102)
SEEN-BY: 633/267 270
@PATH: 250/102 99 10/345 106/1 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™.