473,946 Members | 1,851 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getting segmentation fault if I dont pass any command line parameters

Hi,

I am a beginner to C and I wrote a program that takes arguments and
prints it in hex. I am getting a segmentation fault if I dont pass any
arguments instead I want it to display the Usage message that I have.

Here is my simple program.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char * argv[])
{
char buffer[100];
char ch, * string;
int i, a=0;

if (argc < 1) {
printf("Usage: test <IPv4 Address\n");
}
else {
string = argv[1];
strcpy(buffer, string);
a = strlen(buffer);
printf("a value is: %d", a);
for (i = 0; i < a; i++)
{
printf("Value is: %02x\n", buffer[i]);
//printf("Value is: %X\n", buffer[i]);
}
}
}

Thanks,
doni

Oct 30 '07 #1
5 2084
doni <do********@gma il.comwrites:
if (argc < 1) {
printf("Usage: test <IPv4 Address\n");
}
argc is almost always at least 1 in a hosted environment, because
argv[0] is used to contain the program's name. You should be
testing whether argc is less than 2.
--
int main(void){char p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Oct 30 '07 #2
doni wrote:
>
Hi,

I am a beginner to C and I wrote a program that takes arguments and
prints it in hex. I am getting a segmentation fault if I dont pass any
arguments instead I want it to display the Usage message that I have.

Here is my simple program.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char * argv[])
{
char buffer[100];
char ch, * string;
int i, a=0;

if (argc < 1) {
printf("Usage: test <IPv4 Address\n");
}
else {
string = argv[1];
strcpy(buffer, string);
a = strlen(buffer);
printf("a value is: %d", a);
for (i = 0; i < a; i++)
{
printf("Value is: %02x\n", buffer[i]);
//printf("Value is: %X\n", buffer[i]);
}
}
}
Try: "if (argc < 2) ...".

When printing an address (i.e. a) use the %p specifier, and cast
the address to void*.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Oct 30 '07 #3
On Tue, 30 Oct 2007 15:30:13 -0500, CBFalconer wrote:
doni wrote:
[...]
> int i, a=0;
[...]
> printf("a value is: %d", a);
[...]
When printing an address (i.e. a) use the %p specifier, and cast
the address to void*.
Huh? What address?
--
Army1987 (Replace "NOSPAM" with "email")
A hamburger is better than nothing.
Nothing is better than eternal happiness.
Therefore, a hamburger is better than eternal happiness.

Oct 30 '07 #4
On Oct 30, 11:42 am, Ben Pfaff <b...@cs.stanfo rd.eduwrote:
doni <doni.se...@gma il.comwrites:
if (argc < 1) {
printf("Usage: test <IPv4 Address\n");
}

argc is almost always at least 1 in a hosted environment, because
argv[0] is used to contain the program's name. You should be
testing whether argc is less than 2.
--
int main(void){char p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}

Thanks, Ben.

doni

Oct 30 '07 #5
Army1987 wrote:
CBFalconer wrote:
>doni wrote:
[...]
>> int i, a=0;
[...]
>> printf("a value is: %d", a);
[...]
>When printing an address (i.e. a) use the %p specifier, and cast
the address to void*.

Huh? What address?
Wasn't any. I read too quickly and carelessly.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Oct 30 '07 #6

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

Similar topics

2
6824
by: sivignon | last post by:
Hi, I'm writing a php script which deals with 3 ORACLE databases. This script is launch by a script shell on an linux machine like this : /../php/bin/php ./MySript.php (PHP 4.3.3) My script works fine and do all what I need. But at the end of the execution, I can read "Segmentation Fault". The segmentation fault appear at the end of my script execution,
6
8448
by: jerrygarciuh | last post by:
Hello, I have a script running in the wee hours via cron job. When I access the script via browser it works like a charm. I noticed it was not succeeding and ran it manually from the command line and it threw a Segmentation Fault. Googling produces a ton of information on this subject but al of specific to certain binaries and it doesn't seem germaine to solving my problem. Any one have any advice? Whole script is below.
2
4112
by: Ken Shaffer | last post by:
I am trying to install DB2 UDB 8.1 on linux. I get the segmentation fault on line 130 of db2jinst that others have mentioned. I tried: install on RH Fedora Core 2. got the seg fault error, tried installing sun java 1.4.1 (or .2 - not sure) no help. THen I tried booting to a non-SMP kernel - same error, tried using the LD_ASSUME_KERNEL = 2.2.5 (that was suggested here) -- same error. installed RH ES3.0 update 2 (which IS a...
1
2431
by: Dawn Minnis | last post by:
Hey guys - this code when called with parameters: driver.o n n 12 12 12 12 12 12 2.6 3.2 is kicking back a segmentation fault. I've read the rest of the postings but am still confused. Can someone take a look and tell me how to fix it - please dont be like the guy I spoke to today and tell me that I am not allocating the memory correctly and then walk off. Please, if possible provide me with
1
378
by: Amar Prakash Tripaithi | last post by:
Hi Friends, I am working on a project of User Interface Class Library. In it, I have to create a class library in "gnu" using c++ and curses library. I am facing problem while I am on the way of organizing the file. Actually I had to create the header files. Now, when I initialized the "WINDOW *win" in the Super class Window and destroy it there using "endwin()" command, I am facing segmentation fault. Beside this I had organized the...
1
5395
by: cesco | last post by:
Hi, I'm using the boost library program_options for parsing the command line given to my program as described in the class Parameter: // Main.cpp #include "Parameter.h" using namespace std; int main(int ac, char* av)
9
9078
by: aamircheema | last post by:
Hi, I have written a program (my first big program in c++). When I run the program it gives segmentation fault but when i use a printf statement to debug the program, it runs normally. I am very confused that what may be the reason? Of course I dont want to use printf because it is in loop and prints so many things which I don't want. Any Ideas?
3
1571
by: Kiran | last post by:
Hello All, In my program, I have a main thread which is the GUI (wxPython) and then a thread which goes and reads data from a socket. The reason this is in a different thread is because the data might take some time to come back, and I want to have the GUI to be responsive during this wait. When I run my program in Linux, a segmentation fault occurs. When I run it in Windows XP, it works just fine.
3
5200
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection from 10g release2 PHP is configured with the following parameters './configure' '--prefix=/opt/oracle/php' '--with-apxs=/opt/oracle/apache/bin/apxs' '--with-config-file-path=/opt/oracle/apache/conf' '--enable-safe-mode' '--enable-session'...
0
10151
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9977
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11556
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11333
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10685
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8245
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7412
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6106
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3533
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.