成都创新互联网站制作重庆分公司

c语言函数结构体数组指针 c语言结构体指针做函数参数

C语言编程问题:结构体数组及指针

struct student *list;

成都创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于成都网站建设、做网站、任城网络推广、微信小程序定制开发、任城网络营销、任城企业策划、任城品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联公司为所有大学生创业者提供任城建站搭建服务,24小时服务热线:18980820575,官方网址:www.cdcxhl.com

int count = ReadStudentInfo("假设这是文件名", list);

这个函数是这样被调用。

里面应该这样写

int ReadStudentInfo(const char* filename, struct student** pStudents)

{

*pStudents = (struct student*)malloc(sizeof(** pStudents) * n);//假设有n个学生

}

C语言编程指向结构体数组的指针

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

#include stdio.h

#include malloc.h

typedef struct st

{

char name[20];

int chinese;

int math;

int english;

float average;

}student;

void swap(student *a, student *b)

{

student temp = *a;

*a = *b;

*b = temp;

}

void sort(student *array, int n)

{

int i, 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;

}

}

void print(student *array, int n)

{

int i;

printf("姓名\t语文\t数学\t英语\t平均成绩\n");

for (i = 0; i  n; i++)

{

printf("%s\t%d\t%d\t%d\t%f\n", array[i].name, array[i].chinese,

array[i].math, array[i].english, array[i].average);

}

}

int main()

{

int number = 9;

int i;

student *stu = (student *)malloc(sizeof(student) * 9);

for (i = 0; i  number; i++)

{

printf("请输入第%d个学生的姓名及成绩(姓名 语文 数学 英语成绩以空格隔开):\n", 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);

return 0;

}

C语言函数怎么传结构体指针

#include stdio.h

#include stdlib.h

#define LINE_MAX 80

struct body{

char data[100]; //要定义成数组才可以,不然,还要去分配内存

int num;

};

void create(struct body *bd); //结构体定义后,才能使用结构体类型,所以,移动到定义之后

int main(int argc, char *argv[]) {

int choose;

struct body *bd;

bd = (struct body*)malloc(sizeof(struct body));

while(1)

{

printf("        *******************欢迎来到文章编辑系统********************\n");

printf("1.   创建新文本\n");

printf("2.   统计文本\n");

printf("5.   退出系统\n");

printf("请选择你需要的功能的序号:");

scanf("%d",choose);

switch(choose)

{

case 1:

printf("创建新文本\n");

create(bd);

continue;

case 2:

printf("统计文本\n");

continue;

case 5:

printf("谢谢您的使用!\n");

break;

default:

printf("请正确输入!\n");

continue;

}

if(choose == 5)

break;

}

return 0;

}

void create(struct body *bd)

{

printf("编辑文本,Enter键保存\n");

scanf("%s",bd-data);//结构体指针引用成员用-,  同时,格式串应该是%s

printf("您输入的文本是:%s\n",bd-data); //同上

}

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语言指向结构体的指针

首先你的linkman是struct person的数组

linkman本身就是个地址

可以直接传递到函数里的

如果你要用指针的话,就需要一个struct的二维指针,或者指针数组,如struct person **p;或者struct person (*p)[200];

还是建议你直接使用linkman传递地址。

另外,你要用linkman里面的name的话,如果是当字符串来使用的话,直接linkman[1]-name就可以了


网页名称:c语言函数结构体数组指针 c语言结构体指针做函数参数
浏览路径:http://cxhlcq.com/article/hieijd.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部