C编程基本存在疑虑 [英] C programming basic doubts

查看:87
本文介绍了C编程基本存在疑虑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我最近看到的结构语法令人困惑。有结构功能吗?下面提到的C程序具有函数存储类型作为结构,它不是由终止; 。有人可以解释我或指导我这个

  struct  resource * platform_get_resource( struct  platform_device * dev,
unsigned int 类型,unsigned int num)
{
int i;

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

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



解决方案

这个单词struct意味着一种新的类型我在其范围内创建的数据。这意味着在一个名称下收集了很少的基本类型,如int,char,short,long等。在这种情况下,新类型名称是resource。这意味着当您在内存中创建类型为resource的对象时,为每个基本类型保留足够的字节。这段代码可能是用C语言编写的.c和c ++之间的差异就是当你在C中使用结构时,你需要使用结构字。在c ++资源中,您将需要使用新类型。

  struct 学生
{
int 数字;
char [ 100 ] FirstName;
char [ 100 ] SecondName;
char [ 100 ] ThirdName;
char [ 100 ]电话号码 // 你不能在int中的数字之间保留 - ;)
double AverageGrade;
}; // } StudentList [40];您可以在//声明后立即定义类型为Student的贵重物品

学生NewStudent;



这是最常用的示例之一。这是创建结构的方式。名称是Student。每次在名称的内存中创建此类型的成员时,将为FirstName,SecondName,ThirdName.PhoneNumber,number和AverageGrade的值保留位置。您可以使用操作员设置/获取这些值。(点)

这意味着如果您有一个新学生,您将使用该行创建新学生

学生NewStudent; 



你可以这样设置/获取他的号码

 NewStudent.number 
// 例如
NewStudent.number = 12345
NewStudent.AverageGrade = 6 00





所以你问题的答案。这就像一个名为platform_get_resource的函数,它有三个参数,一个是类型指针,一个是资源类型和两种基本类型。它们并不有趣。这个函数的结果将是一个指向类型资源的指针(对于指针而不是操作符。你需要我最喜欢的 - > Suc *它C#)这就是为什么你有这个奇怪的dev-> num_resources。

第一行未终止;因为它的函数定义和这个struct资源是函数返回的数据的类型


这是一个返回指向结构的指针的函数。



通常,程序员会使用类似 typedef struct resource {...}资源的typedef; 然后只需使用该名称编写代码。



基本上用 typedef struct A {...} B; 以下内容相同:



  struct  A * f(){静态 A a;  return & a; } 
B * f(){静态 B b; return & b; }


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;
}


解决方案

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


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; }


这篇关于C编程基本存在疑虑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