结构化程序设计循环结构例子_C程序设计中的结构

结构化程序设计循环结构例子

结构化程序设计循环结构例子

C provides primitive data types like int , char , float etc. But in order to solve real-world problems, we need more than these types. Newer object-oriented languages have a lot of features to implement real-world situations. For example, C++ provides Object Oriented features where we can implement real-world objects. C programming lacks these features but provides type. A struct is used to provide composite data type which can provide multiple primitive types.

C提供了诸如intcharfloat等原始数据类型。但是,为了解决现实世界中的问题,我们不仅需要这些类型。 较新的面向对象的语言具有许多功能,可以实现实际情况。 例如, C++提供了Object Oriented功能,我们可以在其中实现真实世界的对象。 C编程缺少这些功能,但是提供了类型。 结构用于提供可提供多种基本类型的复合数据类型。

定义结构 (Defining Structure)

Defining a struct is similar to a union. We will provide the elements we want to store in a struct and the name of the struct like the following the syntax.

定义结构类似于联合。 我们将提供要存储在结构中的元素以及该结构的名称,如以下语法所示。

struct [STRUCT_NAME] { 
MEMBER
... 
} STRUCT_VARIABLES];

The syntax can provide some hint about struct but the best way to understand and learn is defining struct as a real-world example. In this example, we will create a struct named Student which have the following members?

语法可以提供有关struct的一些提示,但是理解和学习的最佳方法是将struct定义为实际示例。 在此示例中,我们将创建一个名为Student的结构,该结构具有以下成员?

  • name holds student name as char variables

    name将学生name保留为char变量

  • id holds student id as int

    id将学生ID保留为int

We have used only two members to make things simple but there is no limit about the members other than memory.

我们仅使用两个成员使事情变得简单,但是除了内存之外,成员没有其他限制。

struct Student { 
   int id; 
   char name[20]; 
};

初始化结构 (Initialize Struct)

We can initialize new structs variables like below just providing the struct keyword with the struct name and the variable name we want to use. Here we create a struct named s1 with Student struct type.

我们可以像下面这样初始化新的structs变量,只需为struct关键字提供struct名称和我们要使用的变量名即可。 在这里,我们创建一个名为s1Student结构类型的结构。

struct Student s1;

访问结构成员 (Accessing Structure Members)

We have defined struct members id and name . We need to set and get these members values. We can simply access them with the struct variables name and the member name.

我们已经定义了struct成员idname 。 我们需要设置并获取这些成员值。 我们可以简单地使用结构变量名称和成员名称来访问它们。

#include <stdio.h> 
#include <string.h> 
 
struct Student { 
   int id; 
   char name[20]; 
}; 
 
int main( ) { 
 
   struct Student s1;       
 
   s1.id=123; 
   strcpy( s1.name, "Ahmet Ali"); 
 
   printf( "Studen ID : %i\n", s1.id); 
   printf( "Studen Name : %s\n", s1.name); 
 
   return 0; 
}

We have set the id with the following line

我们用以下行设置了id

s1.id=123;

We can also access the same syntax to the id variable like below.

我们还可以对id变量使用相同的语法,如下所示。

printf( "Studen ID : %i\n", s1.id);

结构即函数参数 (Structure As Function Arguments)

We have seen that structures provide good flexibility. We generally use structures to pass values to the functions. In this part, we will look at how can we pass the structure variable to the function. We need to define a struct parameter as function argument like defining a normal struct.

我们已经看到结构提供了良好的灵活性。 我们通常使用结构将值传递给函数。 在这一部分中,我们将研究如何将结构变量传递给函数。 我们需要将struct参数定义为函数参数,就像定义普通struct一样。

#include <stdio.h> 
#include <string.h> 
  
struct Student { 
   int id; 
   char name[20]; 
}; 
 
 
void print(struct Student s) 
{ 
   printf( "Studen ID : %i\n", s.id); 
   printf( "Studen Name : %s\n", s.name); 
} 
  
 
int main() { 
 
   struct Student s1;         
 
   s1.id=123; 
   strcpy( s1.name, "Ahmet Ali"); 
 
   print(s1); 
 
   return 0; 
}
LEARN MORE  Memcache Delete Operation with Python Example
通过Python示例了解更多Memcache删除操作

翻译自: https://www.poftut.com/c-structures/

结构化程序设计循环结构例子

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值