Here's a small snippet from my code. Following a Knuth Shuffle model in the Shuffle function, I get 6 Pointer Expected errors in line 15, but I'm sure its really 2 due to the SWAP function in the header. Now the two things I'm swapping are a random index and the i-1 index from the same character array. I'm just confused why a character array isn't being recognized as a pointer already.

#include <stdlib.h>
#include <string.h>
#define SWAP(a,b) {a^=b;b^=a;a^=b;}
#define MAX 60
#define MIN 10

int i;
char input[MAX];

int shuffle (char input)
{
    for (i=strlen(&input); i>1; i--)
    {
        SWAP(input[i-1], input[rand()%i]);    // <- This is where the 6 (or 2) Pointer Expectations occur
    }
}

int main (void)
{

    printf("Please enter a string greater than 10 letters: \n");
    scanf(" %s", &input);
    printf("You entered:\n");
    printf("%s\n", input);
    printf("Here's your shuffled string:\n");
    printf("%d\n", shuffle(*input));

return 0;

}

Recommended Answers

All 2 Replies

why a character array isn't being recognized as a pointer already.
The reason is that you have hidden the global char array named "input" with the local parameter with the same name. In such cases the compiler ignores the global variable.

To correct the problem delete the global array and make it local to main(), then on line 10 make the parameter an array.

The code you posted didn't compile.

test.c: In function ‘shuffle’:
test.c:14: error: subscripted value is neither array nor pointer
test.c:14: error: subscripted value is neither array nor pointer
test.c:14: error: subscripted value is neither array nor pointer
test.c:14: error: subscripted value is neither array nor pointer
test.c:14: error: subscripted value is neither array nor pointer
test.c:14: error: subscripted value is neither array nor pointer
test.c: In function ‘main’:
test.c:21: warning: incompatible implicit declaration of built-in function ‘printf’
test.c:22: warning: incompatible implicit declaration of built-in function ‘scanf’
test.c:22: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[60]’

So I changed some things:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define SWAP(a,b) {a^=b;b^=a;a^=b;}
#define MAX 60
#define MIN 10
//int i;             <------------------ Not 
//char input[MAX];   <------------------   needed to be global
void shuffle (char *input) {    // <---- Want to pass a pointer to char not char
  int i,len=strlen(input);
  for (i=len; i>1; i--) {
      SWAP(input[i-1], input[rand()%len]);  // <---- I think you might want this to always be len
  }
}

int main (void) {
  char input[MAX];   // <---- Made local
  printf("Please enter a string greater than 10 letters: \n");
  scanf("%s", input);  // <----------------------- Want to use input(char*) not &input(char**)
  printf("You entered:\n");
  printf("%s\n", input);
  printf("Here's your shuffled string:\n");
  shuffle(input);
  printf("%s\n", input);  // <------- Want to print output not the return value from shuffle
  return 0;               //          Just want pointer to char passed in, not just one char.
}

Output:

$ ./a.out
Please enter a string greater than 10 letters: 
ThisIsaTestToCheckThingsOut
You entered:
ThisIsaTestToCheckThingsOut
Here's your shuffled string:
Tf@^w|r~IcRIasg^otDlDbD
$
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.