0

This time it's about using a struct as a parameter. In this assignment I have to create a FIFS queue system. The static person queue is of the size 10. It means that just 10 persons can be on the queue.

I also have variables head, tail and nbr_elem of type static. The tail variable is used to add the person in a specific position. And vice_versa for the variable head. nbr_elem is the amount of persons in the queue. I think you don't need more information to help me. Now to the question:

How can I use a struct as a parameter in a function? This is what I have done so far:

    static person queue[QUEUE_MAX_SIZE];
static int head = 0, tail = 0, nbr_elem = 0;
 struct  person
{
    char first_name[7];
    char sur_name[10];
    char pers_nbr[10];

};


void person_info(struct person p1){


    /*printf("First name: ");*/
    scanf("%s", &p1.first_name[7]);
   /* printf("Last name: ")*/;
    scanf("%s", &p1.sur_name[10]);
    /*printf("Id-number: ");*/
    scanf("%s", &p1.pers_nbr[10]);
}

everything is fine so far but now I have to put struct person p1 in the queue by first copying the inf using the strcpy-function and then putting it to the array person queue[QUEUE_MAX_SIZE] i guess(?)

 void enqueue( person queue[QUEUE_MAX_SIZE])
{
    queue[person_info(p1.first_name[7])];
}

I don't even know if this is a good start of the function enqueue so I need some help here. And how can I use the static person queue[QUEUE_MAX_SIZE] in the function?

I also have a header-file queue.h that contains the typedef person. The header-file is then called to queue.c which is this file.

1
  • You are updating a copy of your original structure. You need to return the modified structure from your function (or go with Joachim's answer). Commented Nov 9, 2013 at 17:57

1 Answer 1

1

With the function

void person_info(struct person p1)

the structure is passed by value, that is a copy is made and you modify only this copy. You want to pass it by reference, which in C is done by passing as a pointer:

void person_info(struct person *p1)

You scanf calls in the function is very wrong by the way. It will write beyond the arrays in the structure instance. Just use e.g.

scanf("%6s", p1.first_name);

Continuing reading your question, it seems you have some very basic misunderstandings of how to use arrays, so I suggest you go back and re-ready a chapter about arrays and array indexing in the nearest book or tutorial.

In fact, considering your basic misuse of arrays, I doubt you will understand a concept such as pointers immediately, so I recommend you go and read some books and tutorials about that subject as well.

Sign up to request clarification or add additional context in comments.

1 Comment

yeah I realized now that the scanf is wrong, there shouldn't be any [7] at the end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.