Long time listener, first time caller.
I do apologize if this problem has already been addressed (I imagine that is has been covered extensively), but I've searched through many questions about pointers and other seemingly related topics, but I still am unable to solve my problem.
I'm currently writing a string library for class project, and when I am getting a segmentation fault error when I try this:
#include "str2107.h"
#include <stdio.h>
void lstrip(char *s) {
char *p, *q;
p = q = s;
while (*p == ' ' && *p != '\0') {
*p++;
}
while (*p != '\0') {
*q = *p; //this is where the actual segmentation fault occurs.
p++;
q++;
}
*q = '\0';
}
My main program looks like this:
#include <stdio.h>
#include <stdlib.h>
#include "str2107.h"
int main(int argc, char** argv) {
char *z1 = " weeee";
printf("lstrip function\n");
printf("%s\n",z1);
lstrip(z1);
printf("%s\n",z1);
return 0;
}
(*p == ' ' && *p != '\0'). Under exactly what possible conditions will the first part be true and the second part be false ??? Shorter version:(*p == ' ')