Pointers question?

Which one of these declarations is valid and why?
int *p=*a;
OR
int *p=&a;
This happily compiles:
1
2
int** a;
int*p = *a;



and so does this:
1
2
int a;
int* p =&a;


So they're both valid.

That link presents void main() as C++. If it can't even get that right, I wouldn't trust it.
I don't know.. it has such gems as 2-dimensional pointers and apparently passing a pointer to a pointer is passing by reference. I never knew. I always thought you passed by reference to pass by reference!
Ahh, perspective, one views it as something and other views as something else!!!@Moschops:please google on how to use main function or main function types in c++.
Last edited on
@anirudh sn
This is not a matter of views or perspective. Your code is not valid C++.
@anirudh sn: Are you suggesting Moschops doesn't know how to use main? The C++ standard specifically states that the return type of main must be int. It does kind of suck how many compilers and tutorials allow void. MSVC++ allows void without a single complaint.
@szak1592: As Moschops so cleverly pointed out, it depends on implementation. The '*' operator is the dereference operator, which returns the object at an address, and the '&' operator is the address-of operator, which returns the address of an object.
There is no perspective on main in C++.

The ISO C++ standards made it very clear (in 1998, 2003 and again in 2011) that main returns an int. Using google will do no more than show up endless cases where people literally do not know what they are talking about and in many cases didn't even realise that C++ is defined by a paper document.

This seems to be a particular problem in parts of Asia, where the very common course textbooks incorrectly use void main(), and teachers commonly push their students into using compilers over a decade old (or that Microsoft travesty :) ) that wrongly accept it without so much as a warning.

Last edited on
ofcourse it is not a good programming practise, but its not invalid, anyway as per ANSI standard void main() shouldnt be used.
closed account (z05DSL3A)
I have never seen anything official that has ever stated that "void main() is valid". The only place where it could be valid is in a freestanding implementation of a compiler.
ofcourse it is not a good programming practise, but its not invalid,


It is as invalid as string main(), or bananasOnToast main(), or anything else. Everyone else here has read the relevant parts of the ISO standard; the actual C++ definition. You can stand there as long as you like saying "no, actually it's valid because it's in my textbook and my twenty year old compiler doesn't stop me using it"; it won't change the facts.
Topic archived. No new replies allowed.