산업 제조
산업용 사물 인터넷 | 산업자재 | 장비 유지 보수 및 수리 | 산업 프로그래밍 |
home  MfgRobots >> 산업 제조 >  >> Industrial programming >> C 언어

C 구조 및 기능

C 구조 및 기능

이 자습서에서는 구조체 변수를 함수에 인수로 전달하는 방법을 배웁니다. 예제를 통해 함수에서 구조체를 반환하는 방법을 배우게 됩니다.

내장 유형의 변수와 유사하게 구조 변수를 함수에 전달할 수도 있습니다.

<시간>

함수에 구조체 전달

구조체를 함수에 전달하는 방법을 배우기 전에 이 자습서를 배우는 것이 좋습니다.

함수에 구조를 전달하는 방법은 다음과 같습니다.

#include <stdio.h>
struct student {
   char name[50];
   int age;
};

// function prototype
void display(struct student s);

int main() {
   struct student s1;

   printf("Enter name: ");

   // read string input from the user until \n is entered
   // \n is discarded
   scanf("%[^\n]%*c", s1.name);

   printf("Enter age: ");
   scanf("%d", &s1.age);

   display(s1); // passing struct as an argument

   return 0;
}

void display(struct student s) {
   printf("\nDisplaying information\n");
   printf("Name: %s", s.name);
   printf("\nAge: %d", s.age);
}

출력

Enter name: Bond
Enter age: 13

Displaying information
Name: Bond
Age: 13  

여기서 구조체 변수 s1 struct student 유형 생성됩니다. 변수는 display()로 전달됩니다. display(s1);를 사용하는 함수 성명서.

<시간>

함수에서 구조체 반환

함수에서 구조를 반환하는 방법은 다음과 같습니다.

#include <stdio.h>
struct student
{
    char name[50];
    int age;
};

// function prototype
struct student getInformation();

int main()
{
    struct student s;

    s = getInformation();

    printf("\nDisplaying information\n");
    printf("Name: %s", s.name);
    printf("\nRoll: %d", s.age);
    
    return 0;
}
struct student getInformation() 
{
  struct student s1;

  printf("Enter name: ");
  scanf ("%[^\n]%*c", s1.name);

  printf("Enter age: ");
  scanf("%d", &s1.age);
  
  return s1;
}	

여기에서 getInformation() 함수는 s = getInformation();을 사용하여 호출됩니다. 성명. 이 함수는 struct student 유형의 구조를 반환합니다. . 반환된 구조는 main()에서 표시됩니다. 기능.

getInformation()의 반환 유형은 또한 struct student입니다. .

<시간>

참조로 구조체 전달

참조로 구조체를 전달할 수도 있습니다(내장 유형의 변수를 참조로 전달하는 것과 유사한 방식). 계속 진행하기 전에 참조 자습서로 통과를 읽는 것이 좋습니다.

참조로 전달하는 동안 구조체 변수의 메모리 주소가 함수에 전달됩니다.

#include <stdio.h>
typedef struct Complex
{
    float real;
    float imag;
} complex;

void addNumbers(complex c1, complex c2, complex *result); 

int main()
{
    complex c1, c2, result;

    printf("For first number,\n");
    printf("Enter real part: ");
    scanf("%f", &c1.real);
    printf("Enter imaginary part: ");
    scanf("%f", &c1.imag);

    printf("For second number, \n");
    printf("Enter real part: ");
    scanf("%f", &c2.real);
    printf("Enter imaginary part: ");
    scanf("%f", &c2.imag);

    addNumbers(c1, c2, &result); 
    printf("\nresult.real = %.1f\n", result.real);
    printf("result.imag = %.1f", result.imag);
    
    return 0;
}
void addNumbers(complex c1, complex c2, complex *result) 
{
     result->real = c1.real + c2.real;
     result->imag = c1.imag + c2.imag; 
}

출력

For first number,
Enter real part:  1.1
Enter imaginary part:  -2.4
For second number, 
Enter real part:  3.4
Enter imaginary part:  -3.2

result.real = 4.5
result.imag = -5.6  

위 프로그램에서 세 개의 구조 변수 c1 , c2결과의 주소 addNumbers()로 전달됩니다. 기능. 여기 결과 참조로 전달됩니다.

결과일 때 addNumbers() 내의 변수 변경되면 결과 main() 내의 변수 그에 따라 기능도 변경됩니다.


C 언어

  1. C# 구조체
  2. C++ friend 함수 및 friend 클래스
  3. C 구조체
  4. C 구조체와 포인터
  5. C - 프로그램 구조
  6. C - 데이터 유형
  7. C - 상수 및 리터럴
  8. C - 기능
  9. C++ 오버로딩(연산자와 함수)
  10. 전기 변압기:기능, 구조 등