2

I have a very simple question.

I want to write the below line of code in 2 lines :

IplImage *image = 0;

like :

IplImage *image;
image = 0;

I want to know what I have written is correct or else I want to know how to write the correct one (in two lines).

Thanks

3
  • 3
    Why do you call this an "integer pointer"? You have a pointer to an IplImage structure, not to an int. The literal 0 is defined by the C++ language as the source code notation for a NULL pointer, it is not used as an integer. Using any other integer would cause a compile error. Commented Aug 1, 2010 at 5:45
  • 1
    There's no way to say whether it is "correct" or not without knowing what you are trying to do. You code is formally correct. Yet there's a strong suspicion that it doesn't do what you want it to do, since you are mentioning "integer pointer" of some kind in the question title, while there's no "integer pointer" in your code. Commented Aug 1, 2010 at 5:48
  • @GMan I keep falling for that trap again and again. My bad, would be IplImage *image(0);? Commented Aug 1, 2010 at 5:50

5 Answers 5

5

Perfectly correct. But if you don't have a very good reason to do it that way, I'd suggest leaving it as a single line that both declares and initializes it. It's more obvious, and you're less likely to ever miss initializing a pointer that way.

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

Comments

1

It is correct. Why didn't you just try it?

1 Comment

Well, the argument could be made that most every compiler has extensions; just because it works doesn't mean it's valid according to the standard.
1

Writing

IplImage *image = 0;

seem to be clearer as it is obvious that a pointer is used.

With

IplImage *image;
image = 0;

you may have additional lines of code between the first and the second line. The second line (image = 0) appears less clear to me. Maybe renaming the variable to pImage improves readability if you prefer the second option (two liner).

Comments

0

Iplimage is the structure defined in opencv. You can use code below to initialize the pointer:

Iplimage* image=cvCreateImage(width,height,channels);

Comments

0

In C++

IplImage *image(NULL);

woul also be allowed, even if it may be confusing about your image to be allocated by calling the constructor with the first argument set to NULL.

However, declaring + initializing in 1 line is still remended and almost all case. The only case I see where it's better to initilize in 2 lines it's when the line is too long.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.