[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

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

daniele

7/13/2011 6:39:00 PM

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
--
Il pugile: "Come vado?".
L'allenatore: "Se l'ammazzi fai pari".
-- Beppe viola
6 Answers

China Blue Veins

7/13/2011 6:55:00 PM

0

In article <87sjqa2ffq.fsf@father.nostromo.wy>,
daniele@father.nostromo.wy (daniele.g) wrote:

> 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?

The * is part of following identifier, not the preceding type name.

CvMat * tmp, * q3;
is
CvMat (* tmp), (* q3);

CvMat* tmp;
is
CvMat(* tmp);

--
I remember finding out about you, | Go out with a bang.
Everyday my mind is all around you,| I'm whoever you want me to be.
Looking out from my lonely room | Annoying Usenet one post at a time.
Day after day. | At least I can stay in character.

Joe Pfeiffer

7/13/2011 7:43:00 PM

0

daniele@father.nostromo.wy (daniele.g) writes:

> 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**).

Keith Thompson

7/13/2011 8:01:00 PM

0

Joe Pfeiffer <pfeiffer@cs.nmsu.edu> writes:
> daniele@father.nostromo.wy (daniele.g) writes:
>> 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.

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.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

daniele

7/14/2011 11:01:00 AM

0

Joe Pfeiffer <pfeiffer@cs.nmsu.edu> writes:

[...]

> 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.c...

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.c...
IplImage: http://tinyurl.c...

--
"Al diavolo gli ordini!"
-- Data (Brent Spiner), "Star Trek - Primo Contatto"

Kenneth Brody

7/14/2011 8:36:00 PM

0

On 7/13/2011 2:39 PM, daniele.g wrote:
> I though the two form are equivalent, but when I look at this code some
> doubt arises:
[...]

Question from subject:

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

There is no difference to the compiler. However, a beginner programmer
might mistake "mtype*" as being the type, which does make a difference if
you are defining more than one variable.

Consider:

char *pointer1,*pointer2;

versus the incorrect:

char* pointer1,pointer2;

This is also why it's a bad idea to hide such definitions in a #define,
rather than a typedef.

Consider:

typedef char *char_ptr;
char_ptr pointer1, pointer2;

versus the incorrect:

#define char_ptr char *
char_ptr pointer1, pointer2;

(Whether it's a good or bad idea to hide "pointerness" within a typedef is
left as a debate for another time.)

--
Kenneth Brody

Joe Pfeiffer

7/15/2011 5:49:00 PM

0

daniele@father.nostromo.wy (daniele.g) writes:

> 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.