-1

My task is to store list of films in one array and store actor name in other array and make sure that film name is pointing to the respective actor. I'm using linked list. I'm facing difficulty in storing the address of the actor into the movie structure so that it can point to it. The code is in c. Please help!

struct movie
{
    char name[10];
    struct movie *data;
    struct movie *next;
};

typedef struct movie m;

m *insert(m *first, char actor[10], int i)
{
    m *cur;
    cur = (m *)malloc(sizeof(m));
    printf("\nEnter movie name: ");
    scanf("%s", cur->name);
    printf("\nEnter actor name: ");
    scanf("%s", actor);
    cur->data = &actor;
    cur->next = first;
    printf("\n%d", &actor);
    printf("\n%d", cur->data);
    printf("\n%s", actor);
    printf("\n%s", *cur->data);
    return (cur);
}

int main()
{
    m *first = NULL, *ptr = NULL, *ptr1;
    char actor[10];
    int i = 0, ch;
    first = (m *)malloc(sizeof(m));
    ptr = (m *)malloc(sizeof(m));
    do
    {
        printf("\n1.Enter movie: ");
        printf("\n2.Search movie: ");
        printf("\n3.Exit");
        printf("\nEnter your choice: ");
        scanf("%d", &ch);
        switch(ch)
        {
            case 1:
                first = insert(first, actor, i);
                break;
            case 2:
                if(first == NULL)
                {
                    printf("\nList Empty!");
                    break;
                }
                else
                {
                    printf("\nEnter movie name: ");
                    scanf("%s", ptr->name);
                    ptr1 = first;
                    while(ptr1->next != NULL)
                    {
                        if(strcmp(ptr->name, first->name)==0)
                        {
                            printf("\n%s", &ptr->data);
                            break;
                        }
                        else
                        {
                            printf("\nNot found");
                        }
                        ptr1 = ptr1->next;
                    }
                }
                break;
        }
    }while(ch != 3);
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
MVK059
  • 351
  • 3
  • 14
  • 1
    Please state what problem you are having, exactly. Also, I don't see any "array" of actors (I'm going to assume that when you say "array", you mean "list".) – ams Nov 28 '14 at 13:30
  • Please [do not cast](http://stackoverflow.com/q/605845/2173917) the return value of `malloc()`. – Sourav Ghosh Nov 28 '14 at 13:35
  • i suggest `struct movie *data;` --> `char actor[10];` – BLUEPIXY Nov 28 '14 at 13:56

1 Answers1

0

Ok, I assume you want to store the actor name on the data part of your movie struct. If this is the case, then it's best practice to use descriptive names for variables. So I would change the struct to

struct movie

{
    struct char name[10];
    struct actor_t *actor;  //we will define that later
    struct movie *next;
};

Ok, no you need to actually create that actor structure. Do you need to store anything other than the actor name? If you need just a name then you could simply do:

 struct actor_t 
{
       char name[10];
       struct actor_t *next;
}

I suppose that the actors should be in a list too (and not an array as you wrote), that's why you need a next pointer to navigate to all the actors.

(Tip: if this task is for educational purposes, then you are probably ok with that. It would be much more efficient though to utilize a hash table to search for an actor by name)

Each time you get (via scanf) an actor name, you must make some space for an actor list node.

struct actor_t *new_actor = malloc(sizeof(*new_actor));

Now that you ve created space for a new actor, copy the string that you just got from the input into the actor structure:

strncpy(new_actor->name, actor, strlen(actor));

Ok, you ve got your first actor now. Initially, it's the only node in the actors list, so you want to set the next pointer to null.

new_actor->next = NULL;

Supposing that you have already malloced space for a movie node, make that movie's actor point to the actor node you just created:

cur->actor = new_actor;

Now if you have another movie, with the same actor you could just point to that same actor like this:

next_movie->actor = new_actor; //or whatever nave you gave to the first actor node

A few thoughts: It does not seem normal for a movie to point to just one actor. Are you sure that is your task? Does your movie need to point to several actors? If yes, that is a completely different problem.

You must work this code out. I just gave you some ideas on how to accomplish some simple tasks, but basically you need some practice on list manipulation. And ofc get your hands dirty.

sestus
  • 1,875
  • 2
  • 16
  • 16