473,395 Members | 1,978 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.

convertin string to integer using atoi() function

Sir please consider the following code......
int getint(char *);
int main ()
{
char name[80];
printf("enter the string\n");
scanf("%s",name);
getint(name);
}
int getint(char *ptr)
{
int x=0;
x=atoi(*ptr);
printf("%d\n",x);
}
WHEN I RUN THIS ON LINUX O/S I AM GETTING "SEGMENTATION" FAULT
COULD YOU PLEASE HELP ME WITH THE PROBLEM
THANK YOU...
Apr 20 '07 #1
11 6467
Banfa
9,065 Expert Mod 8TB
You are passing the wrong parameter type to atoi, if you where to include the correct headers the compiler would pick up this mistake for you.
Apr 20 '07 #2
Savage
1,764 Expert 1GB
Char *ptr represent's ur string so it's simmiliar to ptr[].When u want to print whole array u don't specify array element instead u put it like ptr.The same is with this pointer.So:

x=atoi(ptr);

should work.


Savage
Apr 20 '07 #3
try atoi(ptr) instead of atoi(*ptr)
Apr 20 '07 #4
askcq
63
that works fine for ascii to integer conversion...
example ...if the sting is "123"
and not "123abc"

even I need to know how to convert a string to integer and vice versa
Apr 20 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
The string 123abc is not an integer.
Apr 20 '07 #6
Hello Sir ...I've changed the code as you suggested. If I am giving input string as "martin" the output is zero for me....Could there be any problem in the code...
Please help me !!
Apr 24 '07 #7
Banfa
9,065 Expert Mod 8TB
Hello Sir ...I've changed the code as you suggested. If I am giving input string as "martin" the output is zero for me....Could there be any problem in the code...
Please help me !!
What did you expect it to output? The string "martin" contains no digits, therefore the conversion to an int stops on the 'm' and the function returns 0.
Apr 24 '07 #8
Sir Could you tell me what actually atoi(const char *) fuction does if possible with an example....
Thank you..
Apr 24 '07 #9
Sir suppose lets take two strings string 1 and string 2
string1="arun"
string2="1234"
In the memory wouldnt the accii values of each character be stored..
Apr 24 '07 #10
Savage
1,764 Expert 1GB
Sir Could you tell me what actually atoi(const char *) fuction does if possible with an example....
Thank you..
Atoi converts string to integer.It stop working when it comes to first invalid or '\0' character and returns 0.

Example:

string=123abc;

number=atoi(string);

number=123;

Savage
Apr 24 '07 #11
Savage
1,764 Expert 1GB
Sir suppose lets take two strings string 1 and string 2
string1="arun"
string2="1234"
In the memory wouldnt the accii values of each character be stored..
atoi does not convert char into it ascii decimal value and assagning it to the
number specifed.It searches for specific interval of numbers

From: 48
To: 57
,inclusive.

Savage
Apr 24 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Convert TextBox.Text to Int32 Problem | last post by:
Need a little help here. I saw some related posts, so here goes... I have some textboxes which are designed for the user to enter a integer value. In "old school C" we just used the atoi function...
9
by: priyanka | last post by:
Hi there, I want to convert a String into integer. I get the string froma file using : string argNum; getline(inputStream,argNum); I now need to convert argNum into integer.
6
by: Eli Luong | last post by:
I had a string element, using the string class, and I'm trying to convert this to an integer. I took a substring of the string because that is where the integer is located, and I tried to use...
17
by: mac | last post by:
Hi, I'm trying to write a fibonacci recursive function that will return the fibonacci string separated by comma. The problem sounds like this: ------------- Write a recursive function that...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.