Array of Strings - Empty pointer error

Posted By: xbox

Array of Strings - Empty pointer error - 02/01/15 19:50

I have an array of strings defined as:
Code:
STRING* bg_sounds[12];
bg_sounds[0] = "song1.mp3";
...
bg_sounds[11] = "song11.mp3";


before the main function I have a handle:
Code:
var bg_handle;


at the end of my main function I have:
Code:
bg_handle = media_play(bg_sounds[0], NULL, 100);


It compiles and runs, but immediately I'm greeted with an "Empty pointer in main" error. If I comment out the media_play line, the game runs so I know that's the line causing issue but I am at a loss as to why. Any help would be greatly appreciated.

Also, I've replaced bg_sounds[0] in the media_play with the string itself and it works but with the array it gives the error.
Posted By: 3run

Re: Array of Strings - Empty pointer error - 02/01/15 19:52

Maybe file wasn't found?

Edit: sorry, I was too blinded at the first look, try this:
Code:
STRING* names[10];

void main(){

	str_cpy(names[0], "song1.mp3");

	var bg_handle = media_play(bg_sounds[0], NULL, 100);

}

Thing is, that you can't set string like "string = "blahblah", you have to use those 'str_' function, to deal with strings. I hope this helps laugh

Greets
Posted By: xbox

Re: Array of Strings - Empty pointer error - 02/01/15 21:34

I did just this and now I am getting, "Invalid arguments in main", and "Empty pointer in main".
Posted By: 3run

Re: Array of Strings - Empty pointer error - 02/01/15 21:50

I'm surprised that it doesn't work on my side too.. I'll do some more tests.
Posted By: txesmi

Re: Array of Strings - Empty pointer error - 02/01/15 22:07

The strings creation is needed.

Code:
STRING *strArray[10];

function strArray_create ()
{
   int index=0;
   for ( ; index<10; index+=1 )
      strArray[index] = str_create("");
}

function strArray_remove ()
{
   int index=0;
   for ( ; index<10; index+=1 )
      str_remove ( strArray[index] );
}

function main ()
{
   strArray_create ();
   str_cpy ( strArray[0], "song1.mp3" );
   var hndSong = media_play ( strArray[0], NULL, 100 );
   while ( media_playing ( hndSong ) )
      wait(1);
   strArray_remove ();
   sys_exit ( NULL );
}

Posted By: 3run

Re: Array of Strings - Empty pointer error - 02/01/15 22:30

txesmi was faster than me blush

The thing is, that 'STRING* blah[3];' will create only an array of empty pointers, to use them you need to allocate strings and save them into those pointers.

This what I've just came out with:
Code:
STRING* names[99];

void main(){
       int i = 0;
       for(i=0; i < 99; i++){
              names[i] = str_create("");
       }

        str_cpy(names[0], "Arty - Kate (Original Mix).mp3");

         media_play(names[0], NULL, 100);

         while(1){
               draw_text(names[0], 10, 10, COLOR_WHITE);
               wait(1);
         }
}



There might be some typos, I'm using my cellphone to write this post. I hope it helps laugh

Greets
Posted By: txesmi

Re: Array of Strings - Empty pointer error - 02/01/15 23:26

you can also use a TEXT struct and forget about allocation and removal.

Code:
TEXT *txtSongs = { string ( "song01.ogg", "song02.ogg" ); }
...
var hndSong = media_play ( (txtSongs.pstring)[1], NULL, 100 );

Posted By: xbox

Re: Array of Strings - Empty pointer error - 02/02/15 04:42

Wow, okay. Thanks for the responses! I hadn't realized that the STRING type was so needy grin

I hadn't even considered the TEXT object, but that is almost exactly what I wanted to accomplish so I guess I'll just change it to a TEXT object and not have to worry about it. Thanks guys! I really appreciate it!
© 2024 lite-C Forums