Paul Wratt wrote:
When I came from PHP to C (GEM Instant Messenger), I had a really tough time wrapping my head around pointers, etc. Not so much the concept of them, but really how to use them properly, how they are passed and returned in and out of functions, etc. It did and still annoys me to this day that nobody has wrapped standard app memory and string handling into a library so I moved on to C++ which is supported everywhere yet has these things taken care of. I would use c# if mono would build but it doesn't for numerous reasons... Just as well, it'd probably be pretty slow anyway. In C# you can pretty much pass by value everywhere and the "expected effect" is what happens, same as in PHP. It's wasteful but it is much cleaner in my brain. For instance if I have an object that I want to modify the contents of in some way, in C you would tend to pass the pointer so as not to make a copy of it. You can easily do this in c# as well but I tend to make the function return a new instance of the object and just doOn Sat, Dec 5, 2009 at 11:38 PM, Kåre Andersen <kareandersen@gmail.com> wrote:On Sat, Dec 5, 2009 at 12:21 PM, Paul Wratt <paul.wratt@gmail.com> wrote:Also, can anyone tell me how to initialize a CHAR var when you dont know what the max length will. I have had no success in either a net search, or looking through the code (for other uses of it)A char is exactly one byte. In C, strings are made with arrays of type char - that means what you are looking for is not char, but pointer to char! Dynamically sized strings can be made with malloc. There is also a bunch of functions with names starting with str, like strcpy, which means there is no need to reinvent a bunch of wheels for each piece of software written. Please make yourself familiar with pointers and the standard libraries. Vast amounts of documentation on this on the web... One suggestion for reading is http://publications.gbdirect.co.uk/c_book/chapter5/character_handling.html - although knowing pointers and arrays first is probably a good idea. Also, any set of *nix manpages will be a great asset (esp. for the string functions). Best of luck :) /KåreThanks dude I am aware of the "str" functions, having seen them used in other XaAES code, they are pretty much like php, which is handy to know. I tried grabbing a C cheat sheet, but they are somewhat limited in what they contain. I saw the kernel compile a "strstr" and "strrstr" object, which is how php does it (when not using alias's for them). I know about pointers, for what I am doing, if its not "char *temp_file_name;" it wont compile. The problem is that it shows a warning about that var "possibly uninitialized" (I think because it is "hidden" in an if block). I am using this particular var name in other functions, so I guess it would be OK to declare it as "static char *temp_file_name[256];" outside of any functions (its a file name with path info).
object=modify_object(object);Which has the overhead of making 2 new copies and leaving garbage collection to pick up the remnants.
Anyway, if you want to placate the compiler you can just do; char *foo = null; if (testcase) foo=malloc(sizeof(char)*12345); else foo=malloc(sizeof(char)*123456);This is the whole problem with C. You have to do bounds checking and everything else to make sure your buffers aren't being overflowed. These days I've been warming up to doing all that stuff my way though. But this is the advantage of C. It gets people thinking in terms of instruction by instruction waste and makes them program more efficiently. But like I said, I have to think and behave very differently when it comes to writing a line of business app at work (get it done FAST, ignoring most efficiency issues just to make sure the data is accurate) or a GEM app (efficiency is critical and any mistakes will show through because your prog will be slower than a similar competitor.)
You should read the K&R C book. Google it you should find it. Those guys are the creators of C originally IIRC.
Thanks, Mark