You don't allocate memory for "temp", thus you can't copy a string to it.Code:void sort_in_order(int argc, char * argv[]) { char * temp; ... strcpy(temp,argv[i]);
But copying the strings is the wrong approach. You need to modify the pointers.
argv[n] is a pointer to a string, i.e. it points to the memory location where the first character of the string is stored. Now it looks like your implementation stores the command line arguments consecutively like:
Now if you swap for example argv[1] and argv[4] like you do in your sort function (using strcpy()) the result will be:Code:b o b \0 d a v i d \0 j o h n \0 a n d r e w \0 ^ ^ ^ ^ 1 2 3 4 (argv-indices)
As you can see you have just swapped the contents, but the pointers (argv[1], argv[2], ...) will still point to the same memory location as before. That's why you print out garbage and why you need to rearrange the pointers instead.Code:a n d r e w \0 i d \0 j o h n \0 b o b \0 e w \0 ^ ^ ^ ^ 1 2 3 4
Bye, Andreas



1Likes
LinkBack URL
About LinkBacks


