Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.48 KB | None | 0 0
  1. int myfn1() {
  2. int i=10;
  3. return i;
  4. }
  5.  
  6. char myfn2() {
  7. char buf[10]="Hello";
  8. return buf;
  9. }
  10.  
  11. int main(){
  12. printf("%dn", myfn1());
  13. printf("%sn", myfn2());
  14. }
  15.  
  16. 10
  17. Segmentation fault
  18.  
  19. char myfn2() {
  20. buf[10]="Hello";
  21. return buf;
  22. }
  23.  
  24. char * myfn2() {
  25. buf[10]="Hello";
  26. return buf;
  27. }
  28.  
  29. int main(void) {
  30. printf("%pn", (void*) myfn2()); // OK - pointer's value is printed
  31. printf("%sn", myfn2()); // Bad - code attempts sting access to invalid pointer.
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement