标签: gcc-warning

GCC编译器警告:格式'%c'需要'char*'类型的参数,但参数2的类型为'int*'[-Wformat]

我尝试编译代码时收到以下警告:

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)

c gcc-warning

0
推荐指数
1
解决办法
1182
查看次数

是什么导致编译器警告未使用的功能?

设置简单:功能和功能实现有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函数警告。其他人没有。为什么?

c arrays prototype function-pointers gcc-warning

0
推荐指数
1
解决办法
8971
查看次数

gcc警告:指针类型不兼容

在我的代码中,我有:

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)

我究竟做错了什么?

c gcc gcc-warning

0
推荐指数
1
解决办法
325
查看次数

为什么我会收到一条警告说"赋值从整数中生成没有强制转换的指针"?

这是我在混杂模式下从以太网接收原始数据包的代码.编译时我在代码行中收到警告

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)

c sockets gcc-warning

0
推荐指数
1
解决办法
3822
查看次数

警告:对于"long"类型,整数常量太大

我有以下代码.

  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位的无符号整数.

为什么编译器会生成此警告?

c gcc gcc-warning

0
推荐指数
1
解决办法
7629
查看次数

缺少'未初始化'警告

这是一个示例代码:

#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)

我应该用什么选项来发出警告?

c gcc gcc-warning

0
推荐指数
1
解决办法
73
查看次数

gcc 4.9.2错误-Werror = sizeof-pointer-memaccess?

#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)

c++ gcc gcc-warning

0
推荐指数
1
解决办法
2482
查看次数

如果比较两个指针的地址而不是内容,gcc/g ++警告?

假设我有一个带指针输入的函数

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,bint *编译器会警告我?

c gcc pointers compiler-warnings gcc-warning

0
推荐指数
1
解决办法
86
查看次数

GCC - 用于16位加法的转换警告

在现有代码库中解决此类警告的正确方法是什么?

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位的,我不会看到这段代码应该写的另一种方式,除了因为转换结果整数提升.

  1. 我应该抛弃所有这些案件吗?铸造感觉就像我做错了什么.
  2. 禁用警告?我宁愿不完全禁用警告,因为它在某些情况下可能有用.

c gcc type-conversion gcc-warning

0
推荐指数
1
解决办法
79
查看次数

数据类型范围有限

当我在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]

regnewflags分别是uint8_t *uint8_t类型。

这是什么意思?而我该如何解决呢?

c bit-manipulation bitwise-operators gcc-warning

0
推荐指数
1
解决办法
63
查看次数