Thread: What's wrong with this?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Smile What's wrong with this?

    Hi,

    Getting to grips with new operator.

    What is wrong with the following code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    void main()
    {
    
    	x = new double[200];
    
    
    	cout << "x has size " << sizeof(x) << " and has value of %f" << x << endl;
    
    }
    Cheers

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    23
    still need to declare a variable
    Code:
    double *x = new double[ 200 ];
    no data type sets x to an int/undefined?? and sizeof(x) is returning 4 most probably ( 32 bits ) or 8 for 64-bit OS

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Not an int. 'x' is undefined, so speculating the value of sizeof is pointless.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Don't forget to
    delete [] x;
    afterwards.

    Also, main should return int.
    You might also want to check up smart pointers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wrong wrong with my xor function?
    By Anddos in forum C++ Programming
    Replies: 5
    Last Post: 04-26-2009, 01:38 PM
  2. whats wrong with this? no errors but wrong result
    By InvariantLoop in forum C Programming
    Replies: 6
    Last Post: 01-28-2005, 12:48 AM
  3. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  4. What's wrong
    By money? in forum C Programming
    Replies: 3
    Last Post: 06-14-2003, 07:13 PM
  5. What am I doing wrong?!
    By NebulousMenace in forum C++ Programming
    Replies: 4
    Last Post: 02-05-2003, 11:26 AM