Click here to Skip to main content
15,945,603 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello i recently saw a structure syntax which was confusing . Is there any structural functions? The below mentioned C program has functions storage type as structure and it is not terminated by ; . Can someone explain me or guide me to a nice tutorial on this
struct resource *platform_get_resource(struct platform_device *dev,
                       unsigned int type, unsigned int num)
    int i;

    for (i = 0; i < dev->num_resources; i++) {
        struct resource *r = &dev->resource[i];

        if (type == resource_type(r) && num-- == 0)
            return r;
    return NULL;

Updated 11-Aug-18 6:02am

This word struct means that a new type of data i created in its range. It means that few basic types like int, char, short, long and so on are gathered under one name. In this case the new types name is resource .This means that when you create a object of type resource in the memory are reserved enough bytes for each of the basic types. This code is probably written in C .One of the differences between c and c++ is when you use structs in C you need to use the struct word aswell. In c++ resource will be all you need to use the new type.
struct Student
  int number;
  char[100] FirstName;
  char[100] SecondName;
  char[100] ThirdName;
  char[100]Phone Number //you cant keep the "-" between the digits in an int ;)
  double AverageGrade;
};  // }StudentList [40]; You can define valuables of type Student right after the //declaration

Student NewStudent;

This is one of most used examples . This is the way of creating a struct .The name is Student. Each time you create a member of this type in the memory under the name will be reserved place for the values of FirstName, SecondName , ThirdName.PhoneNumber, number and AverageGrade. You can set/get these values by using the operator .(dot)
This means if you have a new student you will create the new student with the line
Student NewStudent;

and you can set/get his number this way
//For example
NewStudent.number = 12345
NewStudent.AverageGrade = 6.00

So the answer of your question . This like shows a function called platform_get_resource with three parameters one of type pointer to type resource and 2 basic types .They aren't interesting. The result of this function will be a pointer to type resource (for a pointer instead of the operator . you need my favorite -> Suc* it C#)That's why you have in the for this strange dev->num_resources.
The first line is not terminated by ; because its a definition of the function and this struct resource is the type of the data returned by the function
Share this answer
steven8Gerrard 16-Dec-12 4:55am    
Thanks . Much appreciated
This is a function that returns a pointer to a structure.

OftenC programmer would use a typedef like typedef struct resource { ... } resource; and will then simply write code using that name.

Essentially with typedef struct A { ... } B; the following would be equivalent:

struct A * f() { static A a; return &a; }
B * f() { static B b; return &b; }
Share this answer
steven8Gerrard 16-Dec-12 4:55am    
Thanks . Much appreciated

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900