Passing arrays of pointers into functions
Hello again everyone,
Still working on my program to store a list of mp3 details from a text file.
I've been writing a few functions to perform different operations on the stored information, such as; sort all the songs alphabetically by artist (using a bucket sort) and create playlists (using linked lists).
The mp3 info is stored in a structure like this:
Code:
typedef struct mp3info
{
char title[100];
char artist[100];
char album[100];
char genre[100];
} SONG;
The memory location of each SONG structure is then stored in an array of pointers, so that if I wanted to print the artist of track 1054 I could do this:
Code:
printf ( "Artist: %s", array[1054]->artist );
Now, if I wanted to do this within a function, I assumed I would do this:
Code:
void printsong ( int tracknumber, SONG *array[2000] )
{
printf ( "Track: %d\n", tracknumber );
printf ( "Title: %s\n", array[tracknumber]->title );
printf ( "Artist: %s\n", array[tracknumber]->artist );
printf ( "Album: %s\n", array[tracknumber]->album );
printf ( "Genre: %s\n", array[tracknumber]->genre );
}
int main ( void )
{
SONG *array[2000]; //assume track details are already stored in array
int tracknumber;
/* Print track 3 */
tracknumber = 3;
printsong ( tracknumber, array );
return 0;
}
This is just a simple example of the type of thing I'm having trouble with. Am I doing anything wrong when passing the array? If I try to print details from the array within the main function everything seems to work fine.
Thanks for everyone who's helped me with this so far! Wouldn't have managed without all your help and advice!