當前位置:首頁 » 編程語言 » c語言結構體指針數組

c語言結構體指針數組

發布時間: 2023-01-06 11:09:49

『壹』 c語言的指針結構體數組問題

#include<stdio.h>
#include<string.h>

structstu{

intnum;
charname[20];
charsex;
floatscore;
}boy[5]={{101,"liping",'m',45},
{102,"zhangping",'m',62.5},
{103,"hefang",'m',92.5},
{104,"chengling",'f',87},
{106,"wangming",'m',58}};
structnode
{
structstunum;
structnode*next;
};

main()
{
inti=0,bjg=0;
structnodes[5]={0};
for(i=0;i<5;i++)
{
s[i].num.num=boy[i].num;
strcpy(s[i].num.name,boy[i].name);
s[i].num.sex=boy[i].sex;
s[i].num.score=boy[i].score;
s[i].next=&s[i+1];
}
s[4].next=NULL;
printf("不及格: ");
for(i=0;i<5;i++)
{
if(s[i].num.score<60)
{
bjg++;
printf("[%d][%s][%c][%.2f] ",s[i].num.num,s[i].num.name,s[i].num.sex,s[i].num.score);
}
}
printf("不及格人數【%d】 ",bjg);
}
不及格:
[101][liping][m][45.00]
[106][wangming][m][58.00]
不及格人數【2】
Pressanykeytocontinue

不貼代碼我就不調了自己寫了個

不說別的光是malloc也不對啊你不mallocstructnode嗎?
不mallocstructnode你的next指針存哪裡?
至於鏈表賦值我就不看了

『貳』 c語言中數組指針和結構體指針問題

樓上都說的很不錯。針對補充,我說兩句
結構體名字不是指針,結構體數組的名字才是指針
就好像int x;.....x不是指針,int x[2].....x是常量指針
同樣 struct aa{};...aa x;...x不是指針,aa x[3]....x是常量指針
------------------------------------------------------
對於數組int a[2]={25,32},我們通常把a看成是指針
但實際上,這個指針並不存在的,a代表這個數組的首地址,只有才參與表達式運算時,才暫時轉化為常量指針(不可更改)
所以
&a,取出的這個實際不存在的指針的地址,同數組第一個元素的地址是一樣的
---------------------------------------------------
換個例子,如果你這么定義
int a[3],*p=a;
printf("%d",&p);........就是p的自身地址,類似你上面的xxxx
printf("%d",p);..........就是數組a的首地址,類似你上面的yyyy
---------------------------------------------------
總結
你定義一個普通指針p的話,p和&p是不同的
你定義一個數組a的話,a和&a是相同的,因為C中只是把a看做常量指針,而不是實際存在的

『叄』 C語言編程指向結構體數組的指針

下面的程序是我以前寫的,你稍微改改就能符合你的要求了

#include<stdio.h>
#include<malloc.h>
typedefstructst
{
charname[20];
intchinese;
intmath;
intenglish;
floataverage;
}student;
voidswap(student*a,student*b)
{
studenttemp=*a;
*a=*b;
*b=temp;
}
voidsort(student*array,intn)
{
inti,j,flag;
for(i=0;i<n-1;i++)
{
flag=1;
for(j=0;j<n-i-1;j++)
{
if(array[j].average>array[j+1].average)
{
swap(array+j,array+j+1);
flag=0;
}
}
if(flag)
break;
}
}
voidprint(student*array,intn)
{
inti;
printf("姓名 語文 數學 英語 平均成績 ");
for(i=0;i<n;i++)
{
printf("%s %d %d %d %f ",array[i].name,array[i].chinese,
array[i].math,array[i].english,array[i].average);
}
}
intmain()
{
intnumber=9;
inti;
student*stu=(student*)malloc(sizeof(student)*9);
for(i=0;i<number;i++)
{
printf("請輸入第%d個學生的姓名及成績(姓名語文數學英語成績以空格隔開): ",i+1);
scanf("%s%d%d%d",(*(stu+i)).name,&(*(stu+i)).chinese,
&(*(stu+i)).math,&(*(stu+i)).english);
(*(stu+i)).average=((*(stu+i)).chinese+
(*(stu+i)).math+(*(stu+i)).english)/(float)3.0;
}
print(stu,number);
sort(stu,number);
print(stu,number);
free(stu);
return0;
}

『肆』 C語言結構體指針數組怎麼聲明

我說明寫在案例的備注里,你參考吧。

#include<stdio.h>
typedefstructst
{
intid;
}ST,*STP;//先定義類型ST是結構類型STP是結構指針類型
intmain()
{
STPst[2];//這里st就是你要的結構指針數組
STst1,st2;//這里我定義了2個結構變數,並賦值,讓指針數組的元素分別指向這兩個變數地址
st1.id=1;st2.id=2;
st[0]=&st1;
st[1]=&st2;
printf("%d,%d ",st[0]->id,st[1]->id);//列印指針數組指向地址內容
return0;
}

『伍』 C語言編程問題:結構體數組及指針

struct student *list;
int count = ReadStudentInfo("假設這是文件名", &list);
這個函數是這樣被調用。
裡面應該這樣寫
int ReadStudentInfo(const char* filename, struct student** pStudents)
{
*pStudents = (struct student*)malloc(sizeof(** pStudents) * n);//假設有n個學生
}

