Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: indirection requires pointer operand ('int' invalid)

Tags:

c

The purpose of this code is to pass a virtual address in decimal and output the page number and offset.

After I compile my code using the gcc compiler on Linux I get this error:

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;

}
like image 244
azizoh Avatar asked Nov 02 '25 07:11

azizoh


2 Answers

This error occurs when you want to create pointer to your variable by *my_var instead of &my_var.

like image 130
Fusion Avatar answered Nov 04 '25 01:11

Fusion


virtualAddress = *atoi(argv[1]);

atoi function returns int (not int * so no need to dereference return value) and you try to dereference int , therefore , compiler gives an error.

As you need it in unsinged long int use strtoul -

char * p;
virtualAddress = strtoul(argv[1], &p,10);
like image 42
ameyCU Avatar answered Nov 03 '25 23:11

ameyCU