Fgets rewriting last line on the position [0] of an string array on every call
<p>i'm having a hard time finding the problem on an assignment for college. The program is a music player and it has like 5 functions which all work fine, but one, adding songs to the playlist. The problem is on the second call of the function, when it has to add a second song to the string array playList. I have noted debugging the code that in the moment I press enter after the fgets() function it changes playList[0] into what i just wrote, without even entering the function which modifies the array. So i think the problem comes from somewhere else than the function. I'd use a dynamic array, which would be a lot better but i'm not allowed to use dynamic memory D: (Sorry if i mispelled anything english is not my main language). I believe the problem is in the function fill_playlist, maybe when i assign a string to playList[i] but i can't find where. It also happens when i call fgets in the other menu option, Search Record.
I think it's a problem of pointer arithmetic i'm missing.
Basically everytime i use fgets() playList[0] is instantly filled with the last string written and i can't fill playList[1] or more.<br>
Thanks<br>
<a href="http://pastebin.com/FX2bHUSk" rel="nofollow">http://pastebin.com/FX2bHUSk</a></p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;time.h&gt;
#include &lt;unistd.h&gt;
#include &lt;sys/wait.h&gt;
#include &lt;string.h&gt;

#define LIBRARY_DIRECTORY "/home/user/Desktop/library/" 
#define MSG_RANDOM_LIST "How many songs would you like to randomly listen?"
#define MSG_RANDOM_ERR "There are not that many songs in the list!"
#define MSG_INPUT_ERROR "There has been an input error!"
#define MSG_SEARCH "These are the songs in the playlist that contain the keyword: "
#define MSG_EMPTY_LIST "The list is empty! You can add records using the 'Add Record' function."
#define MSG_FULL_LIST "The list is full, you have added all the songs in the library!"
#define MSG_ALREADY_IN "The song you intend to add is already on the playlist. Please try with another one."
#define MSG_ADD_RECORD "Write the name of the song you would like to add."
#define MSG_WRONG_NAME "The song you entered is not in the list, please add another one."
#define MSG_SUCCESFUL_ADD "The song you wrote was succesfully added to the playlist!"
#define MSG_SEARCH_RECORD "Enter a keyword to search for a record on the playlist."
#define MSG_EMPTY_SEARCH "There are no songs matching the input keyword."
#define OPT_PLAY_IN_ORDER  "0 - Play in order."
#define OPT_PLAY_RANDOM "1 - Play randomly."
#define OPT_PLAY_RECORD "2 - Play record."
#define OPT_ADD_RECORD "3 - Add record."
#define OPT_SEARCH_RECORD "4 - Search record."
#define OPT_LIST_RECORD "5 - List records."
#define OPT_EXIT "6 - Exit."
#define MAX_PLAYLIST 25
#define MAX_RECORD 20
#define MAX_MENU 6

typedef enum {PLAY_IN_ORDER=0,PLAY_RANDOM=1,PLAY_RECORD=2, ADD_RECORD=3, SEARCH_RECORD=4,LIST_RECORD=5,EXIT=6} first_choice_t;
typedef enum {ST_INPUT_ERROR=0,ST_EMPTY_LIST=1,ST_FULL_PLAYLIST=2,ST_ALREADY_IN=3,ST_WRONG_NAME=4,ST_RANDOM_ERR=5,ST_EMPTY_SEARCH=6,ST_EXIT=7,OK=8} status_t;

void initialize_array(char ** array);
char * select_record (char ** playList, int i);
status_t menu_limit(int choice);
void printMenu();
status_t player(char ** playList);
status_t playOrder(char** playList);
status_t validateChoice(int choice, char ** playList);
status_t playRandom(int max_rand,char ** playList);
status_t printPlaylist(char** playList);
void playRecord (char * record);
int limit_random(int max_random);
void cleanstdin();
status_t add_record (char * record,char ** playList);
status_t fill_playlist (char * record,char ** playList);
status_t check_repeat (char * record,char ** playList,int length);
status_t search_record (char * keyword, char ** playList);

int main()
{
    /*string array which i'll initialize with 0s to fill with songs later.*/
    char * playList [MAX_PLAYLIST];
    status_t st= OK;

    initialize_array(playList);
    srand(time(NULL));
    if((st=player(playList))!=OK)
        return EXIT_FAILURE;

    return EXIT_SUCCESS;

}

/*prints the main menu of the program*/
void printMenu(){

    puts(OPT_PLAY_IN_ORDER);
    puts(OPT_PLAY_RANDOM);
    puts(OPT_PLAY_RECORD);
    puts(OPT_ADD_RECORD);
    puts(OPT_SEARCH_RECORD);
    puts(OPT_LIST_RECORD);
    puts(OPT_EXIT);

}

