473,473 Members | 1,555 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C - integer from string in array of chars (or array of integers?)

10 New Member
Hi.

I've just thrown myself head first into C, and am having a bit of trouble getting my head round the way the datatypes and arrays work.

At the moment, I'm working on a problem which involves a very long (1000 digit) number - I need to pick out individual digits from it, rather than use it as a whole. The idea I initially had was to do:

char n[] = "1232534654734634...";
int v = n[0]; //Presumably some type conversion needed?

But that didn't seem to work: the compiler would just throw out a 'makes pointer from integer without a cast' message.

The second idea was storing the values in an array of integers, therefore negating the need for switching between types, but I don't know how to go about it.

Could anyone help?

Thanks,
Sep 18 '07 #1
5 1670
Savage
1,764 Recognized Expert Top Contributor
Hi.

I've just thrown myself head first into C, and am having a bit of trouble getting my head round the way the datatypes and arrays work.

At the moment, I'm working on a problem which involves a very long (1000 digit) number - I need to pick out individual digits from it, rather than use it as a whole. The idea I initially had was to do:

char n[] = "1232534654734634...";
int v = n[0]; //Presumably some type conversion needed?

But that didn't seem to work: the compiler would just throw out a 'makes pointer from integer without a cast' message.

The second idea was storing the values in an array of integers, therefore negating the need for switching between types, but I don't know how to go about it.

Could anyone help?

Thanks,
To convert between int and char you only need to add +'0':(ASCII code for 0)

char c='1';
int v=c+'0';

now v is 1;

Savage
Sep 18 '07 #2
dperren
10 New Member
That doesn't seem to be working unfortumately:

The code I have:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5.     char n[] = "731671765313306249192251196744265747"; //This keeps going for longer in the actual version
  6.     int v = n[0];
  7.  
  8.     printf("%d \n", v);
  9. }
This outputs '55'. Changing 'int v = n[0];' to 'int v = n[0]+'0'; outputs 103. Possibly the correct ascii code for the character, but not what I'm after.

I want n[0] = 7, n[1]=3, etc.

Thanks for the help so far,
Sep 18 '07 #3
Savage
1,764 Recognized Expert Top Contributor
That doesn't seem to be working unfortumately:

The code I have:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5.     char n[] = "731671765313306249192251196744265747"; //This keeps going for longer in the actual version
  6.     int v = n[0];
  7.  
  8.     printf("%d \n", v);
  9. }
This outputs '55'. Changing 'int v = n[0];' to 'int v = n[0]+'0'; outputs 103. Possibly the correct ascii code for the character, but not what I'm after.

I want n[0] = 7, n[1]=3, etc.

Thanks for the help so far,
Sorry my bad,you should decrease it for '0'

Savage
Sep 18 '07 #4
dperren
10 New Member
That seems to have done the trick.

Thanks for the solution :)
Sep 18 '07 #5
Savage
1,764 Recognized Expert Top Contributor
That seems to have done the trick.

Thanks for the solution :)
I'm more than happy to help you.

Savage
Sep 18 '07 #6

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

Similar topics

4
by: David Lawson | last post by:
I know how to conver a string to an array of strings, but I need to convert an ascii string to an array of integers (really unsigned chars). Eg, $str ="ABC"; needs to convert to something...
21
by: Glen Wheeler | last post by:
Hello all, My program uses many millions of integers, and Python is allocating way too much memory for these. I can't have the performance hit by using disk, so I figured I'd write a C...
11
by: johnny | last post by:
Can anyone tell me what is wrong with the following code fragment? char a = x; char b = y; int result = strtol(&a, NULL, 10) + strtol(&b, NULL, 10); I got the same result using...
27
by: Daniel Lidström | last post by:
Hello! I want to work with individual bytes of integers. I know that ints are 32-bit and will always be. Sometimes I want to work with the entire 32-bits, and other times I want to modify just...
8
by: Jeff | last post by:
Hello everybody, I'm kind of new to C programming, but here's a little question. Usually, when you have an array of chars, you put a \0 at the end of it to terminate the string. That way, it is...
20
by: GS | last post by:
The stdint.h header definition mentions five integer categories, 1) exact width, eg., int32_t 2) at least as wide as, eg., int_least32_t 3) as fast as possible but at least as wide as, eg.,...
13
by: coinjo | last post by:
Is there any function to determine the length of an integer string?
6
by: comp.lang.php | last post by:
I'm involved in a rather nasty debate involving a strange issue (whereby the exasperated tell me to RTFM even after my having done so), where this is insanely possible: print_r(is_int('1'));...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
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
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...
1
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...
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,...
1
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...
0
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...
0
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...
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.