Re: Feedback on C code?

@37, no, that only works on static arrays (allocated during compile time), and isn't actually correct. This is how you do it for static arrays:

//...
const char* versions[5] = {"1.0", "1.1", "1.2", "1.3", "1.4"};
// sizeof(versions) / sizeof(versions[0]) == 5

This, however, is not correct, and is an error in GCC (-Wall -Wextra -Werror):

char** versions = malloc(5*sizeof(char*));
for (int i = 0; i < 4; ++i)
versions[i] = malloc(4*sizeof(char));

strncpy(versions[0], "1.0", 4);
//...
// sizeof(versions) / sizeof(versions[0]) == error

Notice the use of the malloc and strncpy functions. If you don't know, malloc allocates uninitialized memory, and strncpy copies characters from a source to a destination. strncpy is a (slightly safer) alternative to strcpy, which is less safe and does no bounds checking. strncpy requires that you pass in the length of the string your copying, and will only copy the amount you specify. Same with functions like snprintf vs. sprintf and strncat vs. strcat. On the links I've pointed you to you will notice functions like strcat_s, strncat_s, etc. Don't use these functions; only Windows actually implements them (and they don't exactly offer any advantages, though they claim to). You can either start using functions like these now or later; you might've already started.

-- 
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector

Reply via email to