/*the program itself, depending on the input number continues with the selected function */
status_t player (char ** playList){
    int record_choice,max_random,choice;
    char aux[MAX_RECORD];
    char * record;
    char * keyword;
    char * err_library[] = {MSG_INPUT_ERROR, MSG_EMPTY_LIST, MSG_FULL_LIST,MSG_ALREADY_IN,MSG_WRONG_NAME,MSG_RANDOM_ERR,MSG_EMPTY_SEARCH};
    status_t st=OK;

    do{
        printMenu();
        scanf("%d",&amp;choice);
    cleanstdin();
    if((st =menu_limit(choice))!=OK){
        fprintf(stderr, "%s\n",err_library[st]);
        return st;
    }


    switch(choice)
    {
        case(PLAY_IN_ORDER):
        {
            if((st=playOrder(playList))!=OK){
                fprintf(stderr,"%s \n", err_library[st]);
                }
            else 
                st = ST_EXIT;
            break;
        }
        case(PLAY_RANDOM):
        {
            puts(MSG_RANDOM_LIST);
            scanf("%d",&amp;max_random);
            cleanstdin();

            if((st=playRandom(max_random,playList))!=OK){
                fprintf(stderr,"%s\n",err_library[st]); 
                break;
            }
            st=ST_EXIT;
            break;
        }
        case(PLAY_RECORD):
        {
            if((st=printPlaylist(playList))!=OK){
                fprintf(stderr, "%s\n",err_library[st]);
                break;
            }
            scanf("%d",&amp;record_choice);
            cleanstdin();
            if((st=validateChoice(record_choice,playList))!=OK){
                fprintf(stderr, "%s\n", err_library[st]);
                break; 
            }
            record = select_record(playList,record_choice);
            playRecord(record);

            st=ST_EXIT;
            break;
        }
        case(ADD_RECORD):{

            printf("%s \n",MSG_ADD_RECORD);
            fgets(aux,MAX_RECORD,stdin);
            aux[(strlen(aux)-1)]='\0';
            record= aux;
            if ((st=add_record(record,playList))!=OK)
                fprintf(stderr,"%s \n",err_library[st]);
            break;
        }
        case (SEARCH_RECORD):
            puts(MSG_SEARCH_RECORD);
            fgets(aux,MAX_RECORD,stdin);
            keyword= aux;
            if((st =search_record(keyword,playList))!=OK)
                fprintf(stderr,"%s\n",err_library[st]);
            break;

        case (LIST_RECORD):
            if((st=printPlaylist(playList))!=OK){
                fprintf(stderr, "%s\n",err_library[st]);
                return st;
            }
            break;
        case(EXIT):
        {
            return ST_EXIT;
        }
        default:
        {
            fprintf(stderr,"%s\n",MSG_INPUT_ERROR);
            return ST_EXIT;
        }
    }  
}while (st != ST_EXIT);
return OK;
}

/*Checks that, having six functions, you don't put a 7*/
status_t menu_limit (int choice){

    if (choice &gt; MAX_MENU)
        return ST_INPUT_ERROR;
    return OK;
}

/*Plays the songs in the playlist in order*/
status_t playOrder(char ** playList){
    int i;
    char * record;

    for(i=0;playList[i] != 0 &amp;&amp; i &lt;MAX_PLAYLIST; i++){
        record = select_record(playList,i);
        playRecord(record);
    }
    if(i == 0)
        return ST_EMPTY_LIST;
    return OK;
}
 /*Checks that, having for example, 6 songs in the playlist, you don't put song n°7*/
status_t validateChoice(int choice,char ** playList){
    int i;

    for(i=0; playList[i] != 0 &amp;&amp; i &lt; MAX_PLAYLIST; i++);
    if(choice &gt; MAX_PLAYLIST || choice &gt; i)
        return ST_INPUT_ERROR;
    return OK;
}

/*Plays the playlist randomly */
status_t playRandom(int max_random,char ** playList){
    int i;
    status_t st;
    char * record;

    if ((st = validateChoice(max_random,playList)) != OK){
        return ST_RANDOM_ERR;
}
    for(i=0;i&lt;max_random;i++){
    record = select_record(playList,limit_random(MAX_PLAYLIST));
    playRecord(record);
}

    return OK;
}
/*print current playlist*/
status_t printPlaylist(char ** playList){
    int i;

    for(i=0;playList[i] != 0 &amp;&amp; i&lt; MAX_PLAYLIST ;i++){
        printf("%d -%s \n",i,playList[i]);
}
    if (i == 0)
        return ST_EMPTY_LIST;
    return OK;
}

 /*recieves an int and returns the corresponding song (this could be all inside of playrecord)*/
