What's the difference between "mytype* var" and "mytype * var"

D

daniele.g

I though the two form are equivalent, but when I look at this code some
doubt arises:

CvMat * tmp, * q3;
....
cvCopy(q3, tmp, 0);

but elsewhere

CvMat* tmp;
IplImage * complexInput;
....
cvCopy( complexInput, &tmp, NULL );

Here, CvMat is a general matrix, IplImage is an image which derives from
CvMat.
The point is the use of tmp and &tmp indistinctly.

Any clue?
Thanks in advance
 
J

Joe Pfeiffer

I though the two form are equivalent, but when I look at this code some
doubt arises:

CvMat * tmp, * q3;
...
cvCopy(q3, tmp, 0);

but elsewhere

CvMat* tmp;
IplImage * complexInput;
...
cvCopy( complexInput, &tmp, NULL );

Here, CvMat is a general matrix, IplImage is an image which derives from
CvMat.
The point is the use of tmp and &tmp indistinctly.

Any clue?
Thanks in advance

How is CvCopy being declared? You're calling it twice with different
argument types, so I'd expect one or the other call to have (possibly
multiple) syntax errors.

The first call is being passed q3 and tmp, both of which are pointers to
CvMat.

The second call is being passed complexInput and &tmp, which are a
pointer to an IplImage and a pointer to a pointer to a CvMat,
respectively.

(I'm not sure what you mean IplImage is "derived from" CvMat -- is it a
field in a CvMat? Is it a typedef? If the latter, I understand why the
first parameter in each call is working, but not the second. Or are you
asking a C++ question in a C newsgroup? In that last case, CvCopy is
overloaded and there's one version expecting a CvMat* and another
expecting a CvMat**).
 
K

Keith Thompson

Joe Pfeiffer said:
How is CvCopy being declared? You're calling it twice with different
argument types, so I'd expect one or the other call to have (possibly
multiple) syntax errors.

A bit of Googling indicates that cvCopy is declared as:

void cvCopy( const CvArr* A, CvArr* B, const CvArr* mask =0);

(looks like C++ rather than C), and CvArr is declared The:

typedef void CvArr;

Since any pointer type can be implicitly converted to void*, I'd expect
both calls to compile without error -- but it's likely that (at least)
one of them is wrong.

The C++ rules are subtly different in this area, so this question should
*really* go to a C++ group.

[...]

To answer the question in the subject line, "mytype* var" and "mytype
* var" are identical; spacing between tokens is not signficant,
either in C or in C++. (In some cases it can serve to specify
where one token ends and another begins, but an identifier can be
adjacent to a punctuation symbol.)

Typically
mytype *var;
is considered better style in C, and
mytype* var;
is considered better style in C++. The C form more closely follows
the language grammar, but as long as you don't try to declare multiple
items together:
mytype* var1, var2; // var1 is a mytype*, var2 is a mytype
either form is probably ok.
 
D

daniele.g

[...]
How is CvCopy being declared? You're calling it twice with different
argument types, so I'd expect one or the other call to have (possibly
multiple) syntax errors.

http://tinyurl.com/6cxbdhe

The code compiles smoothly.
The first call is being passed q3 and tmp, both of which are pointers to
CvMat.

The second call is being passed complexInput and &tmp, which are a
pointer to an IplImage and a pointer to a pointer to a CvMat,
respectively.

(I'm not sure what you mean IplImage is "derived from" CvMat -- is it a
field in a CvMat? Is it a typedef? If the latter, I understand why the
first parameter in each call is working, but not the second. Or are you
asking a C++ question in a C newsgroup? In that last case, CvCopy is
overloaded and there's one version expecting a CvMat* and another
expecting a CvMat**).

I'm still a newbe with opencv, that's why theese questions.
By the way:
CvMat: http://tinyurl.com/6hxo35z
IplImage: http://tinyurl.com/6fzulfw
 
J

Joe Pfeiffer

I though the two form are equivalent, but when I look at this code some
doubt arises:

CvMat * tmp, * q3;
...
cvCopy(q3, tmp, 0);

but elsewhere

CvMat* tmp;
IplImage * complexInput;
...
cvCopy( complexInput, &tmp, NULL );

Here, CvMat is a general matrix, IplImage is an image which derives from
CvMat.
The point is the use of tmp and &tmp indistinctly.

Any clue?
Thanks in advance

Coincidentally, my son had just mentioned opencv to me, so I was already
sort of curious to take a look at it...

It turns out that, in cxcore.h, cvCopy() is declared as

CVAPI(void) cvCopy( const CvArr* src, CvArr* dst,
const CvArr* mask CV_DEFAULT(NULL) );

But CvArr itself is a typedef in cxtypes.h

typedef void CvArr;

So... you can pass any pointer at all to cvCopy() without any
complaints from the compiler. I would expect the second call you put in
above to crash with a segfault or something at run time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top