Array of pointers trouble

Hi, I'm trying to make an array of pointers to variables and I can't see why my code is different from the examples I've found online. Why does this not work?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    #include<iostream>
    using namespace std;

    int *p1;
    int *p2;

    int main(void)
{
    *p1 = 3;
    *p2 = 4;

    int* ar[2];

    ar[0] = p1;
    ar[1] = p2;

    cout<<*ar[0]<<*ar[1];

   return 0;
}


Moving the initialization of p1 and p2 inside the main, however, makes the program work on ideone.com but on codeblocks it still doesn't work.
Thank you for any help
p1 and p2 are never set to point to anything, so they just point to random data. Then you suddenly come in with a sledgehammer and slam 3 and 4 onto those random memory locations. I am very surprised it worked on ideone.
I'm curious. How could you get the program to assign a data location to those pointers so that you could use just the pointer instead of making a variable?
Usually with the 'new' operator.
EDIT: Computergeek01 posted a short post while I was typing a long post.

GRex2595 wrote:
I'm curious. How could you get the program to assign a data location to those pointers so that you could use just the pointer instead of making a variable?
No, do not say or even think "assign a data location to those pointers". Instead, say (and think) "change what the pointers point to" or "make the pointers point to something". There are two ways.

A. You can make them point at something that already exists:
1
2
3
4
5
int x;
int *p;

p = &x; //make p point to x (p holds the address of x)
*p = 7; //change the value of what p points to (make x be 7) 
B. You can make them point at something you just created out of thin air:
1
2
3
4
5
6
7
8
int *p;

p = new int; //dynamically allocate an integer and mak p point to it
*p = 7; //change the value of the new object we just created (p is not affected)

//...

delete p; //do this, or else your program will have memory leaks 
Note that if you make a new array with new int[blah], then you must use delete[] to free it.

Generally though, pointers are seldom used in C++ - instead we use the wrapper classes like unique_ptr (go google it!).
Last edited on
Drat. Well that explains alot:P Thank you, that certainly is great to know, but that means I probably didnt ask the right question.
I'm trying to make a basic SDL program, and wanted to make an array of pointers to SDL_Surfaces, since I had learned that you create a surface using

SDL_Surface* background= SDL_LoadBMP("background.bmp");
and so i wanted to create an array of these surfaces,
SDL_Surface* _1= SDL_LoadBMP("dot.bmp");
SDL_Surface* _2= SDL_LoadBMP("dot.bmp");
etc.

and then somehow put them into an array.
I saw they were pointers to what looked like objects of type SDL_Surface, and tried to ask a simpler, analogous question.
So does anyone know if the answers given above still apply to SDL_Surfaces?
By "assign a data location to those pointers" I really meant what you were doing with example B, I just wasn't sure how to phrase it. I know in C you can use malloc for that, but I wasn't sure with C++. Also, I'm curious if there's a C++ version of realloc like new is for malloc.

I already more or less know about unique_ptr, but I haven't taken time to use it yet.

Thanks L B
Ok I've tried a couple things and gotten it to compile and run correctly using what you guys said, thank you:)

Topic archived. No new replies allowed.