1

I'm trying to do this:

const char *p = "/home/paul";
dp = opendir(*p);

But that fails with the following error:

passing argument 1 of 'opendir' makes pointer from integer without a cast

I'm at a loss here, as far as I know what I'm attempting is perfectly valid. After all, I'm passing a const char to a function who's input is a const char. What am I doing wrong?

user985779
  • 51
  • 1
  • 2
  • 7

3 Answers3

6

The opendir() function accepts a const char * argument, but you're sending it a const char. *p dereferences the value pointed to by p, and returns the first character in the array, which is "/". So the result of *p is the const char value "/".

p however is a const char *, so change that to:

dp = opendir(p);
rid
  • 61,078
  • 31
  • 152
  • 193
0

Your code is failing because you are going indirect through p:

dp = opendir(*p);

Because opendir takes a char * as an argument, and you are telling opendir to look for that char * in the spot where p is pointing, opendir is using "/home/paul" as its char *.

But p is the exact value opendir wants. Instead, say:

dp = opendir(p);

and everything will be smooth as glass.

Pete Wilson
  • 8,610
  • 6
  • 39
  • 51
0
const char *p = "/home/paul";
dp = opendir(*p);

the declaration const char *p = "/home/paul"; means that 'p' is pointing to the start of the string where p is essentially an address in memory where the string is.

when you write *p it means you are accessing the content of where p points which is the first character in the string, namely '/'

AndersK
  • 35,813
  • 6
  • 60
  • 86