0

In the code:

FMOD_RESULT result;
FMOD::System *system;

result = FMOD::System_Create(&system);
FMODErrorCheck(result);

result = system->init(100, FMOD_INIT_NORMAL, 0);
FMODErrorCheck(result);

FMOD::Sound *sound;
result = system->createSound("/home/djameson/Music/Hurts-Like-Heaven-Cold-Play.mp3", FMOD_DEFAULT, 0, &sound);
FMODErrorCheck(result);

FMOD::Channel *channel;
result = system->playSound(FMOD_CHANNEL_FREE, sound, false, &channel);
FMODErrorCheck(result);

result = channel->setVolume(0.5f);
FMODErrorCheck(result);

I am confused about the pointer usage. For example the line FMOD::Channel *channel, creates a pointer of type Channel but It doesn't say where it points too.

Don't you usually go pointer = &variable

I am new at c++. Thank-you for your help.

user3213163
  • 607
  • 3
  • 8
  • 10

3 Answers3

1

2nd line you pass a pointer to pointer for initialize FMOD System

0

In the next line you pass a pointer to that pointer to playSound function, so that it can initialize it for you.

tumdum
  • 1,981
  • 14
  • 19
0

When you call playSound passing in &channel you are passing a pointer to a pointer to a channel. This means the function can point your pointer to the channel created in playSound. Confused? OK, a diagram!

Channel* x //is a pointer to a channel

ie:

x ---> [SOME MEMORY WHERE A CHANNEL IS STORE]

So normally you'd do

x = &channel // where channel is the actual (non-pointer) channel

Instead, what we are doing is

Chanel** y = &x

ie

y ----> x ---> [SOME MEMORY WHERE A CHANNEL IS STORED]

Still confused, lets try a simple example.

 int a = 4; // stores 4 a
 int b = 8; // stores 8 in b

 int* x = NULL;
 int** y = NULL;

 // normal use case, point x at a
 x = &a;

 // now when we modify a, this can be accessed from x
 a = a + 1;

 // *x (i.e. the value stored where x is pointed) is now 5

 // and now for y
 y = &x;

 // we now have *y == x and **y = a

 x = &b;

 // we now have *y == x and **y = b

So the function syntax for the FMOD call takes a pointer to a pointer, allowing it to fill in the pointer value, so you have it. Hopefully that makes it a little clearer...

T. Kiley
  • 2,752
  • 21
  • 30
  • So the pointer you create gets pointed to a pointer which points to the address of the channel. If so why pass the address of the pointer as a parameter and not the pointer itself? Thanks for all you help. – user3213163 Jan 19 '14 at 23:58
  • Don't worry i got it now – user3213163 Jan 20 '14 at 04:35