返回結構體指針c語言
發布時間: 2023-01-17 20:18:51
㈠ c語言函數能不能返回結構體
c語言函數可以返回結構體,操作方法如下:
1、首先在電腦中打開visual studio新建項目,給這個結構體取個名字叫student。
㈡ C語言返回結構的指針問題
直接把結構體變數傳遞參數是傳值,
st *fun(struct st x)
這里的形參x其實是傳遞的實參y的拷貝,這和形參不能影響實參是同樣的道理。而且x在fun函數結束後就被系統回收了,取得的地址當然就成野指針了,所以應該用結構體指針傳遞參數:
#include"stdio.h"
#include"string.h"
structst
{
inta;
charb[10];
};
voidfun(structst*px)
{
px->a=100;
strcpy(px->b,"99999");
}
intmain()
{
structsty,*p;
p=&y;
y.a=999;
strcpy(y.b,"0");
printf("y.a=%dy.b=%s ",y.a,y.b);
fun(p);
printf("(*p).a=%d(*p).b=%s ",(*p).a,(*p).b);
}
㈢ 求解一個c語言函數返回結構體指針的問題
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<fstream>
#include<stdlib.h>
usingnamespacestd;
typedefstructpoint{//結構體設計
intA,B,C,a,b,c,id,n;
}point;
point*ReadFile(){
staticpointss[110];
chardata[50]={'