结构体
xt8_1
/*有参有返回值*/
#include <stdio.h>
main()
{
int fun(int);
printf("%d\n",fun(10));
}
int fun(int n)
{
int i,sum=0;
for(i=0;i<=n;i++)
if(i%2!=1) sum+=i;
return sum;
}
/*改为有参无返回值、无参有返回值、无参无返回值三种形式*/
xt8_2
/*按值单向传递*/
#include <stdio.h>
void fun(int x,int y)
{
printf("原 值:x=%d,y=%d\n",x,y);
x+=5;y*=3;
printf("运算后:x=%d,y=%d\n",x,y);
}
main()
{
int a,b;
scanf("%d%d",&a,&b);
printf("调用前:a=%d,b=%d\n",a,b);
fun(a,b);
printf("调用后:a=%d,b=%d\n",a,b);
}
/*解释运行结果*/
xt8_3
/*变量地址-指针变量*/
#include <stdio.h>
#define N 4
main()
{
int i=3;
void fun(int *);
fun(&i);
printf("%d\n",i);
}
void fun(int *p)
{
*p=10;
}
/*解释运行结果*/
xt8_4
/*数组名-指针变量*/
#include <stdio.h>
#define N 4
main()
{
int a[]={1,3,5,7},i;
void fun(int *,int);
fun(a,N);
for(i=0;i<N;i++)
printf("%3d",a[i]);
printf("\n");
}
void fun(int *p,int n)
{
int i;
for(i=0;i<n;i++)
++(*p++);
}
/*解释运行结果*/
xt8_5
/*数组名-数组名*/
#include <stdio.h>
#define N 4
main()
{
int a[]={1,3,5,7},i;
void fun(int []);
fun(a);
for(i=0;i<N;i++)
printf("%2d",a[i]);
printf("\n");
}
void fun(int b[])
{
b[1]=b[0]+b[2];
}
/*解释运行结果*/
xt8_6
/*指针变量-指针变量*/
#include <stdio.h>
#define N 4
main()
{
int a[]={1,3,5,7},*p,i;
void fun(int [],int);
p=a;
fun(p,N);
for(i=0;i<N;i++)
printf("%3d",a[i]);
printf("\n");
}
void fun(int *q,int n)
{
int i;
for(i=0;i<n;i++)
*(q+i)+=i;
}
/*解释运行结果*/
xt8_7
/*z250 31*/
#include <stdio.h>
main()
{
union example
{
struct
{
int x,y;
}in;
int a[2];
}e={0,0};
e.a[0]=1;e.a[1]=2;
printf("%d,%d\n",e.in.x,e.in.y);
}
/*1,2*/
xt8_8
/*z249 25*/
#include <stdio.h>
union u_type
{
int i;
double x;
float f;
}u;
struct s_type
{
int i;
char x;
float f;
}s;
struct str_type
{
char str[100];
union u_type a[2];
}str;
main()
{
printf("%d,%d,%d\n",sizeof(s),sizeof(u),sizeof(str));
}
/*24,8,120*/
xt8_9
/*z249 25*/
#include <stdio.h>
union u_type
{
int i;
double x;
float f;
}u;
struct str_type
{
int j;
double y;
float f1;
}s;
main()
{
printf("%d,%d\n",sizeof(u),sizeof(s));
}
xt8_10
/*z249 25*/
#include <stdio.h>
main()
{
struct s_type
{
int i;
double x;
float f;
}s;
printf("%d,%d,%d,%d\n",sizeof(s.i),sizeof(s.x),sizeof(s.f),sizeof(s));
}
/*4,8,4 24?*/
xt8_11
/*z249 25*/
#include <stdio.h>
struct date
{
int year,month,day; };
struct s_type
{
int i;
char x;
float f;
struct date birthday;
}s;
main()
{
s.i=10;
s.birthday.year=1975;
printf("%d,%d\n",s.i,s.birthday.year);
}
/*24,8,120*/
xt8_12
/*z53 4.22*/
#include <stdio.h>
struct st_type
{
char name[10];
float score[3];
}s;
union u_type
{
short cat;
unsigned char ch;
struct st_type student;
}t;
main()
{
printf("%d,%d\n",sizeof(s),sizeof(t));
}
xt8_13
/*z53 4.22*/
#include <stdio.h>
#include <string.h>
struct st_type
{
char name[10];
float score[3];
}s;
main()
{
int i;
for(i=0;i<3;i++)
scanf("%f",&s.score[i]);
strcpy(s.name,"Zhang");
printf("%s\n",s.name);
for(i=0;i<3;i++)
printf("%.2f ",s.score[i]);
printf("\n");
}