• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

passing an address vs. passing a pointer

I'm coming from a C++ background. In main, I have the following:

vector * v;
test(v);

where the function is:

void test(vector * v)

Would the effect of passing:

vector * v;
test(&v);

where the function is:

void test(vector * v)

be the same? What's happening in each case?
0
arabiafish
Asked:
arabiafish
  • 2
1 Solution
 
Harisha M GCommented:
Hi arabiafish,
   
Are you asking


vector *v;
test(v);

is same as

vector v;
test(&v);

??

They are both same..

*v is a vector in first case... => v is a &vector
v is a vector in second case... => &v is a &vector

Note that * &  are inverse operators

Bye
---
Harish
0
 
efnCommented:
No, they would not be the same.

v is a pointer to a vector.  test expects a pointer to a vector.  So test(v) should work.

&v is a pointer to a pointer to a vector.  A pointer to a pointer to a vector is not the same thing as a pointer to a vector.  test(&v) should at least get a warning from the compiler.  Even if it compiles, it is unlikely to work.

With the & operator, you take the address of something.  You can get the address of a pointer just the same as the address of any other object.

You haven't specified what a vector actually is.  There is some dodginess with arrays.  I think if a is an array, passing a as a parameter is the same as passing &a.  If this is of interest to you, I can confirm it, or perhaps someone else will.  But even if vector is a typedef for an array, a pointer to an array is not the same as a pointer to a pointer to an array.
0
 
Harisha M GCommented:
efn... I think you did not understand the question properly...
He asked for entirely different code styles..

Thanks arabiafish !
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now