Though I don't understand your "moving array 1 time left"
comment, I presume it means rotate all elements to the left by 1 so you end up with "string2", "string3", "string1"
, and then swapping first/last would end up with "string1", "string3", "string2"
(effectively swapping the 2nd and 3rd elements as the final result)
As mentioned, you have an array-of-pointers-to string-literals, so the content of the strings is immutable, but you can swap the order of the pointers. If you take the logic above, and apply the left-rotate and swap first/last your foo1()
function could be:
void foo1 (char **array)
{
/*moving array 1 time left and placing first one at the end*/
char *tmp = array[0];
for (int i = 1; i < LENGTH; i++) /* move all elements left by 1 */
array[i-1] = array[i];
array[LENGTH-1] = tmp;
tmp = array[0]; /* swap 1st and last */
array[0] = array[LENGTH-1];
array[LENGTH-1] = tmp;
}
(note: if you don't need the left-rotate of elements, you can delete that part of the code)
If you add the header required, the full code could be written as:
#include <stdio.h>
#define LENGTH 3
void foo1 (char **array)
{
/*moving array 1 time left and placing first one at the end*/
char *tmp = array[0];
for (int i = 1; i < LENGTH; i++) /* move all elements left by 1 */
array[i-1] = array[i];
array[LENGTH-1] = tmp;
tmp = array[0]; /* swap 1st and last */
array[0] = array[LENGTH-1];
array[LENGTH-1] = tmp;
}
int main (void) {
char *hohoho[] = {"string1", "string2", "string3"};
puts ("original order:");
for (int i=0; i<LENGTH; i++)
puts (hohoho[i]);
foo1 (hohoho);
puts("\norder after rotating left 1 and swapping first/last:");
for (int i=0; i<LENGTH; i++)
puts (hohoho[i]);
printf ("\n%s.\n", hohoho[LENGTH-1]);
}
Example Use/Output
$ ./bin/swap_arr_of_ptrs
original order:
string1
string2
string3
order after rotating left 1 and swapping first/last:
string1
string3
string2
string2.
(no idea why you include the last output, but it was there...)
Look things over and let me know if I have the logic of the "moving array 1 time left"
part wrong or if you have further questions.