pointer didnt locate the right place~

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 () {    ...

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    pointer didnt locate the right place~

    Please check the code below first, the pointer didnt point to the a.
    PHP Code:
    #include <iostream>

    void main()
    {
      
    int a=0;
      
    typedef int pt_int;
      
    pt_int pa;
      *
    pa=a;
      
    cout << &<< endl;
      
    cout << pa << endl;

    anyone could figure out why *pa=a failed ?
    Never end on learning~

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >*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 11:15 PM.
    My best code is written with the delete key.

  3. #3
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    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 11:40 PM.
    Never end on learning~

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.

  5. #5
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    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!

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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++:
    Code:
    #include <iostream>
    
    int main()
    {
      std::cout<<"Hello, world!\n";
    }
    -Prelude
    My best code is written with the delete key.

  7. #7
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    >>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~

  8. #8
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    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.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,746
    > 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

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 04:47 PM
  2. Replies: 1
    Last Post: 03-24-2008, 11:16 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  4. How did you master pointers?
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-17-2006, 08:23 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21