0
char * players[3]={"Player 1","Player 2","Player 3";
char *p = *players;
char *temp;
for(int t = 0; t < 2; t++)
     {
         temp = *(p+t);
         *(p+t) = *(p+t+1);
         *(p+t+1) = temp;
     }

How can I resort the array using pointers only ? I wan't it to be like this : "Player 2","Player 3", "Player 1"

4
  • Possible duplicate of C library function to do sort Commented Dec 11, 2018 at 1:30
  • 3
    Resort based on what ordering ? Or do you mean reorder by rotating everything left, and the initial left-most pointer placed on the sequence tail ? Commented Dec 11, 2018 at 1:34
  • 1
    qsort() only uses pointers. Oh, and the code is missing a } on line 1. Commented Dec 11, 2018 at 1:35
  • I wanna reorder by replacing every element with the right element next to it. @WhozCraig Commented Dec 11, 2018 at 1:44

1 Answer 1

1

It looks like you want to do a left-shift (rotation).
This is a pretty simple operation

  • Keep a pointer to the head
  • For every element, make that spot point to the next element
  • put the head on the tail

The Code:

#include <stdio.h>

void rotateLeft( char **arr, int elements )
{
    // keep a pointer to the head
    char *head = arr[0];

    // shift every element left
    for (int i=0; i<(elements-1); i++)
    {
        arr[i] = arr[i+1];
    }

    // put the head on the tail
    arr[elements-1] = head;
}

// EDIT - rotate without [] notation.
void rotateLeftNoArrayNotation( char **arr, int elements )
{
    // keep a pointer to the head
    char *head = *arr; //arr[0];

    // shift every element left
    for (int i=0; i<(elements-1); i++)
    {
        *(arr+i) = *(arr+i+1); //arr[i] = arr[i+1];
    }

    // put the head on the tail
    *(arr+elements-1) = head; //arr[elements-1] = head;
}

int main( void )
{
    char *players[3] = { "Player 1", "Player 2", "Player 3" };

    for (int i=0; i<3; i++)
        printf( "players[%u] = \"%s\"\n", i, players[i] );

    rotateLeftNoArrayNotation( players, 3 );

    for (int i=0; i<3; i++)
        printf( "players[%u] = \"%s\"\n", i, players[i] );

    return 0;
}

Which gives:

$ ./rotLeft
players[0] = "Player 1"
players[1] = "Player 2"
players[2] = "Player 3"
players[0] = "Player 2"
players[1] = "Player 3"
players[2] = "Player 1"
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, exactly, But I wanna do it using pointers, without using any of [ ].
@YamenMassalha subscripting via [ ] is just as applicable against a pointer as it is against an array (which converts to pointer-to-first-element when use in an expression context, i.e. almost everywhere except decl). The expression *(p+i) is synonymous with p[i] whether p is an array or a pointer. So the statement "I wanna do it using pointers" is irrelevant. what you're really saying is you want to do it without using array subscript notation (apparently), applied anywhere (pointer or otherwise). Is that correct ?
@YamenMassalha - I updated the answer, see rotateLeftNoArrayNotation() - basically it's the same function, slightly different syntax. Syntax that would not pass a code review ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.