Code:
char a = "a";
This is not correct and does not do what you want, you end truncating a pointer to 8-bits and the value is stored in the variable a, as "a" denotes a pointer to the string "a\0", what you probably meant was:
Code:
char a = 'a';
Code:
void main(int argc, char *argv[]) {
main never returns void, it always returns int, if you turned on warnings (which you should be doing, especially considering your being unfamiliar with the language), you would get a warning here. int is the only ISO/IEC standards compliant return value.
Code:
char buff1[] = "some long string";
char buff2[256];
strncpy(buff2,buff1,200);
char buff2[256];
strncpy(buff2,buff1,200);
why the hell are you copying 200 bytes? "some long string" is no where close to 200 bytes regardless of character set. You actually read outside the bounds of buff1, which can have any number of potential implications, most likely that you potentially leak data you didnt mean to (i.e. stack cookie), or potentially crash due this bad read. This code should read:
Code:
unsigned char buff0[] = "some long string";
unsigned char buff1[256];
memset(&buf1, 0, sizeof(buff1));
strncpy(buf1, buf0, (sizeof(buff0) < sizeof(buff1)-1 ? sizeof(buf0) : sizeof(buff1)-1));
unsigned char buff1[256];
memset(&buf1, 0, sizeof(buff1));
strncpy(buf1, buf0, (sizeof(buff0) < sizeof(buff1)-1 ? sizeof(buf0) : sizeof(buff1)-1));
Code:
char buff[5];
[..]
//Read the first four bytes. If fgets fails, print a message.
if(!fgets(buff,sizeof(buff),fp)){
[..]
//Read the first four bytes. If fgets fails, print a message.
if(!fgets(buff,sizeof(buff),fp)){
This actually reads 5 bytes.
Finally, don't return 0/-1/et cetera, return EXIT_SUCCESS/EXIT_FAILURE, while it's likely that these values expand to 0/-1 they don't necessarily. Please go read the standard and use the language for longer than a week before attempting to teach people C incorrectly. kthx







Forum



Programming : not static?
Forensics : The Julie Amero Case: A Dangerous Farce



