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

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;
}

2 Answers

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

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);
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like