Whats wrong

W

Wally

int main(int argc, char *argv[]) {
int c;
int port=0;
char *srvname = NULL;
extern char *optarg;

/*
**Determine whether we are converting a port
**number to a server name or vice versa.
*/

while ( (c = getopt(argc, argv, ":pnh")) != -1) {
switch(c) {
case 'p': port = *optarg;
printf("port number is %d\n",port);
break;
case 'n': srvname = *optarg;
printf("server name is %s\n",srvname);
break;
case '?': usage();
break;
default: usage();
}
}
}

I get a segmentation fault with the p or n option. Any help would be
appreciated.
 
V

Vladimir Oka

Wally said:
int main(int argc, char *argv[]) {
int c;
int port=0;
char *srvname = NULL;
extern char *optarg;

/*
**Determine whether we are converting a port
**number to a server name or vice versa.
*/

while ( (c = getopt(argc, argv, ":pnh")) != -1) {
switch(c) {
case 'p': port = *optarg;
printf("port number is %d\n",port);
break;
case 'n': srvname = *optarg;
printf("server name is %s\n",srvname);
break;
case '?': usage();
break;
default: usage();
}
}
}

I get a segmentation fault with the p or n option. Any help would be
appreciated.

It seems that the `optarg` pointer is not initialised (at all, or
properly), and you're trying to dereference it. Since it's declared
external, and you don't tell where and what happens to it, it's really
impossible to say more. Look into the code that actually declares and
initialises `optarg`.
 
M

Michael Mair

Wally said:
int main(int argc, char *argv[]) {
int c;
int port=0;
char *srvname = NULL;
extern char *optarg;

You forgot to explain where optarg is actually define --
it is definitely not a Standard C thing.
/*
**Determine whether we are converting a port
**number to a server name or vice versa.
*/

while ( (c = getopt(argc, argv, ":pnh")) != -1) {

No prototype of getopt().
switch(c) {
case 'p': port = *optarg;

As you did not make sure that optarg is initialised and
did not explain how getopt() and optarg interact, there
is no telling whether optarg holds some valid value (i.e.
a null pointer representation or an address).
If this is a getopt() thing, please ask in a newsgroup
where getopt() is on-topic or provide an explanation how
getopt() works.
printf("port number is %d\n",port);

You forgot to
#include said:
break;
case 'n': srvname = *optarg;

If your compiler compiled this without diagnostic message,
throw it away.
printf("server name is %s\n",srvname);
break;
case '?': usage();

No prototype of usage().
break;
default: usage();
}
}
}

I get a segmentation fault with the p or n option. Any help would be
appreciated.

Please provide a minimal compiling example and copy and
paste it; otherwise, turn up the warning level of your
compiler to maximum and work your way through the warnings
until you understood all of them and eliminated the
reasons for those that stem from real errors.


Cheers
Michael
 
R

Richard Tobin

[program using getopt]

getopt() isn't a standard C function; you'd be better off trying a unix
or posix newsgroup.
int c;
int port=0;
char *srvname = NULL;
extern char *optarg;

...
case 'p': port = *optarg;

Here you're taking the first character pointed to by a char * and
storing the result in an integer. This is unlikely to be right
if the variable "port" is supposed to be a IP port number.
case 'n': srvname = *optarg;

Here you're taking that first character and assigning it to a char *,
which is bound to be wrong (and you should be getting at least a
warning from your compiler).

The fact that optarg is a char * should give you a clue: it points to
a string, and you need to convert that string to an integer
in the first case and assign or copy it in the second.

-- Richard
 
O

Olivier Galibert

int main(int argc, char *argv[]) {
int c;
int port=0;
char *srvname = NULL;
extern char *optarg;

/*
**Determine whether we are converting a port
**number to a server name or vice versa.
*/

while ( (c = getopt(argc, argv, ":pnh")) != -1) {
switch(c) {
case 'p': port = *optarg;
printf("port number is %d\n",port);
break;
case 'n': srvname = *optarg;
printf("server name is %s\n",srvname);
break;
case '?': usage();
break;
default: usage();
}
}
}

I get a segmentation fault with the p or n option. Any help would be
appreciated.

You misread the getopt manpage, the parameter definition string should
be "p:n:h". ':' means the _previous_ option requires a parameter.

OG.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top