2016-07-07 3121 views
-4

此代码的目的是传递十进制虚拟地址并输出页码和偏移量。错误:间接寻址需要指针操作数('int'无效)

后,我编译在Linux上使用gcc编译我的代码,我得到这个错误:

indirection requires pointer operand ('int' invalid) virtualAddress = *atoi(argv[1]);

#include <stdio.h> 
#include <stdint.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <math.h> 
#include <curses.h> 

int main(int argc,char *argv[]) 

{ 

    unsigned long int virtualAddress,pageNumber,offset; 

    if(argc<2){ 

      printf(" NO ARGUMNET IS PASSED"); 

      return 0; 

    } 

    virtualAddress = *atoi(argv[1]); 

    //PRINT THE VIRTUAL ADDRESS 

    printf("The Address %lu contains:",virtualAddress); 

    //CALCULATE THE PAGENUMBER 

    pageNumber = virtualAddress/4096; 

    //PRINT THE PAGE NUMBER 

    printf("\n Page Number = %lu",pageNumber); 

    //FIND THE OFFSET 

    offset = virtualAddress%4096; 

    //PRINTS THE OFFSET 

    printf("\n Offset = %lu",offset); 

    getch(); 

    return 0; 

} 

回答

1
virtualAddress = *atoi(argv[1]); 

atoi函数返回int(不int *因此没有必要提领返回值)并且您尝试解除引用int,因此编译器会提供错误。

当你需要它unsinged long int使用strtoul -

char * p; 
virtualAddress = strtoul(argv[1], &p,10); 
+0

感谢。它的工作:) – azizoh

+0

@azizoh很高兴帮助。 – ameyCU

+0

@azizoh - 如果ameyCU的答案解决了您的问题,您应该强烈考虑接受答案 – 4386427