As we talked about in the c with a spoon lecture, I tried the
pointer arithmetic and then free and at least for me it didn't
work.
gcc version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)

[EMAIL PROTECTED]:~]$ gcc nis2.c
[EMAIL PROTECTED]:~]$ ./a.out 
char y[0] a
char y[1] b
char y[2] c
char y[3] d
Segmentation fault (core dumped)

#include <stdio.h>
#include <stdlib.h>
main(){
      char *y = (char *)malloc(4*sizeof(char));
      char *x = y + 2;
      y[0]='a';
      y[1]='b';
      y[2]='c';
      y[3]='d';
      printf("char y[0] %c\n", y[0]);
      printf("char y[1] %c\n", y[1]);
      printf("char y[2] %c\n", y[2]);
      printf("char y[3] %c\n", y[3]);

      free(x);

      printf("char y[0] %c\n", y[0]);
      printf("char y[1] %c\n", y[1]);
//     printf("char y[2] %c\n", y[2]);
//     printf("char y[3] %c\n", y[3]);
  
      exit(EXIT_SUCCESS);
}

Regards,
        tzahi.

WARNING TO SPAMMERS:  see at
http://members.lycos.co.uk/my2nis/spamwarning.html



--------------------------------------------------------------------------
Haifa Linux Club Mailing List (http://www.haifux.org)
To unsub send an empty message to [EMAIL PROTECTED]


Reply via email to