473,406 Members | 2,769 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,406 software developers and data experts.

Convert array element

I need to convert an array element (char type) to a int variable.Need to know how i cast it.language is c. If any one have an idea about this please send me a reply. <email address removed per Posting Guidlines>
Mar 8 '07 #1
5 1948
Hi,
Try defining the array variable as type void, then typecast it to the different types.

ex.
#include <stdio.h>
#include <stdlib.h>
int main(){
void *x = NULL;
x = (int *)1;
printf("the value is %d\n",(int *)x);
x = (char *)'A';
printf("the character is %c\n",(char*)x);
}

result:
the value is 1
the character is A


Hope this helped.

Desmond
Mar 8 '07 #2
Remember code tags:
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int main(){
  5.    void *x = NULL;
  6.    x = (int *)1;
  7.    printf("the value is %d\n",(int *)x);
  8.    x = (char *)'A';
  9.    printf("the character is %c\n",(char*)x);
  10. }
  11.  
Could you perhaps explain what happens at
Expand|Select|Wrap|Line Numbers
  1.  void *x = NULL;
  2.  
Why are you using that?
I'm a C++ girl so I'm just wondering ;-)
Mar 8 '07 #3
horace1
1,510 Expert 1GB
I need to convert an array element (char type) to a int variable.Need to know how i cast it.language is c. If any one have an idea about this please send me a reply. <email address removed per Posting Guidlines>
character variables are stored in C as byte sized integers so there is no need to cast between the types
e.g. consider this fragment of code when contains an array of char, the elements of which are printed as character using %c and decimal integer using %d
Expand|Select|Wrap|Line Numbers
  1.     char ch[]={'A','B','C'};
  2.     int i;
  3.     for(i=0; i<3;i++)
  4.       printf("character %c integer value %d\n", ch[i], ch[i]);
when run gives
character A integer value 65
character B integer value 66
character C integer value 67

the ASCII character code for A is 65 decimal
Mar 8 '07 #4
hi,
You said in you initial statement that the language is c. I assumed you wanted c version.

Anyway, it does not matter. "void *" takes anything. This allows you to store and retrieve any data type that you want by type casting it when storing, retrieving, allocating,and distroying the memory area. "void *" gives you more control of you datatypes.
It also works perfectly in c++. I am also a c++ developer.

There are also other ways.

desmond.
Mar 8 '07 #5
hi,,
you could also do the following:

char ch[]={'A','B','C'};
int i;
for(i=0; i<3;i++) {
int val = (int)ch[i];
printf("character %c integer value %d\n", ch[i], val);
}
desmond
Mar 8 '07 #6

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

Similar topics

3
by: Tome73 | last post by:
How would I convert this string to an Array? "243, 567, 324, 345" Where each number becomes a new element in that Array. Thanks
5
by: overbored | last post by:
I can do this: int asdf; int* zxcv = asdf; but not this: int asdf; int** zxcv = asdf;
13
by: Bryan Parkoff | last post by:
I have two variables: "char A" and "short B". I can be able to convert from A to B using explicit case conversion with no problem like "B = short (A);". Right now, I have two variables: "char T"...
4
by: Tamir Khason | last post by:
How can I convert (elegant way) Collection to Array without looping and new instances. E.G: I want to add elements of one menu to other, so secondMenu.MenuItems.AddRange(firstMenu.MenuItems);...
8
by: Stephen Miller | last post by:
Hi, I am converting a code sample found at http://support.microsoft.com/default.aspx?scid=kb;en-us;828279 from C# to VB and I am stuck on one line of code. How would I convert: object pos =...
5
by: Peter Nofelt | last post by:
Hi all, My scenario is as follows: * Receive a base 1 array (index starts at 1 instead of 0) of data from a COM component . * Need to pass this array to a .net component that requires its...
0
by: lvpaul | last post by:
Hello ! I am calling a .NET Webservice and getting back a ADO.NET Datatset. How can I convert this to an PHP-Array ? $ergebnis = $m_service->call( $function, $data ); print_r($ergebnis)...
1
by: simply123 | last post by:
I doing doing C btw... i have been trying to convert array elements into their respective addresses but Im faced with many problems. eg. int x (array with 7 elements) im trying to set the...
15
by: Steve | last post by:
I am having problems getting values out of an array. The array is set as a global array and values are pushed into it as they are read from a JSON file using a "for loop". When the "for loop" is...
2
by: AMP | last post by:
Hello, I want to convert a string Array permanantly to a int Array. Everywhere in my code I have to use Convert.ToInt32 to use the data. I just want to change it once. Thanks Mike
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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:
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
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.