I have something that works but I'm still not completely sure why my previous code did not. I think it is because you cannot change the pointer unless you pass a pointer to the pointer.
Code:
#include <stdio.h>
#include <string.h>
#define LIM 10
#define MAX 81
void print_arrs(char *strs[MAX], int num);
void swap_onetwo(char *strs[MAX], int num);
int main(void)
{
char arr[LIM][MAX];
char *strs[LIM];
int cnt = 0;
puts("Enter a string, blank to quit");
while ((gets(arr[cnt]) && arr[cnt][0] != '\0')) {
strs[cnt] = arr[cnt];
if (++cnt == 10)
break;
puts("Enter a string, blank to quit");
}
print_arrs(strs, cnt);
swap_onetwo(strs, cnt);
print_arrs(strs, cnt);
return 0;
}
void swap_onetwo(char *strs[MAX], int num)
{
char *tmp = NULL;
tmp = strs[0];
strs[0] = strs[1];
strs[1] = tmp;
return;
}
void print_arrs(char *strs[MAX], int num)
{
int i;
for (i = 0; i < num; i++)
puts(strs[i]);
return;
}
I've created an array of pointers and set them to point to the beginning of each string. Is this the right way to do it? Thanks.