先前的隐式声明:(将int转换为unsigned long)

时间:2022-08-26 19:51:37

In this specific area of my project i am taking in info front he command line the doing different things. I'm getting this error even though the program executes correctly as is.

在我的项目的这个特定领域,我正在使用信息前端命令行执行不同的操作。即使程序按原样正确执行,我也会收到此错误。

lab2.c:86: error: conflicting types for ‘rrotate’
lab2.c:67: error: previous implicit declaration of ‘rrotate’ was here

here is the function and where it is called.

这是函数及其调用的位置。

CALL:

呼叫:

  else if(strcmp(action, compare_r) == 0)
        {
        /* unsigned long rrotate(unsigned long x, int n) 
        this function will implement a right rotate function
        that returns the value of the unsigned long x rotated to the 
        right by n bit positions*/
            strcpy(e,argv[3]);
           int n = strtol(e,0,10);
           hex_num = (unsigned long)hex_one;
           unsigned long number = rrotate(hex_num,n);
           print_bits(number);
        }

FUNCTION:

功能:

unsigned long rrotate(unsigned long x, int n){
            unsigned long number = x >> n;


        return number;

}

i run the program by compiling with gcc lab2.c -o lab2 then run like lab2 -r 0x5 3

我通过使用gcc lab2.c -o lab2编译运行程序,然后像lab2 -r 0x5 3一样运行

PROGRAM:

程序:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char*argv[])
{
char action[5];
char compare_p[] = "-p";
char compare_i[] = "-i";
char compare_u[] = "-u";
char compare_c[] = "-c";
char compare_r[] = "-r";
char compare_s[] = "-s";
char compare_m[] = "-m";

char hex[10],e[5];
char *ptr;
unsigned long i,hex_num;
int hex_one,hex_two;
/*argc is the argument count-http://www.tutorialspoint.com/c_standard_library/c_function_atol.htm*//*It is the number of arguments passed into the program from the command line, including the name of the program.*/





    if (argc < 3)
    {
    printf("NADA");
    } 
    else
    {

    strcpy(action, argv[1]);
    strcpy(hex,argv[2]);    
    hex_one = strtol(hex, &ptr, 16);


    if(strcmp(action, compare_p) == 0)
    {
    print_bits(hex_one);

    }

    else if(strcmp(action, compare_u) == 0)
    {
        printf("do this instead");
    }
    else if(strcmp(action, compare_i) == 0)
    {
    /*create an intersection function*/
    }
    else if(strcmp(action, compare_c) == 0)
    {/*complement funtion
     0x7 -->> 1111 1111 1111 1111 1111 1111 1111 1000*/
    hex_one = ~hex_one;
    print_bits(hex_one);
    }
    else if(strcmp(action, compare_r) == 0)
    {
    /* unsigned long rrotate(unsigned long x, int n) 
    this function will implement a right rotate function
    that returns the value of the unsigned long x rotated to the 
    right by n bit positions*/
            strcpy(e,argv[3]);
       int n = strtol(e,0,10); 
           hex_num = (unsigned long)hex_one;
       unsigned long number = rrotate(hex_num,n);
       print_bits(number);
    }
    else if(strcmp(action, compare_s) == 0)
    {

    }
    else if(strcmp(action, compare_m) == 0)
    {

    }


     }/*END ELSE*/ 

return 0;
}


unsigned long rrotate(unsigned long x, int n){
        unsigned long number = x >> n;


    return number;

}
print_bits(unsigned int i)
    {
        int x; 
    int count = 0;
        for(x=(sizeof(int)*8)-1; x>=0; x--)
    {
        (i&(1<<x))?putchar('1'):putchar('0');
    count+=1;

        if(count == 4)
        {
        printf(" ");
        count = 0;
        }
    }
        printf("\n");
    }

2 个解决方案

#1


1  

Before the C99 standard, it was allowed to implicitly declare a function by calling without any previous declaration (function prototype). The compiler will then guess the functions arguments, by examining the types used in the call.

在C99标准之前,允许通过在没有任何先前声明(函数原型)的情况下调用来隐式声明函数。然后,编译器将通过检查调用中使用的类型来猜测函数参数。

However since the C99 standard this isn't really allowed, you need to declare a function before calling it. This can be very simply done by adding a function prototype before the call, so you should have something like

但是,由于C99标准并不是真正允许的,因此您需要在调用之前声明一个函数。这可以通过在调用之前添加函数原型来完成,所以你应该有类似的东西

// Function *declaration*
unsigned long rrotate(unsigned long x, int n);

int main(...)
{
    ...
}

// Function *definition*
unsigned long rrotate(unsigned long x, int n)
{
    ...
}

#2


1  

Move the definition of rrotate before the main function or add a declaration before the main function. Your compiler actually declares rrotate when you call it.

在main函数之前移动rrotate的定义或在main函数之前添加声明。当你调用它时,你的编译器实际上声明了rrotate。

#1


1  

Before the C99 standard, it was allowed to implicitly declare a function by calling without any previous declaration (function prototype). The compiler will then guess the functions arguments, by examining the types used in the call.

在C99标准之前,允许通过在没有任何先前声明(函数原型)的情况下调用来隐式声明函数。然后,编译器将通过检查调用中使用的类型来猜测函数参数。

However since the C99 standard this isn't really allowed, you need to declare a function before calling it. This can be very simply done by adding a function prototype before the call, so you should have something like

但是,由于C99标准并不是真正允许的,因此您需要在调用之前声明一个函数。这可以通过在调用之前添加函数原型来完成,所以你应该有类似的东西

// Function *declaration*
unsigned long rrotate(unsigned long x, int n);

int main(...)
{
    ...
}

// Function *definition*
unsigned long rrotate(unsigned long x, int n)
{
    ...
}

#2


1  

Move the definition of rrotate before the main function or add a declaration before the main function. Your compiler actually declares rrotate when you call it.

在main函数之前移动rrotate的定义或在main函数之前添加声明。当你调用它时,你的编译器实际上声明了rrotate。