What's wrong with this?

This is a discussion on What's wrong with this? within the C++ Programming forums, part of the General Programming Boards category; Hi, Getting to grips with new operator. What is wrong with the following code: Code: #include <iostream> using namespace std; ...

  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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,702
    Not an int. 'x' is undefined, so speculating the value of sizeof is pointless.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    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.

  5. #5
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59

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-27-2005, 11:48 PM
  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, 10:26 AM

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