I am trying to swap characters within a string that is a char *. I want the swap function to take in a void *[] to be generic, but I am getting an odd behavior and I am not sure why.
My main:
int main(int argc, char *argv[])
{
size_t n_bytes = 100;
int bytes;
char *my_input;
int numeric = 0;
my_input = (char *) malloc(n_bytes + 1);
/* if -n option is set, sort numerically versus lexicographically. */
if (argc > 1 && strcmp(argv[1], "-n") == 0) numeric = 1;
while(getline(&my_input, &n_bytes, stdin) && strlen(my_input) > 1)
{
swap((void **) my_input, 0, 2);
}
free(my_input);
return 0;
}
swap function with printf-ing for "debugging":
void swap(void *v[], int i, int j)
{
void *temp;
printf("i: %d j: %d\n", i, j);
for(int x = 0; x < strlen(v); x++)
{
printf("%c\n", v[x]);
}
temp = v[i];
v[i] = v[j];
v[j] = temp;
printf("first letter: %c", v[j]);
}
What appears from the loop of printing is the first character of the string the user inputs (Ex. if input == "hello", the first character printed will be 'h'). The rest that is printed is always garbage though instead of the rest of the characters. The printf that prints what is held in v[j]
prints out the swapped value which would be whatever the first character was. I am guessing I am missing a blatant concept, but can't seem to figure it out.