Hi
I am making an mp3 database at the moment and i have run into some difficulty when passing down the array to a function. I can do it when i create the array globally as obviously this means i dont have to pass it down, but i want to learn how to do it properly - have only been learning for a few months so i am still getting used to it all.
here is my code

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

#define MAXLENGTH 50
#define MAXGENRE 15
#define MAXMP3FILES 100
#define MAXFILEPATH 400  
#define COMMANDLENGTH 800
#define FILENAMELENGTH 400

 // Global Variable //
typedef struct {
           int durmin,dursec;
           char title[MAXLENGTH]; // title=array, MAXLENGTH=size of array
           char artist[MAXLENGTH];
           char genre[MAXGENRE];
           char filepath[MAXFILEPATH];
    } musicfile;  // struct is called musicfile 
    
FILE* fp;

void addtracks (int mp3database[]); 
//////////////////////////////////////////////////////////////////////////////////////
int main (void)
{
    musicfile mp3database[MAXMP3FILES]; // this creates a array called mp3database which is the size of MAXMP3FILES, each element in the array has the struct called musicfile
    
int menuoption;
    
    puts("please enter a menu choice - choose 1 lol");
    scanf("%d",&menuoption);
    
    
    switch(menuoption)
    {
    case 1: //Add a track
      {
        addtracks(mp3database);
        break;
      }  
    }// end switch
    
    system("pause");
    return 0;
}
//////////////////////////////////////////////////////////////////////////////////////
void addtracks (int mp3database[])
{
                 printf("Please enter the song title: ");
                 fflush(stdin); //fflush to clear previous keyboard input
                 fgets(mp3database[0].title,(MAXLENGTH),stdin);
             
                 printf("Please enter the artist: ");
                 fflush(stdin);
                 fgets(mp3database[0].artist,(MAXLENGTH),stdin);
             
                 printf("Please enter the genre: ");
                 fflush(stdin);
                 fgets(mp3database[0].genre,(MAXGENRE),stdin);
             
                 printf("Please enter the duration - m ss: ");
                 scanf("%d %d",&mp3database[0].durmin,&mp3database[*header].dursec);
             
                 printf("Please enter the filepath: ");
                 fflush(stdin);
                 fgets(mp3database[0].filepath,(MAXFILEPATH),stdin);
}
                 
//////////////////////////////////////////////////////////////////////////////////////

Current errors i have been getting are
passing arg 1 of add tracks from incompatible pointer type
Request for member 'title" in something not a structure of union
it does not recognise any of the parts of my typedef.. artist, genre e.t.c

any help really appreciated!!

Emily

void addtracks (int mp3database[]);

This declares a function that takes a pointer to an int, however you want a function that takes a pointer to a musicfile. Which bit of it do you think you need to change?

fflush(stdin); //fflush to clear previous keyboard input

Calling fflush on stdin is at best a platform specific extension and at worst undefined behaviour. Undefined behaviour is bad because literally anything can happen so it should be avoided.

If it is an extension supported by your platform then you could very easily run into trouble if you port you code to a different platform.

I know the fflush isnt exactly good practice but our lecturer has said this is acceptable for the time being, its another avenue i need to explore.

As for the void addtracks (int mp3database[]) i am struggling with this.
musicfile is the name of the struct, i want to enter my data into a musicfile contained by the array

so perhaps

void addtracks (struct musicfile)

As for the void addtracks (int mp3database[]) i am struggling with this.
musicfile is the name of the struct, i want to enter my data into a musicfile contained by the array

so perhaps

void addtracks (struct musicfile)

Your type isn't struct musicfile because you typedef it. The structure is actually unname, to be named it would need a name immediately after the keyword structure in the definition but that does not matter. The name of your type is musicfile.

For any general type T you can declare a pointer to that type as a variable or function parameter as T* SymbolName; additionally for a function parameter you can declare a pointer as T SymbolName[]; .

In void addtracks (int mp3database[]) the second format is used so int represents T. To declare a pointer to musicfile parameter you therefore put musicfile in place of T or void addtracks (musicfile mp3database[]) .

This article has been dead for over six months. Start a new discussion instead.