I have this homework to do in C. I'm beginner so it is probably very easy, but anyway I have a problem with it.
int main(int argc, char* argv){
int fd=open(argv[1], O_RDONLY);
int fileLength=(int)lseek(fd,0,SEEK_END);
lseek(fd,0,SEEK_SET);
char buf[fileLength];
read(fd,buf,fileLength);
int i=0;
for(i=0; i<fileLength; i++){
printf("%c",buf[i]);
}
printf("\n");
}
I get this error:
warning: passing argument 1 of ‘open’ makes pointer from integer without a cast
If I write "file" instead of argv[1], everything is ok.
int main(int argc, char* argv){
has to be:
int main(int argc, char *argv[])
See the error?
char* argv should be char* argv[]
What you did is declaring argv as char* and then argv[1] becomes a char (which is an integer) instead of char *
The correct signature is
int main(int argc, char **argv)
Your problem is in main() declaration, which should be:
int main(int argc, char *argv[])
You defined argv as a char *, which makes it a single pointer, whereas it's an array of char * pointer, with each char * element corresponding a command line argument to your program.
The error you're getting is caused by the fact that when you pass argv[1] to open(), argv[1] is a single char, while open() expects a char *.
Another improvement to your program would be checking that argc > 1 before attempting to use argv[1]. This would catch cases when you didn't pass any arguments to your program.
argv should be a char ** not a char * also you should run the program like
./program filename
Your main function's signature is wrong.
It should look like this:
int main(int argc, const char *argv[]) // notice how 'argv' is now a 'const char *[]',
Try this mate:
main () {
FILE *fin = fopen ("test.in", "r");
FILE *fout = fopen ("test.out", "w");
int a, b;
fscanf (fin, "%d %d", &a, &b); /* two input integers */
fprintf (fout, "%d\n", a+b);
exit (0);
}
instead of "test.in" put your argument, did you try to cast first before?
Related
Summary
I am trying to pass the text that user has put in the terminal and pass it to the function named printString(). I don't fully understand C but I think I has to do with the pointer not being in the heap. Any help would be appreacitaed!
#include <stdio.h>
void printString();
int main (int argc, char* argv[]) {
int commandAmount = 1;
while (commandAmount < argc) {
printString(commandAmount, &argv);
}
return 0;
}
void printString(int commandAmount, char* argv[]) {
printf("the word is %s," , argv[commandAmount]);
}
./shortExample example
Segmentation fault (core dumped)
The prototype void printString(); does not match the actual implementation.
It should have been:
void printString(int commandAmount, char* argv[]);
You could also skip the prototype and just implement the function before main. Your loop while (commandAmount < argc) seems to not have any way to finish since you never increase commandAmount. This can cause undefined behavior and with such, your program may crash or do just about anything.
I suggest making a for-loop to fix that.
Example:
#include <stdio.h>
void printString(int commandAmount, char* argv[]) {
printf("the word is %s,", argv[commandAmount]);
}
int main(int argc, char* argv[]) {
for(int commandAmount = 1; commandAmount < argc; ++commandAmount) {
printString(commandAmount, argv);
}
}
or in the way you structured it:
#include <stdio.h>
void printString(int commandAmount, char* argv[]); // corrected
int main(int argc, char* argv[]) {
int commandAmount = 1;
while (commandAmount < argc) {
printString(commandAmount, argv);
++commandAmount; // needed
}
}
void printString(int commandAmount, char* argv[]) {
printf("the word is %s,", argv[commandAmount]);
}
For starters this function declaration
void printString();
does not provide a function prototype. So the compiler determines the type of the parameters of the function from the function call
printString(commandAmount, &argv);
However the expression &argv used in this call
printString(commandAmount, &argv);
has the type char *** due to the declaration of the identifier argv
int main (int argc, char* argv[]) {
^^^^^^^^^^^^
But the corresponding parameter in the definition of the function printString has the type char ** due to adjusting by the compiler parameters having array types to pointers to array element type.
That is this function declaration
void printString(int commandAmount, char* argv[]) {
is adjusted by the compiler to
void printString(int commandAmount, char** argv) {
^^^^^^^^^^^
Thus there are incompatible types of the argument expression and of the parameter. As a result this call
printf("the word is %s," , argv[commandAmount]);
invokes undefined behavior.
Moreover this loop in main
int commandAmount = 1;
while (commandAmount < argc) {
printString(commandAmount, &argv);
}
in general is an infinite loop because the variable commandAmount is not changed within the loop.
Firstly you should provide the function prototype before main to make your program more safer
void printString(int commandAmount, char** argv);
and call the function like
printString(commandAmount, argv);
^^^^
Of course you need also to change the loop in main.
Pay attention to that as the value of the parameter commandAmount is not outputted within the function then in fact it is redundant. You could pass to the function the pointer to the string itself. For example
#include <stdio.h>
void printString( const char *s );
int main( int argc, char* argv[] )
{
for ( int commandAmount = 1; commandAmount < argc; commandAmount++ )
{
printString( argv[commandAmount] );
}
putchar( '\n' );
return 0;
}
void printString( const char *s )
{
printf( "the word is %s, " , s );
}
I've never understood this error and I keep running into similar one's and it's really frustrating as I can't find a solution to it. (If there is another one please don't bash on me as I couldn't find it).
Looking at a simple piece of code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[]){
char s[10];
int x = strtol(argv[1], &s, 10);
printf("%d", x);
printf("%s", s);
return 0;
}
I keep receiving these errors, but I don't really understand why:
warning: passing argument 2 of ‘strtol’ from incompatible pointer type [-Wincompatible-pointer-types]
int x = strtol(argv[1], &s, 10);
and
note: expected ‘char ** restrict’ but argument is of type ‘char (*)[10]’
When I change char s[10] to char *s I receive a segfault on the line that uses strtol. I don't understand what's going wrong, could someone explain? Thanks in advance.
Changing char s[10] to char *s is the correct way to address the compile error. strtol's second argument is supposed to be a pointer to a pointer variable, which it will initialize to point somewhere within the string that is its first argument. char s[10] does not declare a pointer variable, char *s does.
The only explanation I can think of, why this program might crash, is that you didn't pass it any arguments. In that case argc will be less than 2 and argv[1] will be a null pointer (or possibly not even initialized). You need to do something like this:
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
if (argc != 2) {
fprintf(stderr, "usage: %s integer\n", argv[0]);
return 1;
}
char *s;
long x = strtol(argv[1], &s, 10);
printf("x: %ld\n", x);
printf("s: '%s'\n", s);
return 0;
}
Incidentally, in C, for historical reasons, preferred style is to put the opening curly brace of a function definition on its own line, even if all other opening curly braces are "cuddled".
&s is referring to the address of the array. It is effectively a pointer to a pointer. What you want is to pass in the array name s which is a pointer pointing to the first character of the array s.
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[]){
char s[10];
int x = strtol(argv[1], s, 10);
printf("%d", x);
printf("%s", s);
return 0;
}
You need to create a variable of char * and then pass this to function.
It is not possible to pass array that way as it will not decay to char **.
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[]){
char s[10];
char* s_ptr; //New variable, char *
int x = strtol(argv[1], &s_ptr, 10);
printf("%d", x);
printf("%s", s_ptr);
return 0;
}
See documentation of strtol function:
http://www.cplusplus.com/reference/cstdlib/strtol/
why I am not succeed
int main(char* name,int arg0,int arg1)
{
name = "/u/e2014/Desktop/os/Prog.c";
arg0 = 0;
arg1 = 1;
char my_args[3];
my_args[0] = arg0;
my_args[1] = arg1;
my_args[2] = NULL;
execl(name,m_args);
return(0);
}
I want that my program will execute the program in the path "name".
Right now its do nothing.
I am not understand where is my mistake?
I program in C on linux, and compile it with gcc
Thanks a lot!!
gcc has 3 different signature for main function
int main(void);
int main(int argc, char* argv[]);
int main(int argc, char *argv[], char *envp[]);
Your main function doesn't match either of these. therefore compiler error.
For your case you can use the 2nd signature with a small modification.
#include <stdlib.h>
int main(int argc, char **argv)
{
char *path;
int int1, int2;
path = argv[1];
int1 = atoi(argv[2]);
int2 = atoi(argv[3]);
}
First you are passing wrong parameter in int main(). main() has at least 3 args only.
int main(int argc, char*argv[], char *envp[]);
To execute your program you should use execvp() because you passing arrey of char* not command-line arguments via a variable-argument.
difference between execl and execv?
**L vs V: whether you want to pass the parameters to the exec'ed program as
L: individual parameters in the call (variable argument list): execl(), execle(), execlp(), and execlpe()
V: as an array of char* execv(), execve(), execvp(), and execvpe()**
#include <stdio.h>
#include <unistd.h>
int main(int argc,char*argv[])
{
char *name = "/root/a.out";
char *arg0 = "0";
char *arg1 = "1";
char *my_args[4];
my_args[0] = name;
my_args[1] = arg0;
my_args[2] = arg1;
my_args[3] = NULL;
execvp(my_args[0],my_args);
return(0);
}
So, I have been working on this simple block of code. I would like it to print, when I type in "./a.out -n"
However, that is not working. I have been on stackoverflow trying to work on this, but no such luck. Any help would be appreciated.
#include <stdio.h>
#include <stdlib.h>
void parse_cmdline(int argc, char *argv);
int main (int argc, char *argv[]) {
parse_cmdline(argc, argv);
}
void parse_cmdline(int argc, char *argv)
{
int x,i,m,n = 0;
if (*(++argv) == 'n'){
x = 1;
printf("Output array: "); /* not being displayed*/
}
}
Just write
if (**++argv == 'n'){
And the function should be declared like
void parse_cmdline(int argc, char **argv);
Otherwise you should specify what parameter you are going tp pass to the function. For example
parse_cmdline(argc, argv[1]);
You can check what parameters are passed to the program the following way
int main (int argc, char *argv[]) {
for ( int i = 0; i < argc; i++ ) puts( argv[i] );
}
Try:
#include <stdio.h>
#include <stdlib.h>
void parse_cmdline(int argc, char *argv[]);
int main (int argc, char *argv[]) {
parse_cmdline(argc, argv);
}
void parse_cmdline(int argc, char *argv[])
{
int x,i,m,n = 0;
if (*(argv[1]) == '-' && *(++argv[1]) == 'n'){
x = 1;
printf("Output array: "); /* not being displayed*/
}
}
And run it with ./a.out -n.
So, I have examined 0th and 1th character of the argv value on the position one ("./a.out" is located on position 0, and "-n" on position 1).
Is that what you wanted?
Also, you cannot ignore warnings:
1.c: In function ‘main’:
1.c:5:23: warning: passing argument 2 of ‘parse_cmdline’ from incompatible pointer type [-Wincompatible-pointer-types]
parse_cmdline(argc, argv);
^
1.c:3:6: note: expected ‘char *’ but argument is of type ‘char **’
void parse_cmdline(int argc, char *argv);
If you write
parse_cmdline(argc, argv);
then parse_cmdline should be
void parse_cmdline(int argc, char *argv[]);
Here I wrote a little app which is able to read command line arguments
int main (int argc, const char * argv[])
{
int c;
while ((c = getopt (argc, argv, "Il:o:vh?")) != -1)
{
switch(c)
{
case 'I':
printf("I");
break;
}
}
return 0;
}
The problem is that when I try to compile it the compiler prints
warning: passing argument 2 of ‘getopt’ from incompatible pointer type
and program crash.
What I miss ?
The argv argument to main should have type char *[], not const char *[] so that it can be converted to the char *const [] that getopt expects. In fact, char *[] or equivalent is mandated by the C standard for hosted implementations.
int main (int argc, const char * argv[])
should be
//no const
int main (int argc, char * argv[])