really dumb question....

This is a discussion on really dumb question.... within the C++ Programming forums, part of the General Programming Boards category; sorrie im new at this.. but whats wrong with my code? #include <iostream.h> #include <string.h> main() { char *test; test ...

  1. #1
    DJTris
    Guest

    really dumb question....

    sorrie im new at this.. but whats wrong with my code?

    #include <iostream.h>
    #include <string.h>


    main()

    {
    char *test;
    test = NULL;
    *test = 'x';
    cout << *test << endl;
    }

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    #include <iostream.h>
    #include <string.h>


    main()

    {
    char *test;
    test = NULL; //here you are assigning the pointer to NULL
    *test = 'x'; //Now you are trying to fill a NULL pointer...not good
    cout << *test << endl;
    }
    Try;
    #include <iostream.h>
    #include <string.h>


    int main(void)

    {
    char *test = new char;
    //test = NULL;
    *test = 'x';
    cout << *test << endl;
    delete test;
    }

  3. #3
    DJTris
    Guest

    :(

    how bout this one though :

    #include <iostream.h>
    #include <string.h>

    main()

    {
    char *str;
    str = new char[10];
    strcpy(str, "hello");
    delete [] str;
    cout << str << endl;
    char *str2 = new char[10];
    strcpy(str2, "dolly");
    cout << str << " " << str2 << endl;
    }

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    53
    Ummm... what are you trying to do here?
    Take out the 'delete' line (what's it there for?) and try it again.

    Almosthere
    "Technology has merely provided us with a more efficient means for going backwards" - Aldous Huxley

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,764
    > delete [] str;
    Any reference to str after this point is broken

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    Whether you use int main() or int main(void), you'll need to return, usually return 0;
    As far as I know, you can't use main() or main (void), You can use void main()

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,764
    > You can use void main()
    No you can't (not unless your compiler is also broken)

  8. #8
    Unregistered
    Guest
    I just ran this code on MSVC 6.0 and it ran fine. I know it's not recommended, but it works.
    Code:
    #include <iostream>
    using std::cout;
    void main()
    {
    	cout << "Hello world";
    	return;
    }

  9. #9
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Just because some piece of code compiles and runs doesn't mean that its legal c/c++. Read a copy of the c and c++ standards. main() always returns an int.
    The value of this int is seen usually as an error code by the operating system. Returning a value of 0 means that your app finished its work all well and good.Any non-zero value can indicate to the operating system that an error occurred during the running of your app.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dumb question
    By travis999 in forum C Programming
    Replies: 3
    Last Post: 10-26-2007, 01:57 AM
  2. very dumb question.
    By Blips in forum C++ Programming
    Replies: 14
    Last Post: 11-08-2005, 09:37 AM
  3. Dumb question
    By dragon2309 in forum C Programming
    Replies: 18
    Last Post: 10-29-2005, 04:27 PM
  4. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 03:51 PM
  5. another dumb question - loop not working?
    By Captain Penguin in forum C++ Programming
    Replies: 8
    Last Post: 10-06-2002, 11:15 PM

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