0

I need to execute a function that returns array of a specified struct with variable length. Then I should loop through the returned array.

example struct :

typedef struct student {
  int id;
  char *name;
  int grade;
} Student;

function prototypes 1 :

Student *students; 
students = findStudentByGrade(int grade);

function prototypes 2 :

Student *students;
int retval = findStudentByGrade(&students, int grade);

I am bit confused on above methods. How can correctly define a array of struct? call function ? and loop through it untill end? Can some one help me please.

5
  • specified struct with variable length what do you mean by that ? Your structure size would be 2+2 [char pointer]+2 = 6 , you are just storing the pointer to char in struct. Commented Mar 17, 2014 at 7:52
  • @Praksh, specified struct mean the struct which I have shown in code. Student Commented Mar 17, 2014 at 7:56
  • 1
    @BhaveshMunot i wanted to clear the doubt before code :) Commented Mar 17, 2014 at 7:57
  • @BlueBird I understood it's struct Student, but how it is of variable length ? Commented Mar 17, 2014 at 8:00
  • @BlueBird Understood, you can use linked lists for this. Commented Mar 17, 2014 at 8:07

2 Answers 2

1

You can do this in this way. This code is working. I tested in CodeLite.

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

typedef struct student {
    int id;
    char *name;
} Student;

Student *findStudent(int *asize, const int grade);

int main(void)
{
    Student *stds;
    int asize = 0;
    stds = findStudent(&asize, 5);
    int i;  
    for (i = 0; i < asize; i++) {
        printf("ID : %i\n", stds[i].id);
    }
    return 0;
}

Student *findStudent(int *asize, const int grade)
{
    struct student *stds = malloc(sizeof(struct student) * 3);
    stds[0].id = 10;
    stds[1].id = 20;
    stds[2].id = 40;
    *asize = 3;
    return stds;
}

Get the array of struc as returned statement and pass an int variable with argument list to get the size back and simply loop through using a for loop. Or else you will find problem in looping. It is more easy to get the array size from the function which create the array.

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

2 Comments

mhh this makes sense,, what is the option if i want to calculate the array length uisng array itself without passing an extra argument?
@BlueBird you cannot since you are returning a pointer, the only way is either to return a value like fSazy did or allocate one extra, empty record as an end record then loop until you find that.
1

I mean this is quite a basic question, but:

Defining array of your structures would look like:

 int size = ...;
 Student *students = (Student*) malloc(sizeof(Student) * size);

Then just pass that to the function (both size and the array) and then just loop until i < size.

Ofcourse, don't forget to:

 free(students);

at the end.

Comments

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.