We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,756 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Persistent Pointer Expected error

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;

}
3
Contributors
2
Replies
56 Minutes
Discussion Span
1 Year Ago
Last Updated
3
Views
DJXiej
Newbie Poster
1 post since Mar 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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.

Ancient Dragon
Achieved Level 70
Team Colleague
32,116 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,575
Skill Endorsements: 69

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
$
histrungalot
Posting Whiz in Training
280 posts since May 2008
Reputation Points: 76
Solved Threads: 36
Skill Endorsements: 1

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0616 seconds using 2.74MB