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)
Related
say there is a function in my code:
int getwords(int argc, char *argv[])
and I want to call this function in main().
How do I call this in main without erroring out?
`void main(void)
getwords();`
If you write main to provide access to argc and argv:
int main(int argv, char **argv) {
// ...
}
Then you can pass those to another function when you call it:
void foo(int argc, char **argv) {
// ...
}
int main(int argc, char **argv) {
foo(argc, argv);
}
argc will be copied, but argv is a pointer, so the pointer is copied
but not the data it points to.
if for some special reason you do not want to use parameters you can use global variables available in all program.
int gargc;
char **gargv;
int main(int argc, char *argv[])
{
gargc = argc;
gargv = argv;
getopt();
}
Suppose we have this main:
int main(int argc,char *argv[]){
}
How could I manage to get the elements of *argv[],in order to extract some statistics off of each one(e.g letter count,numbers ,etc.)?
I have tried couple of things but didn't work(using pointers ).Eventually I tried a work-around,using strcpy() to copy each element of the array and that worked.
So my question is,are there other ways to achieve that?
Here's an example of my code that works:
int main(int argc,char *argv[]){
char temp[50];
strcpy(temp,argv[1]); //extracting the first element of the array.
Another way of doing so is as below:
int main(int argc,char argv[]){
char *temp = argv;
}
This code will create a string named "temp" which is a copy of "argv[]". You can use to extract any stats you like without having to use the "strcopy" function.
Also, take note that I have changed the argument from
int main(int argc, char *argv[])
to
int main(int argc, char argv[])
I've made a function, but I'm struggling with calling it.
This is the prototype of the function:
char *test(int argc, char **argv);
I've tried calling it this way but it doesn't work :
int main()
{
char tab[3][3] ={
"Yo",
"Hi"};
test(2, tab);
return (0);
}
For me this works:
char* test(int index, char** char2Darray)
{
return char2Darray[index];
}
int main()
{
char* tab[2] ={
"Yo",
"Hi"};
test(1, tab);
return (0);
}
I think there are two problems in your code :
the tab what you provided has only two item
your tab is a pointer which pointing to char[3] types and not char*
When you pass the 2D array
char tab[3][3]
to the function test(), it decays to a pointer of type:
char (*)[3]
which is a pointer to an array of 3 char elements.
This type is not compatible with char** which is a pointer-to-pointer to char. This is the source of your problem. You need to change the function test() to take char (*argv)[3] instead of char **argv.
Looking at function prototype:
char *test(int argc, char **argv);
char **argv is pointer of pointers and the variable char tab[3][3] is array of arrays so they are incompatible.
You can change function prototype to: char *test(int argc, char argv[][SOMECONSTANT]); or char *test(int argc, char (*argv)[3]);
This way
char * test(int argc,const char **argv);
int main()
{
const char * tab[3]=
{
"Yo",
"Hi",
0
};
test(2,tab);
return 0;
}
or
char * test(int argc,char **argv);
int main()
{
char arg1[]="Yo";
char arg2[]="Hi";
char * tab[3]=
{
arg1,
arg2,
0
};
test(2,tab);
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 call Program1 from Program2 with exact same parameters which I called Program2 with.
In Linux, I can do it like this:
int main(char argc, char* argv[]){
execv("./Program1", argv);
}
In windows, I tried CreateProcess
but as the first post says there is potential issue: "argv[0] Doesn't Contain the Module Name as Expected". I do want to send proper argv[0] to Program1. What should I do?
argv[0] is the name of the program itself.
You should do :
int main(char argc, char **argv)
{
char* argvForProgram1[] = { "./Program1", 0 }
execv(argvForProgram1[0], argvForProgram1);
}
or to keep your previous args :
int main(char argc, char **argv)
{
char** argvForProgram1 = argv;
argvForProgram1[0] = "./Program1";
execv(argvForProgram1[0], argvForProgram1);
}
Using execve is better too because you keep the environment:
int main(char argc, char **argv, char **envp)
{
char** argvForProgram1 = argv;
argvForProgram1[0] = "./Program1";
execve(argvForProgram1[0], argvForProgram1, envp);
}