关于C编程结构的问题 [英] Question on struct of C programming

查看:77
本文介绍了关于C编程结构的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  int  main()
{
struct 图书
{
int id;
char name [ 20 ];
char 作者[ 20 ];
char subject [ 20 ];

};

struct Books Books1;

printf( 输入id);
scanf( %d,& Books1.id);
fflush(stdin);

printf( 输入名称);
scanf( %[^ \ n] s,Books1.name);
fflush(stdin);

printf( 输入作者);
scanf( %[^ \ n] s,Books1.author);
fflush(stdin);

printf( 输入主题);
scanf( %[^ \ n] s,Books1.subject);

printf( \ n);
printf( id:%d,Books1.id);
printf( \ nname:%s,Books1.name);
printf( \ nauthor:%s,Books1.author);
printf( \ nsject:%s,Books1.subject);

printData(Books1.id);
return 0 ;
}





我的尝试:



有没有办法将这个结构传递给另一个函数而不使用引用调用(指针)。

有没有办法将struct的单个成员传递给另一个函数而不使用通过引用调用(指针)。



i知道通过引用调用或全局声明struct是可行的。

解决方案
否。问题是它是一个本地结构 - 它只在声明它的方法中知道 - 因此没有实际的方法将它声明为函数参数而不会出现编译器错误。

你可以做如果你全局声明结构 - 在这种情况下,它将像C中的所有其他参数一样通过值传递。


除了之外的任何答案都是否定的id member。

tumb的规则,你只能通过值传递基本数据类型(它们适合cpu寄存器):int char long,float double,pointer .. 。

对于其他任何东西,它是一个指针:数组,结构,列表...

  char 名称[ 20 ]; 



是一个数组



转到C基础知识

C编程语言 - 维基百科,免费百科全书 [ ^ ]

https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf [ ^ ]

http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf [ ^ ]


int main()
{
   struct Books
   {
       int id;
       char name[20];
       char author[20];
       char subject[20];

   };

struct Books Books1;

printf("enter id ");
scanf("%d",&Books1.id);
fflush(stdin);

printf("enter name ");
scanf("%[^\n]s",Books1.name);
fflush(stdin);

printf("enter author ");
scanf("%[^\n]s",Books1.author);
fflush(stdin);

printf("enter subject");
scanf("%[^\n]s",Books1.subject);

printf("\n");
printf("id :%d ",Books1.id);
printf("\nname :%s",Books1.name);
printf("\nauthor :%s",Books1.author);
printf("\nsubject :%s",Books1.subject);

printData(Books1.id);
return 0;
}



What I have tried:

is there any way to pass this struct to another function without using call by reference (pointers).
is there any way to pass a single member of struct to another function without using call by reference (pointers).

i know it is feasible to do by call by refernce or globally declaring struct.

解决方案

No. The problem is that it's a local struct - it's only known within the method it is declared in - so there is no practical way to declare it as a function parameter without getting a compiler error.
You can do it if you globally declare the struct - in that case it'll get passed by value like all other parameters in C.


The answer is no for anything but the id member.
Rule of tumb, you can pass by value only basic data types (they fit in a cpu register): int char long, float double, pointer ...
For anything else, it is a pointer: array, structure, list ...

char name[20];


is an array

Go to C basics
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]


这篇关于关于C编程结构的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