473,573 Members | 2,122 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2122

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************ **********@j55g 2000cwa.googleg roups.com>,
Wally <cr*****@aciwor ldwide.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*****@aciwor ldwide.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
3460
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) Case "Authenticated" Case "CI_CODE" Case "organisation_description" Case "location_description"
3
2385
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."', '".$_POST."')"); ?> <script language="JavaScript"> <!--
4
4059
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
1976
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 wrong ?? tks
1
2452
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. ---------------------------------------------------------------------------- ---- The specified user does not exist. Description: An unhandled exception occurred...
3
2037
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');" %>'>Need Help?</asp:HyperLink> -- mahsa
4
3528
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 whole application (its small for testing purposes) and it seems that window wraps the text, at least when I preview this post: using System; using...
1
2487
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. Whats wrong? Any clues?
7
2220
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 me why? Its probably sooooooo obvious, but it is 1.19 am! Thanks. www.thunderin.co.uk/
5
2312
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 the number. whats wrong with my code? #!/usr/bin/perl use Fcntl; @array = (a..z); srand; foreach (1..5) { $name = int(rand...
0
7793
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7710
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8222
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8092
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5606
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3738
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3745
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2230
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1049
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.