PDA

View Full Version : Help with variables in C


Voltar
11-15-2009, 03:18 AM
I am attempting to process command line arguments and store them into variables for use later. The following is what I came up with so far, the printf at the end is just temporary to make sure the variables took.

(I am still fairly new to C, so I'm sure it is something trivial, or the entire thing could be better overall)

#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[])
{
char IP[1024], defaultrouter[1024], hostname[1024], nameserver[1024];
int i;
for (i=1; i < argc; i++)
{
if (i + 1 != argc)
{
if (argv[i] == "-ip")
{
IP = "argv[i + 1]";
}
else if (argv[i] == "-defaultrouter")
{
defaultrouter = "argv[i + 1]";
}
else if (argv[i] == "-hostname")
{
hostname = "argv[i + 1]";
}
else if (argv[i] == "-nameserver")
{
nameserver = "argv[i + 1]";
}
}
}
printf("IP address is %s\n", IP);
printf("defaultrouter is %s\n", defaultrouter);
printf("hostname is %s\n", hostname);
printf("nameserver is %s\n", nameserver);
return EXIT_SUCCESS;
}
Compiling it with gcc gives:
freebsd-dev# gcc setup.c
setup.c: In function 'main':
setup.c:14: error: incompatible types in assignment
setup.c:18: error: incompatible types in assignment
setup.c:22: error: incompatible types in assignment
setup.c:26: error: incompatible types in assignment

Thanks in advance.

oracleguy
11-15-2009, 03:38 AM
You need to use strcpy instead of trying to assign the strings. Or instead of declaring the variables as arrays on the stack, just create pointers.

oesxyl
11-15-2009, 03:54 AM
look into getopt, it already do what you want to do:

http://www.gnu.org/software/hello/manual/libc/Getopt.html

also argv is a array of pointer, I guess you don't need to copy it's enought to declare for example IP as char * and assign it the argv pointer.

char * IP;
// somewhere in your code:
IP = argv[i+1];


best regards

oracleguy
11-15-2009, 04:30 AM
look into getopt, it already do what you want to do:

http://www.gnu.org/software/hello/manual/libc/Getopt.html


getopt is really nice but it isn't available in all C libraries.

oesxyl
11-15-2009, 04:39 AM
getopt is really nice but it isn't available in all C libraries.
that's true, :)
as far as I know is ported on allmost all platforms and IMO is easy to add the static library to a project but this is another assumption, :)

best regards

testware
11-21-2009, 04:55 AM
>#include <stdio.h>
>#include <stdlib.h> //Not necessary IMO
>#include <string.h> //Needed in C/C++ for array based string manipulations
>
>int main (int argc, char *argv[])
>{
> char IP[1024], defaultrouter[1024], hostname[1024], nameserver[1024];
> int i;
> for (i=1; i < argc; i++)
> {
> if (i + 1 != argc)
> {
> if (strcmp(argv[i],"-ip")==0) //Not (argv[i] == "-ip")
> {
> strcpy(IP,argv[i+1]); //Not IP = "argv[i + 1]";
> }
> else if(strcmp(argv[i],"-defaultrouter")==0) //Not (argv[i] == "-defaultrouter")
> {
> strcpy(defaultrouter,argv[i+1]); //Not defaultrouter = "argv[i + 1]";
> }
> else if(strcmp(argv[i],"-hostname")==0) //Not (argv[i] == "-hostname")
> {
> strcpy(hostname,argv[i+1]); //Not hostname = "argv[i + 1]";
> }
> else if(strcmp(argv[i],"-nameserver")==0) //Not (argv[i] == "-nameserver")
> {
> strcpy(nameserver,argv[i+1]); //Not nameserver = "argv[i + 1]";
> }
> }
> }
> printf("IP address is %s\n", IP);
> printf("defaultrouter is %s\n", defaultrouter);
> printf("hostname is %s\n", hostname);
> printf("nameserver is %s\n", nameserver);
> return EXIT_SUCCESS;
>}

Simple hint: Array based strings can't be manipulated by direct assignments (Unless on of them is a class and you've written an operator function) - they need string functions given in string.h

strcmp(s1,s2) returns 0 if both are equal
strcpy(s1,s2) copies s2 onto s1

:) Have a nice day and enjoy C++