Please check the code below first, the pointer didnt point to the a.
anyone could figure out why *pa=a failed ?PHP Code:#include <iostream>
void main()
{
int a=0;
typedef int * pt_int;
pt_int pa;
*pa=a;
cout << &a << endl;
cout << pa << endl;
}
![]()
This is a discussion on pointer didnt locate the right place~ within the C++ Programming forums, part of the General Programming Boards category; Please check the code below first, the pointer didnt point to the a. PHP Code: #include <iostream> void main () { ...
Please check the code below first, the pointer didnt point to the a.
anyone could figure out why *pa=a failed ?PHP Code:#include <iostream>
void main()
{
int a=0;
typedef int * pt_int;
pt_int pa;
*pa=a;
cout << &a << endl;
cout << pa << endl;
}
![]()
Never end on learning~
>*pa=a;
This should be
pa = &a;
You are assigning a value to a pointer, which is not an address. pa at this point hasn't been initialized so it points to some random address. Thus dereferencing is a bad idea.
>void main()
This should be
int main()
main returns an int, anything else is undefined, which is very bad.
-Prelude
Last edited by Prelude; 08-01-2002 at 10:15 PM.
My best code is written with the delete key.
that is to say, only when an address is passed to the pointer it could be initialized, yes ?
>main returns an int, anything else is undefined, which is very bad.
I tested void and it just works fine. What's the matter please ?
thanx in advance~
Last edited by black; 08-01-2002 at 10:40 PM.
Never end on learning~
>that is to say, only when an address is passed to the pointer it could be initialized, yes ?
A pointer variable must contain a valid address before you can dereference it safely. It's just like any other variable, it has to contain something valid to be of use. The only difference is that a pointer variable holds an address as its value.
>I tested void and it just works fine. What's the matter please ?
It may work for you and it may work for me, but undefined means that anything can happen, re-formatting your hard drive without your consent for example. Undefined is about as bad as it gets and you should avoid anything that results in such behavior.
-Prelude
My best code is written with the delete key.
many compilers safeguard void main. many dont. it's not like it takes any extra work to write int main() then return something![]()
hello, internet!
>it's not like it takes any extra work to write int main() then return something
You don't even have to do that.If main is defined as returning int then 0 is returned by default without any need to explicitly do so. This is acceptable and standard C++:
-PreludeCode:#include <iostream> int main() { std::cout<<"Hello, world!\n"; }
My best code is written with the delete key.
>>int main()
>>main returns an int, anything else is undefined, which is very bad.
Is this the fact that void main do return a value of somewhat undefined type ?
I considered it never returns anything.![]()
black~
Never end on learning~
Is there a standard return value from main to indicate successful program termination? Should I be returning a non-zero digit for success?
Couldn't think of anything interesting, cool or funny - sorry.
> I tested void and it just works fine
Yeah, and writing to an uninitialised pointer can work fine as well - for a while. The fact that your current compiler does not say there is a problem does not mean that the program is in any way bug free, nor is a program which contains bugs obliged to crash at the first instance a rule is broken.
Then all of a sudden, your bad habits catch up with you and bite you in the @ss, say when you upgrade your compiler, move to another compiler, or add that critical line of code which takes it over the edge.
http://www.eskimo.com/~scs/C-faq/q11.12.html
>Is there a standard return value from main to indicate successful program termination?
Yes, there are two. The first is 0, the second is a portable macro defined in <cstdlib>:
return 0;
or
#include <cstdlib>
.
.
return EXIT_SUCCESS;
or you can simply omit the return statement and the value for successful completion ( the equivalent of return 0; ) will be sent automatically...if main was defined as returning int.
-Prelude
My best code is written with the delete key.