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. 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`.
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.
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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)...
|
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."',...
|
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
|
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...
|
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....
|
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');"...
|
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...
|
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....
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |