[SOLVED] int main(int argc, char **argv[]) having trouble getting value out of array argv[ ] C programming.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
int main(int argc, char **argv[]) having trouble getting value out of array argv[ ] C programming.
this may to some seem like a stupid question to them that already know how to do this, But this is using pointers so I have no idea how to completely deal with them.
Getting the number off of the command line that is seen as a string? example
Code:
programName 3
get what number was typed in CLI for processing.
break it down? .. OK
If I run just this function I get the results I need to show me my logic in correct.
Code:
#include <stdio.h>
int main(int argc, char **argv[])
{
int i = 1;
// zero based array
// then look for greater then zero
// zero being the program name
if ( argc >= 1 )
printf("argc %d argv[argc-1] %s\n", argc, argv[argc-1]);
/*
if (argc >= 1 )
{
i=argv[argc-1];
printf("this is i %d\n", i);
}
**/
}
That work. It is when I umcomment out the next block of code to get the value out of the element in argv[argc-1] and try to assinge it to 'i'
is where i get a cast issue.
Code:
userx@slackwhere⚡~/bin/C-files $gcc Yathee.c
Yathee.c: In function 'main':
Yathee.c:29:9: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
i=argv[argc-1];
but when I type set it that too does not work.
Code:
userx@slackwhere⚡~/bin/C-files $gcc Yathee.c
Yathee.c: In function 'main':
Yathee.c:28:11: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
i=(int)argv[argc];
nor this
Code:
if (argc >= 1 )
{
i=&argv[argc-1];
printf("this is i %d\n", i);
}
which I got off somewhere else from the same error message googled.
nor this
Code:
userx@slackwhere⚡~/bin/C-files $gcc Yathee.c
Yathee.c: In function 'main':
Yathee.c:28:11: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
i=(int16_t)argv[argc];
int16_t because It just popped up in Kwrite as a selection, so why not try that too?
I have already googled the this error and what I tried on what I found still did not work.
Quote:
assignment makes integer from pointer without a cast [-Wint-conversion]
Anyways regardless, can someone show me how to get the value out of a pointer( I think is a char) and cast it to int properly?
as I keep getting the same error regardless of what I do.
int main(int argc, char *argv[])
/* OR */
int main(int argc, char **argv)
I find the first declaration ("an array of pointers") more intuitive, but the two are equivalent.
Then your 'printf("argc %d argv[argc-1] %s\n", argc, argv[argc-1])' should work;
ha ha mixing the chili soup with sweet apples just does not taste as good. thanks!
let me put them two to the test.
first int main(int argc, char **argv)
Code:
#include <stdio.h>
int main(int argc, char **argv)
{
int i =1;
if ( argc >= 1 )
printf("argc %d argv[argc-1] %s\n", argc, argv[argc-1]);
if (argc >= 1 )
{
i = argv[argc-1];
printf("this is i %d in argv[argc-1] %s\n", i , argv[argc-1]);
}
return 0;
}
results:
Code:
userx@slackwhere⚡~/bin/C-files $gcc array.c
array.c: In function 'main':
array.c:22:10: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
i = argv[argc-1];
next int main(int argc, char *argv[])
Code:
#include <stdio.h>
int main(int argc, char *argv[])
{
int i =1;
if ( argc >= 1 )
printf("argc %d argv[argc-1] %s\n", argc, argv[argc-1]);
if (argc >= 1 )
{
i = argv[argc-1];
printf("this is i %d in argv[argc-1] %s\n", i , argv[argc-1]);
}
return 0;
}
results:
Code:
userx@slackwhere⚡~/bin/C-files $gcc array.c
array.c: In function 'main':
array.c:22:10: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
i = argv[argc-1];
^
Looks like a fail on both accounts to me.
I think you may have missed something.
"Anyways regardless, can someone show me how to get the value out of a pointer( I think is a char) and cast it to int properly?"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
int i =1;
char num[7];
if ( argc >= 1 )
printf("argc %d argv[argc-1] %s\n", argc, argv[argc-1]);
if (argc >= 1 )
{
strcpy(num, argv[argc-1]);
i = atoi(num);
printf("this is i %d in argv[argc-1]%s\n", i , argv[argc-1]);
}
return 0;
}
or a direct call without copying string first.
Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int i =1;
if ( argc >= 1 )
printf("argc %d argv[argc-1] %s\n", argc, argv[argc-1]);
if (argc >= 1 )
i = atoi(argv[argc-1]);
printf("this is i %d in argv[argc-1]%s\n", i , argv[argc-1]);
}
return 0;
}
#include <stdio.h>
int main(int argc, char *argv[])
{
int i =1;
if ( argc >= 1 )
printf("argc %d argv[argc-1] %s\n", argc, argv[argc-1]);
if (argc >= 1 )
{
i = argv[argc-1];
printf("this is i %d in argv[argc-1] %s\n", i , argv[argc-1]);
}
return 0;
}
results:
Code:
userx@slackwhere⚡~/bin/C-files $gcc array.c
array.c: In function 'main':
array.c:22:10: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
i = argv[argc-1];
^
Looks like a fail on both accounts to me.
That second "if ..." block was commented out in your original code, so I didn't examine it. What are you trying to assign to "i"? If it's the value of the pointer (i.e., an address in virtual memory), then you just need to do an explicit cast:
Code:
i = (int)argv[argc-1]
That's going to have portability problems on a 32-bit machine where sizeof(int) < sizeof(char *), but it will demonstrate the principle just fine on a 64-bit machine.
If the actual argument passed to the program is a numeric string and you want its value, then you need to use atoi(), as others have suggested.
That second "if ..." block was commented out in your original code, so I didn't examine it. What are you trying to assign to "i"? If it's the value of the pointer (i.e., an address in virtual memory), then you just need to do an explicit cast:
Code:
i = (int)argv[argc-1]
That's going to have portability problems on a 32-bit machine where sizeof(int) < sizeof(char *), but it will demonstrate the principle just fine on a 64-bit machine.
If the actual argument passed to the program is a numeric string and you want its value, then you need to use atoi(), as others have suggested.
and that is what I did, but error checking to insure it is a "char" number 1-6 and not a char a-z [anything else] .. will be later on.
and as far as not inspecting that block of commented out code,
It's all cool dude, you must have just missed this is all no big deal, drinks on me..
Code:
That work[s]. It is when I umcomment out the next block of code to get the
value out of the element in argv[argc-1] and try to assinge it to 'i'
is where i get a cast issue.
userx%slackwhere ⚡ C-files ⚡> gcc malchar.c
malchar.c: In function ‘main’:
malchar.c:12:9: warning: implicit declaration of function ‘gets’ [-Wimplicit-function-declaration]
gets(input);
^
/tmp/ccFJrg4n.o: In function `main':
malchar.c:(.text+0x39): warning: the `gets' function is dangerous and should not be used.
userx%slackwhere ⚡ C-files ⚡> gcc malchar.c
malchar.c: In function ‘main’:
malchar.c:12:9: warning: implicit declaration of function ‘gets’ [-Wimplicit-function-declaration]
gets(input);
^
/tmp/ccFJrg4n.o: In function `main':
malchar.c:(.text+0x39): warning: the `gets' function is dangerous and should not be used.
I'm not sure why you're getting the "implicit declaration" message, as long as you #include <stdio.h>. The not safe warning is because gets() doesn't do any error checking as far as the length of the string inputed and the size of string allocated to store it, so it can overrun the buffer, leading to either a crash or worse inserting malicious code into your program.
My compiler simply says "warning: this program uses gets(), which is unsafe."
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.