In the function last_letter() The line c = NULL will cause this program to segfault at while (*c) Commenting it out will not. What's the reason? Setting char pointer to NULL and reassigning something to it? I thought setting pointers to NULL and reassigning them to something else was acceptable?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
char s1[] = "abcdefg";
char s2[] = "xyz";
char* c;
void last_letter(char* a, int i) {
printf("last_letter ( a is %s and i is %d)\n", a, i);
sleep(i);
c = NULL; // comment out, what is different?
sleep(i);
c = a;
sleep(i);
while (*c) {
c++;
}
printf("%c\n", *(c-1));
return;
}
// This function will run concurrently.
void* aa(void *ptr) {
last_letter(s2, 2);
return NULL;
}
int main() {
pthread_t t1;
int iret1 = pthread_create(&t1, NULL, aa, NULL);
if (iret1) {
fprintf(stderr,"Cannot create thread, rc=%d\n", iret1);
}
last_letter(s1, 5);
sleep(10);
printf("Ended nicely this time\n");
return 0; //never reached when c = NULL is not commented out.
}
cto NULL then the other thread might try to use*cwhilecis NULL.