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.
(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.