这个程序的目的是为了建立一个类似图书馆书籍的录入流程,设计这个图书馆的最大藏书量时不想用宏定义,在main函数中设计好让用户自己输入最大值,也就是程序中的b,但想令其为指针当参数传递到其他函数中时开始蒙圈。
#include<stdio.h>
#include<stdlib.h>
struct Date
{
int year;
int month;
int day;
};
struct Book
{
char title[128];
char author[40];
float price;
struct Date date;
char publisher[40];
};
void getinput(struct Book *book);
void pb(struct Book *book);
void initlbr(struct Book *lbr[]);
void plbr(struct Book *lbr[]);
void release(struct Book *lbr[]);
void getinput(struct Book *book)
{
printf("name:");
scanf("%s",book->title);
printf("author:");
scanf("%s",book->author);
printf("price:");
scanf("%f",&book->price);
printf("day:");
scanf("%d-%d-%d",&book->date.year,&book->date.month,&book->date.day);
printf("pr:");
scanf("%s",book->publisher);
}
void pb(struct Book *book)
{
printf("name:%s\n",book->title);
printf("author:%s\n",book->author);
printf("price:%.2f\n",book->price);
printf("day:%d-%d-%d\n",book->date.year,book->date.month,book->date.day);
printf("pr:%s\n",book->publisher);
}
void initlbr(struct Book *lbr[],int *b)
{
int i;
for(i = 0;i < *b;i++)
{
lbr[i] = NULL;
}
}
void plbr(struct Book *lbr[],int *b)
{
int i;
for(i = 0;i < *b;i++)
{
if(lbr[i] != NULL)
{
pb(lbr[i]);
putchar('\n');
}
}
}
void release(struct Book *lbr[],int *b)
{
int i;
for(i = 0;i < *b;i++)
{
if(lbr[i] != NULL);
{
free(lbr[i]);
}
}
}
int main(void)
{
printf("Please set the maxium of books in the lbr");
int *b,c;
scanf("c",&c);
*b = c;
struct Book *lbr[c];
struct Book *ptr = NULL;
int a,index = 0;
initlbr(lbr,b);
while(1)
{
printf("Do you want to register a book?\n");
do
{
a = getchar();
}while(a != 'y' && a != 'n');
if (a == 'y')
{
if (index < *b)
{
ptr = (struct Book *)malloc(sizeof(struct Book));
getinput(ptr);
lbr[index] = ptr;
index++;
putchar('\n');
}
}
else
{
printf("no more space");
break;
}
}
printf("over");
plbr(lbr);
release(lbr);
return 0;
}
第89行 : scanf("c",&c); 修改为:scanf("%d", &c);
第90行 *b = c; 修改为: b = &c;
第91行:struct Book *lbr[c]; 编译器需支持不定长数组的定义。
修改如下,改动处见注释,供参考:
#include<stdio.h>
#include<stdlib.h>
struct Date
{
int year;
int month;
int day;
};
struct Book
{
char title[128];
char author[40];
float price;
struct Date date;
char publisher[40];
};
void getinput(struct Book *book);
void pb(struct Book *book);
void initlbr(struct Book *lbr[],int *b);//修改
//void initlbr(struct Book *lbr[]); //函数声明 函数实现 函数调用,形参个数、数据类型一致
void plbr(struct Book *lbr[],int *b); //修改
//void plbr(struct Book *lbr[]); //函数声明 函数实现 函数调用,形参个数、数据类型一致
void release(struct Book *lbr[],int *b); //修改
//void release(struct Book *lbr[]); //函数声明 函数实现 函数调用,形参个数、数据类型一致
void getinput(struct Book *book)
{
printf("name:");
scanf("%s",book->title);
printf("author:");
scanf("%s",book->author);
printf("price:");
scanf("%f",&book->price);
printf("day:");
scanf("%d-%d-%d",&book->date.year,&book->date.month,&book->date.day);
printf("pr:");
scanf("%s",book->publisher);
}
void pb(struct Book *book)
{
printf("name:%s\n",book->title);
printf("author:%s\n",book->author);
printf("price:%.2f\n",book->price);
printf("day:%d-%d-%d\n",book->date.year,book->date.month,book->date.day);
printf("pr:%s\n",book->publisher);
}
void initlbr(struct Book *lbr[],int *b)
{
int i;
for(i = 0;i < *b;i++)
{
lbr[i] = NULL;
}
}
void plbr(struct Book *lbr[],int *b)
{
int i;
for(i = 0;i < *b;i++)
{
if(lbr[i] != NULL)
{
pb(lbr[i]);
putchar('\n');
}
}
}
void release(struct Book *lbr[],int *b)
{
int i;
for(i = 0;i < *b;i++)
{
if(lbr[i] != NULL);
{
free(lbr[i]);
}
}
}
int main(void)
{
printf("Please set the maxium of books in the lbr:");
int *b,c;
scanf("%d",&c); //scanf("c",&c); 修改
b = &c; //*b = c; 修改
struct Book *lbr[c]; //不定长数组定义,需编译器支持
struct Book *ptr = NULL;
int a,index = 0;
initlbr(lbr,b);
while(1)
{
printf("Do you want to register a book?\n");
do
{
a = getchar();
}while(a != 'y' && a != 'n');
if (a == 'y')
{
if (index < *b)
{
ptr = (struct Book *)malloc(sizeof(struct Book));
getinput(ptr);
lbr[index] = ptr;
index++;
putchar('\n');
}
}
else
{
printf("no more space\n");
break;
}
}
printf("over");
plbr(lbr,b); //plbr(lbr); 修改
release(lbr,b); //release(lbr); 修改
return 0;
}
使用全局变量或者你直接把这个b传到你需要的函数里不就行了
不知道你这个问题是否已经解决, 如果还没有解决的话:①方法1:
#include<stdio.h>
#include<math.h>
int main(){
char c;
int i,j,k,n;
printf("输入n:\n");
scanf("%d",&n);
c='A';
for(k=1;k<=n;k++){
for(i=1;i<=n-k;i++)printf(" ");
for(j=1;j<=k*2-1;j++)printf("%c",c);
c++;
printf("\n");
}
for(k=1;k<n;k++){
for(i=0;i<k;i++)printf(" ");
for(j=2*(n-k);j>0;j--)printf("%c",c);
c++;
printf("\n");
}
return 0;
}
②方法2:
#include<stdio.h>
#include<math.h>
int main(){
char c;
int i,j,k,n;
printf("输入n:\n");
scanf("%d",&n);
c='A';
for(k=1-n;k<=n-1;k++){
i=n-abs(k);
for(j=1;j<=n-i+1;j++)
printf(" ");
for(j=1;j<=2*i-1;j++)
printf("%c",c);
printf("\n");
c++;
}
return 0;
}
// *
// ***
// *****
//*******
// *****
// ***
// *
#include<stdio.h>
int main() {
int i, j, k;
for (i = 0; i <= 3; i++)
{
for (j = 0; j <= 2 - i; j++)
printf(" ");
for (k = 0; k <= 2 * i; k++)
printf("*");
printf("\n");
}
for (i = 0; i <= 2; i++)
{
for (j = 0; j <= i; j++)
printf(" ");
for (k = 0; k <= 4 - 2 * i; k++)
printf("*");
printf("\n");
}
return 0;
}