Extracting elements off an array of pointers - arrays

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[])

Related

How to call another function that utilizes argc, char *argv[] in main without passing things to it?

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

how to copy an element of a string array to another string variable?

int main(int argc, string argv[])
{
this is what I want to do but it obviously won't work
string key = argv[1];
here I want to use the 'key' string as an array of characters to deal with a specific character alone
key = key[1] + 1;
printf("%s", key);
}
the main function is not well formatted . argv is a 2D array.
int main(int argc, char **argv)
then you can malloc your key variable and use for example the strcpy function to copy argv[1] in the key variable.
#include <string.h>
#include <stdio.h>
int main(int ac, char **av){
char *key = strdup(av[1]);
printf("%s", key);
free(key)
return (0);
}

"Pointer from integer without cast" warning when opening file.

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?

Execute external program with specific parameters from windows c/c++ code

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

Modify struct from function?

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)

Resources