473,395 Members | 1,666 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Whats wrong

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.

Jun 5 '06 #1
4 2118

Wally wrote:
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`.

Jun 5 '06 #2
Wally schrieb:
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 <stdio.h>
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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jun 5 '06 #3
In article <11**********************@j55g2000cwa.googlegroups .com>,
Wally <cr*****@aciworldwide.com> wrote:
[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
Jun 5 '06 #4
On 2006-06-05, Wally <cr*****@aciworldwide.com> wrote:
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.

Jun 5 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Colin Steadman | last post by:
I have created a function to kill all session variables that aren't in a safe list. This is the function - Sub PurgeSessionVariables For Each Item In Session.Contents Select Case Trim(Item)...
3
by: Chris Geerdink | last post by:
combo with PHP. what is wrong with the Javascript? else { include("mysql.php"); $query1 = mysql_query("INSERT INTO gbook (naam, email, text) VALUES ('".$_POST."', '".$_POST."',...
4
by: asdf | last post by:
Hello! Can someone tell me whats wrong with this piece of code: Option Compare Database Option Explicit Sub retrieve() Dim rst As ADODB.Recordset Dim i As Integer
5
by: Alexandre Martins | last post by:
Provider=Microsoft.Jet.OLEDB.4.0;UserId=Admin;Password=teste;Data Source=C:\Inetpub\wwwroot\inktoner\dados\db_inktoner.mdb;Persist Security Info=True I can't connect in my database ! whats...
1
by: aa | last post by:
When I am reading from local disk (d:), everithing is OK, but then I am reading from map disk I am geting the this error. Whats wrong. Thanks Server Error in '/Extra' Application....
3
by: mahsa | last post by:
Hi do you know whats wrong with this code? <asp:HyperLink id="HLink_Help" runat="server" NavigateUrl='<%# "javascript:window.open('comments.aspx?id=1,width=500,height=600, scrollBars=yes');"...
4
by: blah | last post by:
Hello everyone, Ive been trying to get my application to "click" on a button in another application using SendMessage, Ive gotten this far but Im not sure whats wrong with this code, here is the...
1
by: '~=_Slawek_=~' | last post by:
$DOW = (jddayofweek(unixtojd(mktime(1, 1, 1, $month, $day, $year)))+6)%7; $DOW= (jddayofweek(juliantojd($month, $day, $year))+6)%7; The results are supposed to be the same, but they are not....
7
by: Mike Barnard | last post by:
It's a simple test... VERY SIMPLE. But... In an external stlyesheet some attributes don't show. With the same styles cut and pasted to the test internally it works as expected. Anyone tell...
5
by: islayer | last post by:
can someone tell me what is wrong with the bold code? i am just learning perl. the program should create a perl file with a random name (5 letters, followed by a number), but the name is always just...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.