c語言 結構體與指標

2021-10-25 04:13:20 字數 1436 閱讀 8970

/*

1.建立結構體

2.結構體成員賦值方式

3.結構體與指標

4.結構體的使用,訪問成員方式:

指標變數->成員名方式:pst->age; pst所指向的結構體變數中的age這個成員;

結構體變數名.成員名方式:st.age; st這個結構體變數所指向的age這個成員;

*/#include

#include

struct student

;voidf(

struct student* pst1)

;int

main()

;//已經為st分配好了記憶體空間//

printf

("%d\t%s\t%s\n"

,st.age,st.name,st.xuehao)

;printf

("\n----------------------------------------\n");

st.age=22;

"李四"; //結構體中,這樣單獨賦值時,字串陣列賦值不能直接賦 //

//[error] incompatible types in assignment of 'const char [5]' to 'char [12]'//

strcpy

(st.name,

"李四");

strcpy

(st.xuehao,

"123456");

printf

("%d\t%s\t%s\n"

,st.age,st.name,st.xuehao)

;printf

("\n----------------------------------------\n");

struct student* pst;

//結構體指標變數 //

pst=

&st;

//結構體指標變數,pst指向了st這個結構體變數 ;//

pst->age=23;

//pst->age等價於(*pst).age;而(*pst).age等價於st.age,所以pst->age==st.age;//

strcpy

(pst->name,

"王五");

strcpy

(pst->xuehao,

"1234567");

printf

("%d\t%s\t%s\n"

,pst->age,pst->name,pst->xuehao)

;printf

("\n----------------------------------------\n");

f(&st)

;return0;

}voidf(

struct student* pst1)

結構體指標,C語言結構體指標詳解

結構體指標,可細分為指向結構體變數的指標和指向結構體陣列的指標。前面我們通過 結構體變數名.成員名 的方式引用結構體變數中的成員,除了這種方法之外還可以使用指標。前面講過,student1 表示結構體變數 student1 的首位址,即 student1 第乙個項的位址。如果定義乙個指標變數 p 指...

C語言結構體與結構體指標的使用

c語言結構體 struct 是由一系列具有相同型別或不同型別的資料構成的資料集合。說的通俗一點就是乙個集合。c語言是一門面向過程的程式語言,而結構體的使用在某些層次上跟物件導向有點異曲同工之處了。下面回歸正題,學習一下結構體以及結構體指標的知識。一 結構體變數的定義和初始化 1 首先我們來看一下結構...

C語言結構體與指標難點

include include include int main stu1 pstu stu1 讀取結構體成員的值 printf s的學號是 d,年齡是 d,在 c組,今年的成績是 1f!n pstu name,pstu num,pstu age,pstu group,pstu score prin...