Jump to content
  • Advertisement
Sign in to follow this  
hothead

need help with code!

This topic is 5134 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need some help with this
#include<iostream>
using namespace std;

int main()
{
    int uno = 1;
    int one = uno;
    int *two = 2;

    cout<<"the number "<<one<<" is small";
    cout<<"two:"<<*two;

    int x;
    cin>>x;
    return 0;
}

errors:initilization to int from int lacks a cast. line 8

Share this post


Link to post
Share on other sites
Advertisement
hothead,

Try removing the * from the line:
"int *two = 2;"

It should be:
int two = 2;

Also remove the * from the output line:
"cout<<"two:"<<*two;"

That should just be

"cout<<"two:"<<two;"

Share this post


Link to post
Share on other sites
Quote:
Original post by hothead
I need some help with this*** Source Snippet Removed ***errors:initilization to int from int lacks a cast. line 8


If you're wanting a pointer to an int, then you need to allocate memory for it.


int *two = new int;
*two = 2;


Share this post


Link to post
Share on other sites
np. By the way, how's your new computer working out? At least I think I read in your journal that your new dell arrived. I ordered a inspiron 1000 a few weeks ago. Haven't set up the printer yet though...

Edit: fixed horrible grammEr

Share this post


Link to post
Share on other sites
I know it doesn't take up much memory, but don't forget to delete the allocated memory for the int when you're done with it. Just good practice. :)

Share this post


Link to post
Share on other sites
You generally dont want to allocate memory "on the heap" in the fashion you are attempting.

In the example you provided, it is much better to just allocate the memory on the stack by not using a pointer at all.

Solution1:

// Create the data on the stack
int two = 2;

vs.

Solution2:

// Allocate memory on the heap and store the address in two
// Then store the data at the location in memory pointed to by two
int *two = new int;
*two = 2;

Solution 1 is marginally faster and does not require you to "delete" the memory allocated to store '2' when you're done with it. With solution 1, the memory is automatically freed when you leave scope.

Share this post


Link to post
Share on other sites
Quote:
Original post by ontheheap
If you're wanting a pointer to an int, then you need to allocate memory for it.


:D

Share this post


Link to post
Share on other sites
Quote:
Original post by jwalsh
You generally dont want to allocate memory "on the heap" in the fashion you are attempting.

In the example you provided, it is much better to just allocate the memory on the stack by not using a pointer at all.

Solution1:

// Create the data on the stack
int two = 2;

vs.

Solution2:

// Allocate memory on the heap and store the address in two
// Then store the data at the location in memory pointed to by two
int *two = new int;
*two = 2;

Solution 1 is marginally faster and does not require you to "delete" the memory allocated to store '2' when you're done with it. With solution 1, the memory is automatically freed when you leave scope.


Seems to me like he's simply learning how to use pointers. I remember writing all sorts of things like that at first.

Share this post


Link to post
Share on other sites
ontheheap,

I agree completely. It occured to me however that most books and tutorials focus far more on "how" to use pointers than on "when" to use pointers.

I figure its never too early to start learning correct usage.

My 2 cents...Once you feel sorta comfortable with pointers...pick up a good book that teaches data structures. Then step through the lessons on Linked lists. By the end you'll either feel thoroughly confused, or you'll have a much more sound understanding of pointers and their use. - probably both. Cheers! =)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!