| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3277 人关注过本帖
标题:请问:指针程序出现这情况是什么原因,如何较好避免
只看楼主 加入收藏
natto
Rank: 1
等 级:新手上路
帖 子:27
专家分:5
注 册:2016-4-10
结帖率:70%
收藏
 问题点数:0 回复次数:2 
请问:指针程序出现这情况是什么原因,如何较好避免
请教一下:以下程序编译后,虽无错误,但出现如下的警告,英文意思百度了下,大约能看得懂是什么意思,但是不能理解,因为按书上所说,这样使用指针是可以,但是为什么会出现这种警告呢,又该怎么编写是相对比较正确的程序呢,请不吝指教,谢谢!
程序代码:
#include<stdio.h>
int add(int,int);
main()
{
    int x,y,z,*p,*q;
    printf("请输入一个数");
    scanf("%d %d",&x,&y);
    p=&x;q=&y;
    z=add(p,q);
    printf("%d\n",z);
}
int add(int *a,int *b)
{
    int sum;
    sum=*a+*b;
    return sum;
}

--------------------配置: vc6.0 - CUI Release, 编译器类型: Microsoft C++ Compiler--------------------


[Warning]  C4047: 'function' : 'int ' differs in levels of indirection from 'int *'
[Warning]  C4024: 'add' : different types for formal and actual parameter 1
[Warning]  C4047: 'function' : 'int ' differs in levels of indirection from 'int *'
[Warning]  C4024: 'add' : different types for formal and actual parameter 2
[Warning] C4028: formal parameter 1 different from declaration
[Warning]  C4028: formal parameter 2 different from declaration

完成编译 I:\electronic_practice\c_program\Console_Application_vc6\Console_Application_vc6.c: 0 个错误, 6 个警告
生成 I:\electronic_practice\c_program\Console_Application_vc6\vc6.0\Console_Application_vc6.obj
搜索更多相关主题的帖子: 如何 
2016-04-10 19:01
azzbcc
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江西财经大学
等 级:贵宾
威 望:81
帖 子:3293
专家分:12919
注 册:2012-11-4
收藏
得分:0 
换个编译器吧

[09:24:08 azzbcc@XJH-PC src]$ gcc -O2 -W -Wall C.c -o C
C.c:3:1: warning: return type defaults to ‘int’ [-Wreturn-type]
 main()
 ^
C.c: In function ‘main’:
C.c:9:5: warning: passing argument 1 of ‘add’ makes integer from pointer without a cast [enabled by default]
     z=add(p,q);
     ^
C.c:2:5: note: expected ‘int’ but argument is of type ‘int *’
 int add(int,int);
     ^
C.c:9:5: warning: passing argument 2 of ‘add’ makes integer from pointer without a cast [enabled by default]
     z=add(p,q);
     ^
C.c:2:5: note: expected ‘int’ but argument is of type ‘int *’
 int add(int,int);
     ^
C.c: At top level:
C.c:12:5: error: conflicting types for ‘add’
 int add(int *a,int *b)
     ^
C.c:2:5: note: previous declaration of ‘add’ was here
 int add(int,int);
     ^
C.c: In function ‘main’:
C.c:7:10: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d",&x,&y);
          ^
C.c:11:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^



[fly]存在即是合理[/fly]
2016-04-11 09:25
huzi741
Rank: 2
等 级:论坛游民
威 望:1
帖 子:7
专家分:14
注 册:2013-1-27
收藏
得分:0 
1、函数申明尽量使用标准的;
2、函数没有返回值,尽量加上void;
#include<stdio.h>
int add(int *a,int *b);
void main()
{
    int x,y,z,*p,*q;
    printf("请输入一个数");
    scanf("%d %d",&x,&y);
    p=&x;q=&y;
    z=add(p,q);
    printf("%d\n",z);
}
int add(int *a,int *b)
{
    int sum;
    sum=*a+*b;
    return sum;
}
2016-04-12 12:37
快速回复:请问:指针程序出现这情况是什么原因,如何较好避免
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.081591 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved