error in pointers[solved]

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
omar shaaban
Posts: 616
Joined: Wed Nov 01, 2006 6:26 pm
Location: Cairo,Egypt
Contact:

error in pointers[solved]

Post by omar shaaban »

when i excute this program it responds because of this line:

Code: Select all

sold->number=223;
here is the code :

Code: Select all

#include <iostream>
using namespace std;
 typedef struct df {
    int number;
    void talk()
    {
        std::cout<<number<< "\n";
        }
    };
int main()
{
    df gh;
    gh.number=1;
      df * sold ;
      sold->number=223;
      sold->talk();
    gh.talk();
    return 0;
}
thanks for reading
Last edited by omar shaaban on Sat Sep 19, 2009 12:55 pm, edited 1 time in total.
B@z
Posts: 876
Joined: Thu Jan 31, 2008 5:05 pm
Location: Hungary

Post by B@z »

i dont know what do u want to achive but you cant just use a pointer without initializing it.

if you want the sold point to gh, then you have to do
df *sold = &gh;

then play with it. if you want sold points a different variable, then you have to create it BEFORE u use xD
Image
Image
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

right, if you want to use a pointer you'll have to create the object...
like this:

Code: Select all

df* sold = new df; 
and don't forget to delete the object when it's not needed anymore !!! ;)

Code: Select all

delete sold;
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Post Reply