...
Code Block |
---|
#define CONDITION_CHECK 42 void func1 (int arg) { int i = arg; } void func2 (void) { int j; if (j == CONDITION_CHECK) printf("Condition passed!!\n"); else printf("ERROR: Condition failed\n"); } int main(int argc, char **argv) { func1(atoi(argv[1])); func2(); } |
...
Code Block |
---|
#define BUF_SIZE 150 void runit(void) { char buf[50]; char *ptr; memset(buf,0,50); strcpy(buf,ptr); } void logit(char *str, int size) { char buffer[BUF_SIZE]; int i; for (i=0; i < BUF_SIZE; ++i) buffer[i] = '\0'; strncpy(buffer, str, size); printf("The message: %s\nhas been logged\n",buffer); } int main(int argc, char *argv[]) { char buf[BUF_SIZE]; int i; for (i=0; i < BUF_SIZE; ++i) buf[i] = '\0'; strncpy(buf, argv[1], BUF_SIZE-1); logit(buf,strlen(buf)); runit(); } |
...
Code Block |
---|
#define BUF_SIZE 150 void runit(void) { char buf[50]; char *ptr = 0; /* initialize ptr to 0 */ memset(buf,0,50); strcpy(buf,ptr); } void logit(char *str, int size) { char buffer[BUF_SIZE]; int i; for (i=0; i < BUF_SIZE; ++i) buffer[i] = '\0'; strncpy(buffer, str, size); printf("The message: %s\nhas been logged\n",buffer); } int main(int argc, char *argv[]) { char buf[BUF_SIZE]; int i; for (i=0; i < BUF_SIZE; ++i) buf[i] = '\0'; strncpy(buf, argv[1], BUF_SIZE-1); logit(buf,strlen(buf)); runit(); } |
References
ISO/IEC 9899 Section 6.7.8 Initialization