2

I have this array "r->phy_addr" defined as follows:

int array [r->numpgs];
r->phy_addr= &array[0];

now I want to pass element zero of the array to a function that takes "int" as an arugument:

add(int x){};

if I do like this

add(r->phy_addr[0]);

then an error of "can't pass a pointer as an int"

How can I do it?

6
  • Have you tried explicitly casting the argument: add((int)r->phy_addr[0]); Commented Dec 5, 2012 at 0:22
  • @Fred yes it doesn't work :( Commented Dec 5, 2012 at 0:23
  • 3
    What's the type of phy_addr ? Commented Dec 5, 2012 at 0:24
  • 4
    what is phy_addr declared as? Commented Dec 5, 2012 at 0:24
  • In case it wasn't obvious. when it comes to questions of pointers and their assignments and dereferencing, if you get errors like this, you probably have a pointer-type wrong. But we can't tell for sure without you showing us the type declarations of the aforementioned variables. Commented Dec 5, 2012 at 0:49

4 Answers 4

3

If the compiler complains about r->phy_addr[0] beng a pointer, then r->phy_addr must have pointer-to-pointer type (int ** most likely). In that case your problems begin even earlier. This

r->phy_addr = &array[0];

is already invalid, since you are assigning int * value to a int ** object. The compiler should have told you about it.

In other words, your call to add is not a problem. Forget about the call to add. It is too early to even look at that call. You have other problems that have to be solved well before that one.

What is r->phy_addr? Is it supposed to have int ** type? If so, why are you assigning it as shown above? Why are you ignoring compiler diagnostic messages triggered by that invalid assignment?

Sign up to request clarification or add additional context in comments.

Comments

2

try add(*(r->phy_addr[0])); it should work

1 Comment

"it should work" No it shouldn't. In order for this to work, phy_addr needs to be an int **, i.e. it must point to an int *. There is no int * anywhere in the program it could possibly point to. Moreover, in the code shown, phy_addr is assigned to &array[0];, i.e. it points to the first element of array, which is an int. *(r->phy_addr[0]) as you suggest would dereference phy_addr twice, which would try to dereference the first element of array (an int) as if it were a pointer. This cannot possibly be good.
1

I suspect that r->phy_addr has int ** type. So r->phy_addr[0] gets int * type, and then compiler warns you that the argument should be int type, rather than int * type.

Change the type of r->phy_addr to int *.

Thanks.

Comments

0

How about:

add(*(r->phy_addr[0]));

? Since, apparently, r->phy_addr[0] is a pointer.

4 Comments

The extra parens are not needed.
@ouah I follow a simple rule - if I'm not sure, I add parens. :)
There is a simple rule: postfix operators always have higher precedence than unary operators.
I like the parens here, I would keep it for the benefit of devs like me who don't memorize precedence rules

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.