『陸』 C語言結構體數組的定義

所謂結構體數組,是指數組中的每個元素都是一個結構體。在實際應用中,C語言結構體數組常被用來表示一個擁有相同數據結構的群體,比如一個班的學生、一個車間的職工等。

結構體可以存儲不同的數據類型,將他們互相聯系起來。結構體數組可以連續存儲多個結構體,和數組作用相似。比如想定義同一個最小外接矩形的四個坐標值,並給予這個矩形一個特徵編號。當需要存儲多個最小外接矩形的信息時,就需要動態申請一個結構體數組。

struct 結構體類型名{類型名 成員名;類型名 成員名;……},先聲明結構體類型,再定義結構體變數名。聲明結構體類型,不分配空間,定義結構體類型變數,就要分配內存空間。

(6)c語言結構體指針數組擴展閱讀:

結構體數組指針類型:

一個結構體變數的指針就是該變數所佔據的內存段的起始地址。可以設一個指針變數,用來指向一個結構體變數,此時該指針變數的值是結構體變數的起始地址,指針變數也可以用來指向結構體數組中的元素。

1、類型一:

指向結構體變數的指針引用結構體變數中的成員。

2、類型二:

用結構體變數和指向結構體變數的指針構成鏈表,鏈表是一種常見的重要的數據結構。鏈表有一個「頭指針」變數,以head表示,它存放一個地址。該地址指向一個元素。

鏈表中的每一個元素稱為「結點」,每個結點都應包括兩個部分:

一是用戶需要用的實際數據,

二是下一個結點的地址。

鏈表中各元素在內存中的存儲單元可以是不連續的。要找某一元素,可以先找到上一個元素,根據它提供的下一元素地址找到下一個元素。這種鏈表的數據結構,必須利用結構體變數和指針才能實現。

可以聲明一個結構體類型,包含兩種成員,一種是用戶需要用的實際數據,另一種是用來存放下一結點地址的指針變數。

參考資料來源:網路—結構體類型—數組

『柒』 c語言結構體中指針數組怎樣賦值

看Unix/Linux上的man:
Standard
C
Library
Functions
gets(3C)
NAME
gets,
fgets
-
get
a
string
from
a
stream
SYNOPSIS
#include
char
*gets(char
*s);
char
*fgets(char
*s,
int
n,
FILE
*stream);
DESCRIPTION
The
gets()
function
reads
bytes
from
the
standard
input
stream
(see
intro(3)),
stdin,
into
the
array
pointed
to
by
s,
until
a
newline
character
is
read
or
an
end-of-file
con-
dition
is
encountered.
The
newline
character
is
discarded
and
the
string
is
terminated
with
a
null
byte.
....
gets是標准庫函數,要求傳入的是一個數組的地址。其實是要求caller應當分配空間給他。你的結構體中指針數組指向了已經分配好的內存嗎?如果沒有就別想了。想用就自己先給他空間

『捌』 c語言如何用指針指向結構體數組

#include<stdio.h>

int main()

{ struct st

{ int id;

char name[20];

}*p,stu[10];

int i;

for(p=stu; p<stu+3; p++)

scanf("%d%s",&p->id,p->name);

for(p=stu; p<stu+3; p++)

printf("%d %s ",p->id,p->name);

return 0;

}

『玖』 C語言結構體指針,結構體數組 之間的賦值問題。

沒有問題呀 這是我的測試代碼
#include<stdio.h>
#include<string.h>
typedef struct ST_Jpeg_Data
{
char Buff[4];
char index;
long songIndex;
long checkIndex;

}t_t;
int main()
{
t_t a[10]={0};
t_t* t=NULL;
t=a;
strcpy(t->Buff,"ab");
t->index='b';
t->songIndex=1;
t->checkIndex=2;
t++;
strcpy(t->Buff,"cd");
t->index='d';
t->songIndex=3;
t->checkIndex=4;
printf("%s\t%c\n",a[0].Buff,a[0].index);
printf("%s\t%c\n",a[1].Buff,a[1].index);
return 0;
}

『拾』 c語言怎麼讓一個數組指向一個結構體指針

指向結構體指針的只能是另一個指針.
數組除非是指針數組.否則是無法做到的.
可以反過來,結構體指針指向數組.做一個強制轉換即可.
比如
struct A * p =(struct A*)a;
其中a是數組名.

熱點內容
隨機啟動腳本 發布:2025-07-05 16:10:30 瀏覽:534
微博資料庫設計 發布:2025-07-05 15:30:55 瀏覽:30
linux485 發布:2025-07-05 14:38:28 瀏覽:310
php用的軟體 發布:2025-07-05 14:06:22 瀏覽:760
沒有許可權訪問計算機 發布:2025-07-05 13:29:11 瀏覽:436
javaweb開發教程視頻教程 發布:2025-07-05 13:24:41 瀏覽:722
康師傅控流腳本破解 發布:2025-07-05 13:17:27 瀏覽:246
java的開發流程 發布:2025-07-05 12:45:11 瀏覽:696
怎麼看內存卡配置 發布:2025-07-05 12:29:19 瀏覽:288
訪問學者英文個人簡歷 發布:2025-07-05 12:29:17 瀏覽:837