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,
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
That doesn't seem to be working unfortumately:
The code I have: - #include <stdio.h>
-
-
int main(void)
-
{
-
char n[] = "731671765313306249192251196744265747"; //This keeps going for longer in the actual version
-
int v = n[0];
-
-
printf("%d \n", v);
-
}
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,
Savage 1,764
Recognized Expert Top Contributor
That doesn't seem to be working unfortumately:
The code I have: - #include <stdio.h>
-
-
int main(void)
-
{
-
char n[] = "731671765313306249192251196744265747"; //This keeps going for longer in the actual version
-
int v = n[0];
-
-
printf("%d \n", v);
-
}
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
That seems to have done the trick.
Thanks for the solution :)
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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.,...
|
by: coinjo |
last post by:
Is there any function to determine the length of an integer string?
|
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'));...
|
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...
|
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...
|
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: 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...
|
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: 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...
|
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...
|
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...
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |