Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Creating Your First Struct | Introduction to Structs
C Structs
セクション 1.  4
single

single

bookCreating Your First Struct

メニューを表示するにはスワイプしてください

Using the visual cue at the end of the first chapter, let's create a data type to describe a chemical element:

main.c

main.c

copy
1234567
struct chemicalElement { char name[3]; int atomicNum; int valency; double mass; };
Note
Note

The mass and atomic number cannot be negative, so it would be more efficient to use the unsigned short int data type.

Now, you can create a chemical element. The code will look like this:

main.c

main.c

copy
123456789101112131415
#include "stdio.h" struct chemicalElement { char name[3]; int atomicNumber; int valency; double mass; }; int main() { struct chemicalElement silicon = {"Si", 14, 4, 28.08}; return 0; }

Access to struct fields will be discussed in the next chapter. Try to describe the geometric point A with a structure.

タスク

スワイプしてコーディングを開始

You are creating a structure to represent a point in a 2D space. Your goal is to store and display information about the point, including its name and coordinates.

The structure Point should contain:

  • A character array for the point's name — it must contain only one letter (for example, "A" or "B"). To store this single letter and the null terminator \0, declare the array as name[2].
  • Two integer fields representing the x and y coordinates.

In the main function:

  • Create a variable of type Point.
  • Initialize it using curly braces {} with the name and coordinate values.

解答

Switch to desktop実践的な練習のためにデスクトップに切り替える下記のオプションのいずれかを利用して、現在の場所から続行する
すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  4
single

single

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

some-alt