简体   繁体   中英

How to return NULL pointer and a pointer to an element of an array of structures from a function?

I am a beginner in C and programming overall.

I have the following function: Students* printGrades() , which is of type pointer to the structure Students and an array of structures Students student[32] . Let's say the structure has data about students' names, ID, and grades.

As the task states, it should return either a NULL pointer or, a pointer to one student with the highest grades (I guess it is meant one element ie name, ID, the grade of 1 student) depending on a specific condition. Let's say I have sorted the array so that the highest student's data (name, ID, grade) stood at the top.

So the challenge I am facing is that I don't really know how to return them. What I did was:

if (a condition) {
    return NULL;
} else {
    return student;
}

I just returned NULL or student (I need the data of the first student in the array, I simply returned student , since in such cases, the first element is returned only and I thought it'd be enough to do it this way, as the function is a pointer itself). However, as my tutor said, it is wrong (specifically the 2 return commands) and he didn't give me any hint (though he hasn't taught it). I looked it up on the internet, but could not find the ways to do that - they are either not appropriate or complex methods we are not allowed to use yet.

So, could you tell me how I return a NULL pointer and a pointer to one student

Student* (pointer to value) and Student (value) are two different types in C. Your students array is defined Students student[32] which means each element in it is of type Student . If you defined it as Students *student[32] , then each of its elements would be Student* , but that's probably not what you need.

Now a pointer ( Student* ) can point to some value. It can also point to nothing, in this case it is NULL.

A value ( Student ) on the other hand is always there. It cannot be NULL.

Lets look at your array. Lets say you found that the student with the highest grades is at the position i in the array, ie it is student[i] .

When returning student[i] from a function, you return the by value which means you actually create a copy of student and return it.

What you want is to return by reference ie to return just the pointer to the student, not really copy its value.

So for this you use operator &. What you do is return &student[i] .

/* Here you find the 'i' of the student with the highest grade */
if (/*a condition*/) {
    return NULL;
} else {
    return &student[i];
}

Here is a little function which performs somthing similar. It avoids some idiomatic C constructs. You should be able to apply it to your problem.

/*-
     struct S *NewS(void) - return a new S; null if exhausted.
 */
#define NS 100
static struct S Stab[NS];
static int  sp;

struct S *NewS() {
    struct S *s;
    if (sp < NS) {
        s = Stab + sp;
        sp += 1;
    } else {
        s = NULL;
    }
    return s;
}

A pointer's value is the address of the object it points to or NULL .

For your problem, just return the student's address, like this: return &student;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM