Hey I am following a video tutorial teaching pointers... I am following along and writing what they have. Which is:

#include <iostream>

using namespace std;

void main()
{
	int myArray[4] = (1, 2, 3, 4);

	cout << (int)myArray << endl;
	cout << (int)&myArray[1] << endl;
}

They are able to run this.
Now they are using an older version of Visual C++ than I am.

Here is the error I am getting when I try to run it:

1>------ Build started: Project: HelloWorld, Configuration: Debug Win32 ------
1>Compiling...
1>helloworld.cpp
1>c:\users\--------\documents\visual studio 2008\projects\helloworld\helloworld\helloworld.cpp(7) : error C2440: 'initializing' : cannot convert from 'int' to 'int [4]'
1>        There are no conversions to array types, although there are conversions to references or pointers to arrays
1>Build log was saved at "file://c:\Users\--------\Documents\Visual Studio 2008\Projects\HelloWorld\HelloWorld\Debug\BuildLog.htm"
1>HelloWorld - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

How can this be fixed

Hi,

It's hard to say without knowing what they are trying to achieve on the tutorial, firstly you should use curly braces to initalize an array at creation:

int myArray[4] = {1, 2, 3, 4};

Secondly, the compiler is right here - what you are asking the compiler to do is convert an object of type array to an object of type int - there is no sensible way to do that. I'd need to know what the code is trying to achieve to write it for you.

Quick point here - don't use old C-style casts i.e. (int) - instead use:

static_cast<int>(myArray)

This will tell the compiler exactly what it is you are trying to do - and may yield better error messages, and also it will stop you accidentally doing a different type of cast (dynamic_cast or reinterpret_cast - see google).

The compiler doesn't seem to be complaining about the last line - code like this is pretty nasty and should be avoided, but for demonstration of pointers I guess it's OK. What do they say this should do?

Phil.

It should display the memory address of the variable.

OK - The code you originally posted actually compiles OK on GCC 4.0.1, so I'm not sure why it's failing on your compiler.

I get it now - they are just demonstrating that myArray is just a pointer to the first int element and &(myArray[1]) is just the address of the second element - note the brackets to make it clear.

Slight correction to what I originally said - you cannot use a static_cast here as you are asking the compiler to reinterpret from - from a pointer to a value. Thus you want to use reinterpret_cast. On GCC using reinterpret_cast compiles correctly and gives exactly the same result as your original code, static_cast produces the same compile time error you are seeing - saying it cannot convert.

A few other points - cout should be able to handle printing out pointers without casting them - thus if you remove the cast altogther you should see the hex memory addresses of the pointer printed out (showing that array[1] is one size_of(int) above array[0] in memory - I'm assuming that is what they are trying to get you to see).

If you want to use a cast try the below code - note I have replaced int with 'unsigned int' - that is because a memory address cannot be negative (an int can) so unsigned int (values >=0 only) represents a better range.

Try this:

#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
  int myArray[4] = {1, 2, 3, 4};

  cout << reinterpret_cast<unsigned int>(myArray) << endl;
  cout << reinterpret_cast<unsigned int>(&myArray[1]) << endl;

  return 0;
}
commented: Wow that was great advice! +1

Problem Solved...
Thanks, you have been very helpful.

+1Rep

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.