Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
When I try and use atoi with an int and malloc I get a bunch of errors and key is given the wrong value, what am I doing wrong?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct arguments {
int key;
};
void argument_handler(int argc, char **argv, struct arguments *settings);
int main(int argc, char **argv) {
argv[1] = 101; //makes testing faster
struct arguments *settings = (struct arguments*)malloc(sizeof(struct arguments));
argument_handler(argc, argv, settings);
free(settings);
return 0;
}
void argument_handler(int argc, char **argv, struct arguments *settings) {
int *key = malloc(sizeof(argv[1]));
*key = argv[1];
settings->key = atoi(key);
printf("%d\n", settings->key);
free(key);
}
You probably want this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct arguments {
int key;
};
void argument_handler(int argc, char** argv, struct arguments* settings);
int main(int argc, char** argv) {
argv[1] = "101"; // 101 is a string, therefore you need ""
struct arguments* settings = (struct arguments*)malloc(sizeof(struct arguments));
argument_handler(argc, argv, settings);
free(settings);
return 0;
}
void argument_handler(int argc, char** argv, struct arguments* settings) {
char* key = malloc(strlen(argv[1]) + 1); // you want the length of the string here,
// and you want char* here, not int*
strcpy(key, argv[1]); // string needs to be copied
settings->key = atoi(key);
printf("%d\n", settings->key);
free(key);
}
But this is very awkward, actually the argument_handler can be rewritten like this:
void argument_handler(int argc, char** argv, struct arguments* settings) {
settings->key = atoi(argv[1]);
printf("%d\n", settings->key);
}
Disclaimer: I only corrected what was obviously wrong, there are still checks that need to be done, e.g. if argc is smaller than 2 etc.
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 );
}
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);
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have the following code:
int ver(unsigned char** v) {
unsigned char str1[] = "1.0.2";
strcpy(v, str1);
return 0;
}
int main(int argc, char* argv[]) {
unsigned char s[10];
ver(s);
printf("version = %s", s);
return 0;
}
I get the following warning that the pointer differs in signedness. Can you please advise.
Change the declaration of ver to:
int ver(unsigned char * v)
I.e. get rid of one of the * characters.
Warning about signedness is because strcpy and printf("%s") expect a char*, but you are passing unsigned char*
Also int ver(unsigned char ** v) should be int ver(char* v)
Actually the problem is this :
int ver(unsigned char ** v)
You have to change the function to that :
int ver(unsigned char * v)
You need to do 2 fixes:
1) Follow what others told you about the method declaration, changing to "int ver(unsigned char* v) {" ;
2) Just include <string.h> at your code.
Example:
#include <stdio.h>
#include <string.h>
int ver(unsigned char* v) {
unsigned char str1[] = "1.0.2";
strcpy(v, str1);
return 0;
}
int main(int argc, char* argv[]) {
unsigned char s[10];
ver(s);
printf("version = %s", s);
return 0;
}
The cause of the warning:
strcpy() expects a signed char*, and is called with a unsigned char*.
But there should be other warnings as well:
Function ver() expects a pointer to a pointer:
int ver(unsigned char ** v){
ver(s) sends a single pointer to char:
unsigned char s[10];
ver(s);
To solve, you can change the code to:
int ver(char *v) {
char str1[] = "1.0.2";
strcpy(v, str1);
return 0;
}
int main(int argc, char *argv[]){
char s[10];
ver(s);
printf("version = %s", s);
return 0;
}
This question already has answers here:
Assignment of function parameter has no effect outside the function
(2 answers)
Closed 9 years ago.
I am trying to understand why this statement doesn't work.
char resp[] = "123456789";
void getValue(char *im)
{
im = resp;
printf("\n%s\n",im);
}
int main(int argc, char *argv[])
{
char imei[11] = {0};
getValue(imei);
printf("\nIMEI: %s\n",imei);
return 0;
}
Output:
123456789
IMEI:
You can not assign with =, use strcpy instead:
#include <stdio.h>
#include <string.h>
char resp[] = "123456789";
void getValue(char *im)
{
im = strcpy(im, resp);
printf("\n%s\n",im);
}
int main(int argc, char *argv[])
{
char imei[11] = {0};
getValue(imei);
printf("\nIMEI: %s\n",imei);
return 0;
}
That's because imei is an array[11] (not just a pointer to), if you want to assign via = you can:
#include <stdio.h>
char resp[] = "123456789";
void getValue(char **im)
{
*im = resp;
printf("\n%s\n",*im);
}
int main(int argc, char *argv[])
{
char *imei; /* Not an array but a pointer */
getValue(&imei);
printf("\nIMEI: %s\n",imei);
return 0;
}
C passes parameters by value. Whatever change you ale to the im will be lost when the function exits. If you want to preserve the change. Pass the address of the pointer. Then you can change the pointer at the address you pass.
Try this:
char resp[] = "123456789";
void getValue(char **im)
{
*im = resp;
printf("\n%s\n",*im);
}
You need to pass a pointer to a pointer as your program argument.
I want to modify file in the items struct from parse_commandline().
I can without problems modify items->file from main() by using strncpy, but not from parse_commandline(). I need to modify parse_commandline() so it can recieve information about items from main(), by i don't know how?
typedef struct {
int pack_01;
int pack_02;
char file[100];
} items;
static items character_y = { 1, 1 }
parse_commandline(int argc, char *argv[])
{
/* PARSE COMMANDLINE ARGUMENTS */
}
int main(int argc, char* argv[])
{
items *ptr_items = &character_y;
parse_commandline(argc,argv);
return 0;
}
The way to do this is pass a pointer to items to the parse_commandline function and let the function update the structure based on the arguments.
parse_commandline(int argc, char *argv[], items* pItems) {
pItem->pack_01 = 42;
...
};
int main(int argc, char* argv[]) {
items items;
parse_commandline(argc, argv, &items);
...
}
Passing a structure to a function in C is no different from passing any other variable. In your case, you should do it by reference so that you can modify the caller's structure:
void parse_commandline(int argc, char *argv[], items *theItems)