📜  结构指针

📅  最后修改于: 2021-05-30 15:55:18             🧑  作者: Mango

结构 指针定义为指向存储结构的存储块地址的指针,称为结构指针。以下是相同的示例:
例子:

struct point
{
   int value;
};

// Driver Code
int main()
{
    struct point s;
    struct point *ptr = &s;
    return 0;
}

下面是说明上述概念的程序:

C
// C program to illustrate the
// structure pointer
  
#include 
  
// Structure declaration for
// vertices
struct point {
    int x;
    int y;
};
  
// Strcuture declaration for
// rectangle
struct rect {
  
    // An object left is declared
    // with 'point'
    struct point left;
  
    // An object right is declared
    // with 'point'
    struct point right;
};
  
// Function to calculate area of
// the given rectangle
void areaOfRectangle(struct rect r)
{
    // Find the area of the rectangle
    // using variables of point
    // structure where variables of
    // point structure is accessed
    // by left and right objects
    int area
        = (r.right.x - r.left.x)
          * (r.right.y - r.left.y);
  
    // Print the area
    printf("%d", area);
}
  
// Driver Code
int main()
{
    // Initialize variable 'r'
    // with vertices of rectangle
    struct rect r = { { 0, 0 }, { 1, 1 } };
  
    // Function Call
    areaOfRectangle(r);
  
    return 0;
}


C++
// C++ program to illustrate the
// structure pointer
#include 
#include 
using namespace std;
  
// Structure declaration for
// vertices
struct point {
    int x;
    int y;
};
  
// Strcuture declaration for
// rectangle
struct rect {
  
    // An object left is declared
    // with 'point'
    struct point left;
  
    // An object right is declared
    // with 'point'
    struct point right;
};
  
// Function to calculate area of
// the given rectangle
void areaOfRectangle(struct rect r)
{
    // Find the area of the rectangle
    // using variables of point
    // structure where variables of
    // point structure is accessed
    // by left and right objects
    int area
        = (r.right.x - r.left.x)
          * (r.right.y - r.left.y);
  
    // Print the area
    cout << area;
}
  
// Driver Code
int main()
{
    // Initialize variable 'r'
    // with vertices of rectangle
    struct rect r = { { 0, 0 }, { 1, 1 } };
  
    // Function Call
    areaOfRectangle(r);
  
    return 0;
}


输出:
1
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”