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;
}
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);
Related
I am new to coding in the C language. I am trying to make a program that detects when the RobloxPlayerBeta.exe is being run, but upon compiling it says that "passing argument 1 of 'strcmp' makes pointer from integer without a cast".
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int logout();
char retrieve();
int main(){
char value;
while(1){
system("cmd.exe /C tasklist > Tasks.txt");
value = retrieve();
printf("%d\n",value);
int thing;
thing = strcmp(value,"1");
printf("%d",thing);
if (thing == 0){
int x = logout();
}
sleep(10);
}
return 0;
}
int logout(){
system("c:\\windows\\system32\\shutdown /l");
return 0;
}
char retrieve(){
system("cmd.exe /C start C:\\Users\\chall\\Documents\\Ccode\\Logout\\dist\\FindTask\\FindTask.exe");
FILE *f;
f = fopen("Tasks.txt","r");
int number = fgetc(f);
return number;
}
FindTask.exe is an exe made with the following python code:
with open(r"C:\Users\chall\Documents\Ccode\Logout\Tasks.txt","r") as db:
dataset = db.readlines()
for data in dataset:
if(data[:20].strip().lower() == "robloxplayerbeta.exe"):
with open("Tasks.txt","w") as f:
f.write("1")
I would like to know what a cast is and why I need one.
Cast is to tell the system convert data of one type to another type.
Example:
#include <stdio.h>
int main(void) {
int a = 10;
double b = (double)a; /* cast is used here */
printf("%f\n", b);
return 0;
}
In this case you don't need cast. strcmp() is for compareing strings. You should use operators to deal with numbers to compare single character.
Wrong:
thing = strcmp(value,"1");
Correct:
thing = value - '1';
Although I think I have more than one issue in this code, I want to solve my compiler error first. Thank you in advance for any suggestions. I will try to address run errors next.
The Error:
fuzzer.c: In function ‘main’:
fuzzer.c:26:25: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
charArray[arraySize-1] = NULL; /*make sure charArray[] is a string array that has a size of arraySize */
^
The Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <time.h>
int main(void)
{
int fuzzNum = 100;
char buffer[1000], *charArray;
int status, ret, i, j, retCode, arraySize; /* */
time_t t;
FILE *fin;
FILE *fout;
srand((unsigned) time(&t)); /* randomize the initial seed */
for(i=0; i<fuzzNum; i++)
{
charArray = (char *) malloc(arraySize);
for (j=0; j< arraySize; j++)
charArray[j] = 'A';
charArray[arraySize-1] = NULL;
/*make sure charArray[] is a string array that has a size of arraySize */
/* open and read the cross.jpg file as a binary format file*/
fin=fopen("./cross.jpg","rb" );
/*generate a variable file name*/
char fileName[30]; int n;
sprintf(fileName, "crashed-%d.jpg", n);
fout=fopen("./test.jpg","wb");
/* execute the jpg2bmp file to process the test.jpg file*/
char comBuf[200];
sprintf(comBuf, "./jpg2bmp test.jpg temp.bmp");
ret=system(comBuf);
free(charArray); /* must free memory for repeat testing! */
ret=system(buffer);
wait(&status);
retCode=WEXITSTATUS(ret);
if ( retCode == 128+11 || retCode ==128+6) /* segmentation fault (11) or Abort (6) */
{
printf("retCode=%d, arraySize = %d", retCode, arraySize);
fflush(stdout); /*make sure output is print out immediately ! */
}
}
return 0;
}
First of all, this is a warning, and not an error. Your code should compile successfully in the least, if this warning is the only issue the compiler produces during the build process. However, if you want to overcome this issue, you can simply change:
charArray[arraySize-1] = NULL;
To:
charArray[arraySize-1] = 0;
While charArray is a pointer that points to the first memory space in the array, charArray[i], where i represents a counter as an example here, is of data type int. Now, NULL is used when a pointer points to nothing, or in computer terms, to the ground. Therefore, the compiler warns you that you are storing a pointer attribute in a non-pointer memory space (Sorry for the really por choice of words, if there is anything wrong with it, please correct me in the comments box).
The above code change should resolve the warning your compiler produces.
Having an issue with reading in from the command line into an array of integers.
Intent is to parse the entered line by spaces/tabs and then atoi() each individual number into the appropriate array slot.
Relevant code:
main.c
#include <stdio.h>
#include <stdlib.h>
#include "functions.h"
int main()
{
int nums[100];
int count = 0;
readInput(&nums, &count);
return 0;
}
functions.h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED
void readInput(int *nums[], int *count);
#endif // FUNCTIONS_H_INCLUDED
functions.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "functions.h"
#define delims " \t\r\n"
#define MAX_LEN 128
void readInput(int *nums[], int *count)
{
char *input = malloc(MAX_LEN);
char *buffer;
gets(input);
buffer = strtok(input, delims);
nums[(*count)++] = atoi(buffer);
while ((buffer = strtok(NULL, delims)) != NULL)
nums[(*count)++] = atoi(buffer);
buffer = strtok(NULL, delims);
}
The lines in functions.c with nums[(*count)++] = atoi(buffer); are flagging with the warning "warning: assignment makes pointer from integer without a cast".
And from main.c, the line readInput(&nums, &count); is flagging with "warning: passing argument 1 of 'readInput' from incompatible pointer type".
The odd thing is this program works when run and any attempts to add in casting or dereferencing has resulted in the warnings being subdued, but the program crashing when run.
When you declare your function:
void readInput(int *nums[], int *count);
the argument declaration int *nums[] is an array of pointers, not a pointer to an array which is what you call it like with &nums.
However, you don't need to pass a pointer to the array here. Arrays naturally decays to pointers to their first element. And when you declare an array as an argument to a function (like the declaration of nums in the function prototype above) it's really a pointer.
When you declare function arguments, a declaration like int nums[] is the same as int *nums.
So the declaration of readInput should really be
void readInput(int *nums, int *count);
If you do that change (including the function definition of course), and call it like
readInput(nums, &count);
then everything should work out fine.
I have some troubles when using strcpy to copy an array of string inside a double pointer with allocated memory, but i can't understand why i get segmentation fault even if i have previously allocated memory.
Here is the code:
#include <stdio.h>
#include <string.h>
typedef struct Students {
int q_exams;
char **done_exams;
}Students;
int main() {
Students a;
int i;
char support[30];
printf("how many exams have you done ?\n");
scanf("%d",&(a.q_exams));
a.done_exams=malloc(sizeof(char*)*a.q_exams);
if(a.done_exams==NULL)
{
printf("out of memory\n");
return 0;
}
for(i=0;i<a.q_exams;i++)
{
printf("Insert the name of the exam\n");
scanf("%28s",support);
a.done_exams[i]=malloc(strlen(support)+1);
if(a.done_exams[i]==NULL)
{
printf("out of memory\n");
return 0;
}
strcpy(a.done_exams[i][0],support);
fflush(stdin);
}
return 0;
}
You need to pass an address of the initial character to strcpy, either like this
strcpy(&a.done_exams[i][0],support);
// ^
// Add an ampersand
or equivalently like this:
strcpy(a.done_exams[i] , support);
// ^
// Remove the second index
Currently, your code passes the value* of the initial character, rather than its address.
* The value is undefined at the time as well, but it is not the primary cause, because you should not be passing value at all.
This code is fixed
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct Students {
int q_exams;
char **done_exams;
} Students;
int main()
{
Students a;
int i;
char support[49];
printf("how many exams have you done ?\n");
scanf("%d",&(a.q_exams));
a.done_exams = malloc(sizeof(char*) * a.q_exams);
if(a.done_exams==NULL)
{
printf("out of memory\n");
return 0;
}
for(i = 0 ; i < a.q_exams ; i++)
{
printf("Insert the name of the exam\n");
scanf("%48s",support);
a.done_exams[i] = malloc(strlen(support)+1);
if(a.done_exams[i] == NULL)
{
printf("out of memory\n");
return 0;
}
strcpy(a.done_exams[i]/*[0]*/, support);
/* ^ ^- this is wrong
* + pass the address to the array not the first element value
*
* if you had warnings turned on you would have seen this
*/
fflush(stdin);
}
return 0;
}
notice that
scanf("%48s", support);
requires
char support[49];
which is also fixed in the code.
See the man page of strcpy().
The first argument should be of type char *.
As per your code, the argument [a.done_exams[i][0]] is of type char. You need to pass a char * [starting address of the destination] actually.
Change your code to
strcpy(a.done_exams[i],support);
The
strcpy(a.done_exams[i][0],support);
should be
strcpy(a.done_exams[i],support);
or
strcpy(&a.done_exams[i][0],support);
My advice would be to always compile with compiler warnings turned on. My compiler (gcc) does a very good job of catching the problem and telling you exactly what needs to be done to fix it:
test.c:37:12: warning: incompatible integer to pointer conversion passing 'char' to
parameter of type 'char *'; take the address with & [-Wint-conversion]
strcpy(a.done_exams[i][0],support);
^~~~~~~~~~~~~~~~~~
&
P.S. You are also missing some #includes:
#include <stdlib.h>
#include <string.h>
This procedure should convert a string that contains a set of double numbers separated by comma (e.g. 7.2,9.5,-5.515) to a vector of double type.
void ToDoubleVec(int d,const char* commaSeparated,double *result)
{
int i;
result[0]=atof(strtok(commaSeparated,","));
for(i=1;i<d;i++)
result[i]=atof(strtok(NULL,","));
}
Here is the snippet of program that calls it:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc,char** argv)
{
...
int i,dim=atoi(argv[1]);
double *lower;
lower = malloc(dim*sizeof(double));
ToDoubleVec(dim,argv[2],lower);
...
}
Debugger's output:
40 lower = malloc(dim*sizeof(double));
(gdb) s
42 ToDoubleVec(dim,argv[2],lower);
(gdb) s
ToDoubleVec (d=2, commaSeparated=0x7fffffffe9d3 "2.3,-62.1", result=0x603010) at testPSO.c:11
11 result[0]=atof(strtok(commaSeparated,","));
(gdb) s
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff77f56bb in ?? () from /lib/x86_64-linux-gnu/libc.so.6
Why doesn't it work? I was sure that I've allocated enough memory for the array and also parameters seems to be passed correctly.
You can reduce your code to this SSCCE (Short, Self-Contained, Correct Example), which crashes nicely when you leave out #include <string.h> and does not compile cleanly when you add #include <string.h>:
segv.c: In function ‘ToDoubleVec’:
segv.c:8:5: warning: implicit declaration of function ‘strtok’ [-Wimplicit-function-declaration]
segv.c:8:20: warning: initialization makes pointer from integer without a cast [enabled by default]
segv.c:14:20: warning: assignment makes pointer from integer without a cast [enabled by default]
Code:
#include <stdlib.h>
//#include <string.h>
static void ToDoubleVec(int d, const char* commaSeparated, double *result)
{
int i;
result[0] = atof(strtok(commaSeparated, ","));
for (i = 1; i < d; i++)
result[i] = atof(strtok(NULL, ","));
}
int main(void)
{
int dim = 2;
double *lower = malloc(dim*sizeof(double));
char arg[] = "7.2,9.5,-5.515";
ToDoubleVec(dim, arg, lower);
}
Passing the return value from a function such as strtok() which can return a null pointer directly to a function such as atof() which does not tolerate null pointers is foolhardy; it leads to crashes. If everything is correct, you'll be OK; if not, you'll crash and burn.
The unchecked memory allocation is a similar problem; you didn't even check that dim was non-zero (and non-negative) before doing the memory allocation in the original.
#include <assert.h>
#include <string.h>
#include <stdlib.h>
static void ToDoubleVec(int d, char *commaSeparated, double *result)
{
int i;
char *number = strtok(commaSeparated, ",");
if (number != 0)
{
result[0] = atof(number);
for (i = 1; i < d; i++)
{
number = strtok(NULL, ",");
if (number != 0)
result[i] = atof(number);
}
}
}
int main(void)
{
int dim = 2;
double *lower = malloc(dim*sizeof(double));
char arg[] = "7.2,9.5,-5.515";
assert(lower != 0);
ToDoubleVec(dim, arg, lower);
}
You could — and in one version of the code I did — add error printing to report if the tests on number failed. But the crash is caused by the implicit declaration of strtok() as returning int and not char *.
I have tried to compile your code, and the compiler warned me that strtok() takes as input a char* and not a const char*. Then I have tried this code, and it is working correctly:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void ToDoubleVec(int d, char* commaSeparated,double *result);
int main(int argc,char** argv)
{
int i,dim=atoi(argv[1]);
double *lower;
lower = malloc(dim*sizeof(double));
ToDoubleVec(dim,argv[2],lower);
for (i=0; i<dim; ++i) {
printf("%f\n", lower[i]);
}
return 0;
}
void ToDoubleVec(int d, char* commaSeparated,double *result)
{
int i;
result[0]=atof(strtok(commaSeparated,","));
for(i=1;i<d;i++)
result[i]=atof(strtok(NULL,","));
}
So try to change const char* to char*, and check the input you pass to your program, maybe it is not correct and this could be the problem.