Cで機能する引数として構造体のアドレスを渡す方法は?
構造体の値をある関数から別の関数に転送する方法は3つあります。それらは次のとおりです-
-
関数への引数として個々のメンバーを渡す。
-
構造体全体を引数として関数に渡す。
-
関数の引数として構造体のアドレスを渡す。
ここで、構造体のアドレスを関数の引数として渡す方法を理解しましょう。
-
構造体のアドレスは、引数として関数に渡されます。
-
関数ヘッダーの構造体へのポインターに収集されます。
利点
関数の引数として構造体のアドレスを渡すことの利点は次のとおりです-
-
コピーを再度作成する必要がないため、メモリを無駄にすることはありません。
-
関数は構造全体に間接的にアクセスして処理できるため、値を返す必要はありません。
例
次のプログラムは、構造体のアドレスを引数として関数-
に渡す方法を示しています。#include<stdio.h>
struct date{
int day;
char month[10];
int year;
};
int main(){
struct date d;
printf("enter the day,month and year:");
scanf("%d%s%d",&d.day,d.month,&d.year);
display(&d);
return 0;
}
void display(struct date *p){
printf("day=%d\n",p->day);
printf("month=%s\n",p->month);
printf("year=%d\n",p->year);
} 出力
上記のプログラムを実行すると、次の結果が得られます-
enter the day, month and year:20 MAR 2021 day=20 month=MAR year=2021
例
以下に、関数全体を引数として呼び出すことにより、構造と関数を示すCプログラムを示します。この関数呼び出し方法により、再度コピーして値を返す必要がないため、メモリを浪費することはありません。
#include<stdio.h>
//Declaring structure//
struct student{
char Name[100];
int Age;
float Level;
char Grade[50];
char temp;
}s[5];
//Declaring and returning Function//
void show(struct student *p){
//Declaring variable for For loop within the function//
int i;
//For loop for printing O/p//
for(i=1;i<3;i++){
printf("The Name of student %d is : %s\n",i,p->Name);
printf("The Age of student %d is : %d\n",i,p->Age);
printf("The Level of student %d is : %f\n",i,p->Level);
printf("The Grade of student %d is : %s\n",i,p->Grade);
p++;
}
}
void main(){
//Declaring variable for for loop//
int i;
//Declaring structure with pointer//
struct student *p;
//Reading User I/p//
for(i=0;i<2;i++){
printf("Enter the Name of student %d : ",i+1);
gets(s[i].Name);
printf("Enter the Age of student %d : ",i+1);
scanf("%d",&s[i].Age);
printf("Enter the Level of student %d :",i+1);
scanf("%f",&s[i].Level);
scanf("%c",&s[i].temp);//Clearing Buffer//
printf("Enter the Grade of student %d :",i+1);
gets(s[i].Grade);
}
//Assigning pointer to structure//
p=&s;
//Calling function//
show(&s);
} 出力
上記のプログラムを実行すると、次の結果が得られます-
Enter the Name of student 1 : Lucky Enter the Age of student 1 : 27 Enter the Level of student 1 :2 Enter the Grade of student 1 :A Enter the Name of student 2 : Pinky Enter the Age of student 2 : 29 Enter the Level of student 2 :1 Enter the Grade of student 2 :B The Name of student 1 is : Lucky The Age of student 1 is : 27 The Level of student 1 is : 2.000000 The Grade of student 1 is : A The Name of student 2 is : Pinky The Age of student 2 is : 29 The Level of student 2 is : 1.000000 The Grade of student 2 is : B
-
EnterキーをTkinterの関数にバインドするにはどうすればよいですか?
キーを押してそのキーで操作を処理することは、ボタンを介してトリガーできるイベントです。 バインドを使用してキーイベントをバインドできます tkinterアプリケーションのメソッド。 キーがトリガーされるたびに、キーイベントの特定の操作を発生させるハンドラーが呼び出されます。 バインド機能でEnterキーをトリガーする場合 、 bind(、Handler)を使用します 方法。 Enterキーには、 bind(、Handler)を使用します 機能。 例 #Import the tkinter library from tkinter import * #Create an instance
-
Windows 11 PC で IP アドレスを見つける方法
インターネット接続に問題がありますか?さまざまなソリューションを試しているときに、コンピューターの IP アドレスを知っていると便利です。 初心者向けに説明すると、IP アドレス (インターネット プロトコル アドレスの略) は、インターネット上でデバイスを識別する一意の数値アドレスです。機械の郵便番号と考えることができます。郵便番号が現実世界の住所を定義するのと同じように、IP アドレスはマシンのアドレスを定義し、相互の検索に役立ちます。 Windows 11 で IP アドレスを見つける方法は複数あります。まず、コマンド プロンプトを使用するなど、最も単純な方法から始めましょう。 1.