Cope letters until space is reached

So I have this string: char *string = "amiral 1 1 H";
I want it to basically copy 'amiral' or all the letters until it reaches the first space. I have made the following while loop but for some reason it only crashes my program.
1
2
3
4
5
 q = 0;
    while(string[q] == ' ' ){
    tempstring[q] = string[q];
    q++;
    }


Any ideas why its not working?
Last edited on
I expect it's crashing because you're writing over memory that isn't yours.

What have you allocated for tempstring?
No I have not, I will try that and see if it solves my problem.
If you did not allocate any memory for tempstring, then you are pointing to random memory. If you write over random memory, your programme will crash if you are lucky, and will just be wrong if you are unlucky.
Thank you very much Moschops! That was the problem and the solution worked!
Topic archived. No new replies allowed.