Advertisement

about pointers and functions

Started by January 29, 2000 09:05 AM
17 comments, last by bosjoh 24 years, 10 months ago

The reason this isn''t working is because you''re
using the same buffer for intermediate results and
your final result. In general, using a global
variable to return the result of your function isn''t
a good idea.

I think your best bet is to just use sprintf and
a local buffer.

If you absolutely have to do it as it''s own function,
you''re going to have to use "void valstr(int,char*)"
(or perhaps "int valstr(int,char*)" so you can return
an error code in case of failure). And you shouldn''t
think of this as inconvenient. It''s flexibility -
you pass in the buffer you want your final result placed
in and the function puts it there. If you use a global
buffer for a return value, you have to use strcpy or
some other method to save the result before you can call
your function again with a different value.
bosjoh, your stringadd function and your val_to_str function are using the same buffer! Of course you''re going to get garbage as your intermediate results overwrite themselves! Try:

char * val_to_str(int val) {
static char buffer[256];
sprintf(buffer, "%d", val);
return buffer;
}

This will work with your stringadd function.
When I was saying static buffer I meant local char array defined with the static keyword, not a global array.
Advertisement
In SirCrane''s example, correct me if I am wrong but you don''t need the static keyword. Take the following code.

char *val_to_str(int val) {
char buffer[50];
sprintf(buffer, "%d", val);
return buffer;
}
void Foo() {
char buffer[50]; //This number is huge no need for 256
strcpy(buffer, val_to_str(30));
}

This will create local buffers in each function. It adheres to scope better than a static variable. I do understand that it is most likely slower because of the double copy(once in val_to_str and once in Foo).

Derek
Derek Licciardi (Kressilac)Elysian Productions Inc.
Sorry Kressilac, but I do believe you are wrong. Without
the static keyword, the buffer in the function ceases to
exist when the function is over. So you end up returning
a pointer to nothing.
I think it''s better to use char *valstr(int,char *) like lordfoul said.
Juggling with all these pointers take to much trouble. Also there is a genius invention in C++ named function overloading (great for my graphics textwrite functions).
Don''t need to write one yourself! Just use ltoa(), itoa(), atoi(), etc. That''s what they''re for. Look in the docs for more information.

Good Luck!


- null_pointer
Advertisement
quote:
Don''t need to write one yourself! Just use ltoa(), itoa(), atoi(), etc. That''s what they''re for. Look in the docs for more information.

Ahhh, but what''s that smell? *sniff sniff* I do believe it''s a HOMEWORK assignment!! Mayhaps that''s the reason the standard library calls weren''t used in the first place.

Just a possibility.
First of all, I''m writing my own I/O library for dos. In that I only use malloc as a library, so that''s why I''m not using itoa() and atoi(). And another thing, these functions doesn''t seem to work well in DJGPP.
Well I personally have had no problems with thoose functions in djgpp. However I haven''t used that compiler in along time(Since I dumped allegro for windows programming/Directx,and MSVC) so perhaps that has changed.

This topic is closed to new replies.

Advertisement