• 13
• 15
• 27
• 9
• 9

At a loss with printf...

This topic is 4137 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Alright, humbling question:
char* str1 = "Chris";

..set obj to point to str1 ...

printf("Index %d is %s ",i,*(char*)hash->HT.elms.obj);


Where obj is a void*. This works if I use ints (and change the nessessary params), but just up and seg faults with char* s. Any help would be slick, as I know its something stupid, but I dunno...lets blame it on being sleepy :P

Share on other sites
*(char*)hash->HT.elms.obj
|
\--- Right here is where you dereference obj from (char *) to char. That means its no longer pointing to an array of chars. Take that out and it should work.

Share on other sites
Yes of course, because it wants a pointer...

much appreciated man! Cheers

Share on other sites
I would argue that one is always "at a loss" when one uses printf(), and this is one reason why. :)

Share on other sites
Quote:
 Original post by ZahlmanI would argue that one is always "at a loss" when one uses printf(), and this is one reason why. :)

Well in C it's pretty much the best there is, and we have no reason to assume he isn't using C (didn't tell us the language, the code is valid C and printf is in namespace std in C++). If you're arguing that C++ streams are better than printf, then printf does also have some advantages over C++ streams* (which is one of the reasons for Boost.Format).

* Herb Sutter wrote something about it here and Andrei Alexandrescu here

Share on other sites
my compiler (g++) tells me when I pass a string but should be passing an int or whatever. So type safety is a redundant argument too imo.