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

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

问题描述

  #include   <   stdio.h  >  
#include < stdlib.h >
struct test
{
int id;
char name [ 20 ];
};
int main()
{

struct 测试t1;

t1.id = 1 ;
fflush(stdin);
fgets(t1.name, 20 ,stdin);

print(& t1.name);
print1(t1.id,&(t1.name));

}
void print( struct test * name)
{
puts(姓名);
}

void print1( struct test id, struct test * name)
{

printf( \\\
%d\\\
,ID);
puts(name);
}





我的尝试:



第一个put in print函数完美地工作,但为什么它不能在print1的第二个函数中工作?

解决方案

简单:它不是<$ c传递给方法的$ c> struct test ,它是一个字符串。

你的函数需要一个结构指针:

  void  print1( struct  test id, struct  test * name)
{
printf( \ n% d\\\
,ID);
puts(name);
}

但你传递了这个:

 print1(t1.id,&(t1.name)); 



哪个是char *:

  struct  test 
{
int id;
char name [ 20 ];
};



你也传递一个整数作为第一个值!

我不确定你在尝试什么在这里做 - 我想你想重新阅读你的课程笔记,然后再试一次!


正如OriginalGriff所说,参数 * name 不是结构,它们是 * char



你应该回到C / 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 [ ^ ]



使用调试器查看代码正在做什么。

调试器允许您按照执行行行,检查变量,你会看到有一点它停止做你期望的。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

#include <stdio.h>
#include <stdlib.h>
struct test
{
    int id;
    char name[20];
};
int main()
{

struct test t1;

   t1.id=1;
   fflush(stdin);
   fgets(t1.name,20,stdin);

print(&t1.name);
print1(t1.id,&(t1.name));

}
void print(struct test *name)
{
    puts(name);
}

void print1(struct test id,struct test *name)
{

    printf("\n%d\n",id);
    puts(name);
}



What I have tried:

the first puts in print function works perfectly but why doesnt it works in second function in print1?

解决方案

Simple: it's not a struct test that you pass to the method, it's a string.
Your functionm expects a pointer to a struct:

void print1(struct test id,struct test *name)
{
    printf("\n%d\n",id);
    puts(name);
}

but you pass this:

print1(t1.id,&(t1.name));


Which is a char *:

struct test
{
    int id;
    char name[20];
};


And you pass an integer as the first value as well!
I'm not sure what you are trying to do here - I think you want to re-read your course notes and try again!


as OriginalGriff already said, the parameters *name are not structures, they are *char.

You should go back to C/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[^]

Use the debugger to see what your code is doing.
The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]


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

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