char * select_record (char ** playList, int i){

return playList[i];
}
 /*given a string plays the record using mpg123 this function was provided by the one who thought the assignment*/
void playRecord (char * record){
    char * cmd[3];
    int pid = fork();

    cmd[0]="mpg123";
    cmd[1]=record;
    cmd[2]=NULL;
    if (!pid)
        execvp(cmd[0],cmd);
    waitpid(pid,NULL,0);
}

/*Initializes the array with 0s*/
void initialize_array(char ** playList){
    int i;

    for(i=0;i&lt;MAX_PLAYLIST;i++){
        playList[i]=0;
    }
}

int limit_random(int max_random){

    int number;

    number = (rand()%(max_random+1));

    return number;

}

void cleanstdin (void){

    int c;

    do
    {
        c=getchar();
    }
    while(c != '\n' &amp;&amp; c != EOF);
}


 /*procedures and validations to add a song into the playlsit.*/
status_t add_record (char * record,char ** playList){
    status_t st;
    char aux[90];

    strcpy(aux,LIBRARY_DIRECTORY);
    strcat(aux,record);
    if (!access(aux, F_OK)){
        if((st=fill_playlist(record,playList))!=OK)
            return st;
    }
    else{
        return ST_WRONG_NAME;
    }
    return OK;

}

/* once the user inputs a name, and after making some validations adds the song to the playlist(first position that doesn't have a "0" on it)*/
status_t fill_playlist (char * record,char ** playList){
    int i;
    status_t st;
    char * aux;

    aux = record;
    for(i=0;playList[i] != 0 &amp;&amp; i &lt; MAX_PLAYLIST; i++){}
    if (i == MAX_PLAYLIST)
        return ST_FULL_PLAYLIST;
    if((st=check_repeat(aux,playList,i))!=OK)
        return st;

    playList[i]=aux;
    printf("%s\n",MSG_SUCCESFUL_ADD);
    return OK;
}
 /* Checks the songs which is about to be added isn't already in the playlist*/
status_t check_repeat (char * record,char ** playList,int length){
    int i;

    for (i=0; i &lt; length;i++){
        if(strcmp(record, playList[i])==0);
            return ST_ALREADY_IN;
    }
    return OK;
}

/*After receiving a keyword checks any match with all songs in the playlist*/

status_t search_record (char * keyword, char ** playList){
    int i,aux=0;


    for (i=0; playList[i] != 0 &amp;&amp; i &lt; MAX_PLAYLIST; i++){
        if(strstr(keyword, playList[i])!= NULL){
            printf("%s\n",playList[i]);
        aux++;
        }
    }
    if(i==0)
        return ST_EMPTY_LIST;
    if(aux == 0)
        return ST_EMPTY_SEARCH;
    printf("%s%s\n",MSG_SEARCH,keyword);
    return OK;
}
</code></pre>

<p>I'm pretty sure the problem is in this function: </p>

<pre><code>   status_t add_record (char * record,char ** playList){
    status_t st;
    char aux[90];

    strcpy(aux,LIBRARY_DIRECTORY);
    strcat(aux,record);
    if (!access(aux, F_OK)){
        if((st=fill_playlist(record,playList))!=OK)
            return st;
    }
    else{
        return ST_WRONG_NAME;
    }
    return OK;

}

/* once the user inputs a name, and after making some validations adds the song to the playlist(first position that doesn't have a "0" on it)*/
status_t fill_playlist (char * record,char ** playList){
    int i;
    status_t st;
    char * aux;

    aux = record;
    for(i=0;playList[i] != 0 &amp;&amp; i &lt; MAX_PLAYLIST; i++){}
    if (i == MAX_PLAYLIST)
        return ST_FULL_PLAYLIST;
    if((st=check_repeat(aux,playList,i))!=OK)
        return st;

    playList[i]=aux;
    printf("%s\n",MSG_SUCCESFUL_ADD);
    return OK;
}
 /* Checks the songs which is about to be added isn't already in the playlist*/
status_t check_repeat (char * record,char ** playList,int length){
    int i;

    for (i=0; i &lt; length;i++){
        if(strcmp(record, playList[i])==0);
            return ST_ALREADY_IN;
    }
    return OK;
}
</code></pre>