構造体メンバーのサイズとオフセットを表示するCプログラムを作成します
問題
構造を定義し、メンバー変数のサイズとオフセットを表示するCプログラムを作成します
構造 −これは、単一の名前でグループ化された、さまざまなデータ型変数のコレクションです。
構造体宣言の一般的な形式
datatype member1;
struct tagname{
datatype member2;
datatype member n;
}; ここで、struct --keyword
tagname-構造の名前を指定します
member1、member2-構造を構成するデータ項目を指定します。
例
struct book{
int pages;
char author [30];
float price;
}; 構造変数
構造変数を宣言する方法は3つあります-
方法1
struct book{
int pages;
char author[30];
float price;
}b; 方法2
struct{
int pages;
char author[30];
float price;
}b; 方法3
struct book{
int pages;
char author[30];
float price;
};
struct book b; 構造の初期化とアクセス
メンバーと構造体変数の間のリンクは、メンバー演算子(または)ドット演算子を使用して確立されます。
初期化は次の方法で実行できます-
方法1
struct book{
int pages;
char author[30];
float price;
} b = {100, "balu", 325.75}; 方法2
struct book{
int pages;
char author[30];
float price;
};
struct book b = {100, "balu", 325.75}; 方法3(メンバー演算子を使用)
struct book{
int pages;
char author[30];
float price;
} ;
struct book b;
b. pages = 100;
strcpy (b.author, "balu");
b.price = 325.75; 方法4(scanf関数を使用)
struct book{
int pages;
char author[30];
float price;
} ;
struct book b;
scanf ("%d", &b.pages);
scanf ("%s", b.author);
scanf ("%f", &b. price); データメンバーを使用して構造を宣言し、それらのオフセット値と構造のサイズを出力してみてください。
プログラム
#include<stdio.h>
#include<stddef.h>
struct tutorial{
int a;
int b;
char c[4];
float d;
double e;
};
int main(){
struct tutorial t1;
printf("the size 'a' is :%d\n",sizeof(t1.a));
printf("the size 'b' is :%d\n",sizeof(t1.b));
printf("the size 'c' is :%d\n",sizeof(t1.c));
printf("the size 'd' is :%d\n",sizeof(t1.d));
printf("the size 'e' is :%d\n",sizeof(t1.e));
printf("the offset 'a' is :%d\n",offsetof(struct tutorial,a));
printf("the offset 'b' is :%d\n",offsetof(struct tutorial,b));
printf("the offset 'c' is :%d\n",offsetof(struct tutorial,c));
printf("the offset 'd' is :%d\n",offsetof(struct tutorial,d));
printf("the offset 'e' is :%d\n\n",offsetof(struct tutorial,e));
printf("size of the structure tutorial is :%d",sizeof(t1));
return 0;
} 出力
the size 'a' is :4 the size 'b' is :4 the size 'c' is :4 the size 'd' is :4 the size 'e' is :8 the offset 'a' is :0 the offset 'b' is :4 the offset 'c' is :8 the offset 'd' is :12 the offset 'e' is :16 size of the structure tutorial is :24
-
二次方程式の根を見つけるためのCプログラムを書く方法は?
問題 ソフトウェア開発手法を適用してC言語の問題を解決する 解決策 二次方程式ax2+bx+cの根を見つけます。 与えられた二次方程式には2つの根があります。 分析 入力 − a、b、c値 出力 − r1、r2値 手順 $ r_ {1} =\ frac {-b + \ sqrt {b ^ 2-4ac}} {2a} $ $ r_ {2} =\ frac {-b- \ sqrt {b ^ 2-4ac}} {2a} $ デザイン(アルゴリズム) 開始 a、b、cの値を読み取る d =b24acを計算します 0の場合 r1 =b + sqrt(d)/(2 * a) r2 =
-
Pythonでプログラムを作成して、特定のDataFrameのインデックスと列を転置します
入力 − DataFrameがあり、インデックスと列の転置の結果は、であると想定します。 Transposed DataFrame is 0 1 0 1 4 1 2 5 2 3 6 ソリューション1 DataFrameを定義する ネストされたリスト内包表記を設定して、2次元リストデータの各要素を反復し、結果に保存します。 result = [[data[i][j] for i in range(len(data))] for j in range(len(data[0])) 結果をDataFrameに変換します df2 = pd.DataFrame(