我尝试编译代码时收到以下警告:
exercise6.c:32:14:警告:格式'%c'需要'char*'类型的参数,但参数2的类型为'int*'[-Wformat]
是什么导致此警告以及如何解决?
/*Write a program that displays the contents of a file at the terminal 20 lines at
a time. At the end of each 20 lines, have the program wait for a character to be
entered from the terminal. If the character is the letter q, the program should
stop the display of the file; any other character should cause the next 20 lines
from the file to be displayed.*/
#include <stdio.h>
#include <stdlib.h>
int main (void)
{ …Run Code Online (Sandbox Code Playgroud) 设置简单:功能和功能实现有n个原型。函数指针有一大堆。每个函数都列在此数组中。使用gcc进行编译时,某些仍然会导致-Wunused函数。
码:
void foo1(void);
void foo2(void);
void bar1(void);
void bar2(void);
/* and their implementations */
void (*functions[])(void) = { foo1, foo2, bar1, bar2 };
Run Code Online (Sandbox Code Playgroud)
这就是设置的样子(只是一个例子)!现在,使用gcc编译时,其中一个foo / bar函数会导致-Wunused函数警告。其他人没有。为什么?
在我的代码中,我有:
char DRAW_EX[DRAW_HEIGHT][DRAW_WIDTH] = {
"* *",
" * * ",
" * ",
" * * ",
"* *"
};
char DRAW_CIRCLE[DRAW_HEIGHT][DRAW_WIDTH] = {
" *** ",
" * * ",
"* *",
" * * ",
" *** "
};
char DRAW_EMPTY[DRAW_HEIGHT][DRAW_WIDTH] = {
" ",
" ",
" ",
" ",
" "
};
Run Code Online (Sandbox Code Playgroud)
给我警告的那条线是:
char** leftDraw;
leftDraw = board[i][0]==EMPTY?DRAW_EMPTY:(board[i][0]==SHAPE_O?DRAW_CIRCLE:DRAW_EX);
Run Code Online (Sandbox Code Playgroud)
警告是:
warning: assignment from incompatible pointer type [enabled by default]
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
这是我在混杂模式下从以太网接收原始数据包的代码.编译时我在代码行中收到警告
logfile=open("sniff_data.bin",O_CREAT|O_APPEND|O_TRUNC,0777);
Run Code Online (Sandbox Code Playgroud)
警告:赋值使整数指针没有强制转换[默认情况下启用].我在谷歌检查但无法找到解决方案.任何人都可以告诉我解决方案,我哪里出错了?
void ProcessPacket(unsigned char* , int);
void print_ip_header(unsigned char* , int);
void print_tcp_packet(unsigned char * , int );
void print_udp_packet(unsigned char * , int );
void print_icmp_packet(unsigned char* , int );
void PrintData (unsigned char* , int);
FILE *logfile;
struct sockaddr_in source,dest;
int i,j;
int main()
{
int saddr_size,data_size;
struct sockaddr saddr;
gopromiscous();
unsigned char *buffer = (unsigned char *) malloc(1024);
logfile=open("sniff_data.bin",O_CREAT|O_APPEND|O_TRUNC,0777);
if(logfile==NULL)
{
printf("Unable to create sniff_data file.");
}
printf("\n Starting..\n");
int sock_raw = socket( AF_PACKET , …Run Code Online (Sandbox Code Playgroud) 我有以下代码.
uint32_t reg_val = 0;
uint64_t val = 0;
reg_val = 0;
reg_val = ((val & 0xffffff000000) >> 24);
dev_write(rw,reg_val);
Run Code Online (Sandbox Code Playgroud)
编译器发出警告说
warning: integer constant is too large for "long" type
Run Code Online (Sandbox Code Playgroud)
我只将24位分配给reg_val,它被定义为大小为32位的无符号整数.
为什么编译器会生成此警告?
这是一个示例代码:
#include <stdio.h>
int main() {
int n = 5;
float v[n];
float sum;
int i;
for(i = 0; i < n; i++) {
v[i] = i + 1;
printf("v[%d]=%f\n", i, v[i]);
}
for(i = 0; i < n; i++) {
sum += v[i]; //uninitialized using
}
printf("sum=%f\n", sum);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
gcc编译它而没有任何未初始化变量的警告.
我正在使用gcc 4.6.3以下选项:
gcc -Wall main.c -o main
Run Code Online (Sandbox Code Playgroud)
我应该用什么选项来发出警告?
#include <string.h>
void test(char charArray [100])
{
strncpy(charArray, "some text", sizeof(charArray));
}
int main()
{
char charArray [100];
test(charArray);
// EDIT: According to comment from P0W I added this line - where is the difference?
strncpy(charArray, "some text", sizeof(charArray)); // compiles OK
}
Run Code Online (Sandbox Code Playgroud)
使用此命令行在SLES 11 SP2上使用gcc 4.9.2编译,g++ gcc-warning-bug-2.cpp -Wall -Wextra -c -Werror我收到此警告.由于-Werror标志我无法编译项目:
gcc-warning-bug-2.cpp: In function ‘void test(char*)’:
gcc-warning-bug-2.cpp:5:40: error: argument to ‘sizeof’ in ‘char* strncpy(char*, const char*, size_t)’ call is the same expression as the destination; …Run Code Online (Sandbox Code Playgroud) 假设我有一个带指针输入的函数
void f(int *a, int *b) {
if (*a < *b) {
printf("hello!\n");
}
}
Run Code Online (Sandbox Code Playgroud)
哪里*a < *b是正确的行为.
在gcc中是否有警告我可以打开,所以每当我编写代码时
a < b
Run Code Online (Sandbox Code Playgroud)
时a,b被int *编译器会警告我?
在现有代码库中解决此类警告的正确方法是什么?
void test(uint16_t x, uint16_t y)
{
// warning: conversion to 'uint16_t' from 'int' may alter its value
uint16_t value = (x + 1) * y;
...
}
Run Code Online (Sandbox Code Playgroud)
所有这三个值都是无符号的16位整数,并且任何算术溢出都可以正确执行,而不会警告它这个平台int是16位的,我不会看到这段代码应该写的另一种方式,除了因为转换结果整数提升.
当我在GCC中编译此代码段时:
uint8_t *reg = ..., newflags = ...;
...
if(*reg == (~(uint8_t)0))
{
newflags |= (1<<2);
newflags |= (1<<7);
}
Run Code Online (Sandbox Code Playgroud)
我收到此警告:
warning: comparison is always false due to limited range of data type [-Wtype-limits]
reg和newflags分别是uint8_t *和uint8_t类型。
这是什么意思?而我该如何解决呢?