Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C C#
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;
}
Posted 14-Dec-12 4:46am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
NewStudent.number
//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
  Permalink  
Comments
steven8Gerrard at 16-Dec-12 4:55am
   
Thanks . Much appreciated
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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; }
  Permalink  
v3
Comments
steven8Gerrard at 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)



Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 16 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100