Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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