All pastes #822574 Raw Edit

Untitled

public text v1 · immutable
·published 2007-12-18 20:32 UTC
rendered paste body
[vl@home tmp]$ cat test.c
#include <stdio.h>
#include <string.h>
void dump(char **foo);
int main() {
  char bar[16];
  dump(&bar);
  printf("bar: %s\n", bar);
  return 0;
}
void dump(char **foo) {
  strncpy(*foo, "bogus", 5);
}

[vl@home tmp]$ gcc -Wall -o test test.c  && ./test
test.c: In function ‘main’:
test.c:6: warning: passing argument 1 of ‘dump’ from incompatible pointer type
Segmentation fault

---- same with memory allocation ----
$ gcc -Wall -o test test.c  && ./test
bar: bogus
[vl@home tmp]$ cat test.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void dump(char **foo);
int main() {
  char *bar = malloc(16);
  dump(&bar);
  printf("bar: %s\n", bar);
  free(bar);
  return 0;
}
void dump(char **foo) {
  strncpy(*foo, "bogus", 5);
}
[vl@home tmp]$ gcc -Wall -o test test.c  && ./test
bar: bogus