• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C++ question

cliftonite

Diamond Member
Is there a way to manipulate the subscript of an array , so that I can have b [0]['a'] = something? ( i want the column subscript to be all the ascii characters).
 
you want to input a character (or ascii values) into the location of a 2 dim array? or you want to go by characters as location?
 
Originally posted by: Tweak155
you want to input a character (or ascii values) into the location of a 2 dim array? or you want to go by characters as location?

The contents of the array will be integers. So A[1]['a']=2. So i need to set the characters as location.
 
if you are referring as storing ascii characters in the column:

for (int i=0; i<max_columns; i++)
{
char temp='a';
array[0]=temp;
temp++;
}

that will go up to whatever character value you want
 
Originally posted by: cliftonite
Originally posted by: Tweak155
you want to input a character (or ascii values) into the location of a 2 dim array? or you want to go by characters as location?

The contents of the array will be integers. So A[1]['a']=2. So i need to set the characters as location.

yeah i dont see why not, try A[0][atoi(character)]=?
 
Originally posted by: Tweak155
Originally posted by: cliftonite
Originally posted by: Tweak155
you want to input a character (or ascii values) into the location of a 2 dim array? or you want to go by characters as location?

The contents of the array will be integers. So A[1]['a']=2. So i need to set the characters as location.

yeah i dont see why not, try A[0][atoi(character)]=?

to better my example:

A[0][atoi('a')]=2


i'm not sure if you need the single quotes or not
 
This post is probably in the wrong forum, but w/e.

As I understand your question, you want to use characters as parameters for arrays, instead of integers. This is not possible in the sense you are describing. Arrays take numbers as subscripts, not arbitrary characters. Computers are like that.

Now, you could use some behind-the-scenes cleverness. If you typecast a char (which happens to be a lowercase letter) as an int and subtract 97, it turns that letter into its corresponding number in the alphabet, starting with 0. Thus, int ('a') - 97 would equal 0, int ('b') - 97 would equal 1, etc. But you cant throw a char directly at the array, or it will read the ASCII code of your letter instead. Thus, the computer wouldn't see array[0]['a'], but instead array[0][97]. That would be bad for you, most likely.
 
Originally posted by: TheLonelyPhoenix
This post is probably in the wrong forum, but w/e.

As I understand your question, you want to use characters as parameters for arrays, instead of integers. This is not possible in the sense you are describing. Arrays take numbers as subscripts, not arbitrary characters. Computers are like that.

Now, you could use some behind-the-scenes cleverness. If you typecast a char (which happens to be a lowercase letter) as an int and subtract 97, it turns that letter into its corresponding number in the alphabet, starting with 0. Thus, int ('a') - 97 would equal 0, int ('b') - 97 would equal 1, etc. But you cant throw a char directly at the array, or it will read the ASCII code of your letter instead. Thus, the computer wouldn't see array[0]['a'], but instead array[0][97]. That would be bad for you, most likely.

i was thinking the same, but as long as he starts and stops in the same location, 97 could theoretically be 0... just has to make sure when he declares the array, he sets the max size appropriately

EDIT:
and the easy way of doing that would be something like:

int array[4][atoi('z')];
 
Originally posted by: manly
In C, a character is an int.
Close, a character is a byte (8 bits), int's are 32-bits for all the compiler's I've used. Also, you'd have to apply an offset to get your array indexing to start at 0 with the letter 'a'

Basically addressing an array as A[3][0] and A[c-97][a-97] are the same as far as C is concerned.
 
Originally posted by: RaynorWolfcastle
Originally posted by: manly
In C, a character is an int.
Close, a character is a byte (8 bits), int's are 32-bits for all the compiler's I've used. Also, you'd have to apply an offset to get your array indexing to start at 0 with the letter 'a'

Basically addressing an array as A[3][0] and A[c-97][a-97] are the same as far as C is concerned.
Check all the C library functions for character handling; they all take an int argument. Besides, I should have said integer instead.

It's simpler to apply the offset than to use atoi.
 
Originally posted by: manly
Originally posted by: RaynorWolfcastle
Originally posted by: manly
In C, a character is an int.
Close, a character is a byte (8 bits), int's are 32-bits for all the compiler's I've used. Also, you'd have to apply an offset to get your array indexing to start at 0 with the letter 'a'

Basically addressing an array as A[3][0] and A[c-97][a-97] are the same as far as C is concerned.
Check all the C library functions for character handling; they all take an int argument.

It's simpler to apply the offset than to use atoi.

A character can store an integer, but they are different things, if they were the same, why would you need int and char? and why cant you store a character into int?
 
Originally posted by: manly
Originally posted by: RaynorWolfcastle
Originally posted by: manly
In C, a character is an int.
Close, a character is a byte (8 bits), int's are 32-bits for all the compiler's I've used. Also, you'd have to apply an offset to get your array indexing to start at 0 with the letter 'a'

Basically addressing an array as A[3][0] and A[c-97][a-97] are the same as far as C is concerned.
Check all the C library functions for character handling; they all take an int argument.

It's simpler to apply the offset than to use atoi.
I'll take your word for it, but I don't know why a character would be a 32-bit int... an ASCII character is only 7+1 bits 😕
 
Originally posted by: Tweak155
Originally posted by: manly
Originally posted by: RaynorWolfcastle
Originally posted by: manly
In C, a character is an int.
Close, a character is a byte (8 bits), int's are 32-bits for all the compiler's I've used. Also, you'd have to apply an offset to get your array indexing to start at 0 with the letter 'a'

Basically addressing an array as A[3][0] and A[c-97][a-97] are the same as far as C is concerned.
Check all the C library functions for character handling; they all take an int argument.

It's simpler to apply the offset than to use atoi.

A character can store an integer, but they are different things, if they were the same, why would you need int and char? and why cant you store a character into int?
A char is a byte, and int is just the native word size of the platform.

Anyway, I should have been slightly clearer the first time, but a character is an integer in C. All the APIs explicitly say so. But you can't directly use a char as an array index of course. I'm also implicitly disagreeing with the previous suggestions. 😛

You can do math with chars, for example consider 'a' - 'a'

C++ is a different story of course.
 
Basically it's probably cleaner/saner to go about this a different way. Maybe you want a std::map? (and I stress the maybe; maps can be so damn cumbersome 😛)
 
depends on your compiler warning level but you should just be able to pass a char into the indexer and it work just fine as an ascii character is just a byte in c, so it should resolve fine and work.

edit: did you try this before you asked? or you just asked first?
 
Originally posted by: yukichigai
Yeah, atoi would be my bet.

atoi is not appropiate in this case, the most he needs to do is cast the charcter into a byte but he probably doesn't even have to do that as it is single char.
 
ohh and make sure you subtract a constant offset because the letters in the ascii table are in the 60s for uppercase and the 90s for lower case


so
'a'-97 == 0
'b'-97 == 1
etc.

if you dont want to do the subtraction in every call you will have to do something slightly smarter like overloading the indexer opperators to do the subtraction for you. or you can simply overallocate the array but that sucks.

 
Originally posted by: cliftonite
Is there a way to manipulate the subscript of an array , so that I can have b [0]['a'] = something? ( i want the column subscript to be all the ascii characters).

Bored at work:

Make a function. This is the best way to do it. If using C++, make a private array of the proper size.

// Code below is assuming range is uppar case A to upper case Z do the following. 'A' = 65 in ASCII

int storedValue[128][26];

foo (int a, char b, int newVal)
{
/* Do error checking for valid character (in range, etc). */
if ((b < 'A') || (b > 'Z'))
return;

/* If character is OK, modify the ascii value of hte minimum acceptable value. */
storedValue[a][atoi(b)-65)] = newVal;

}

// You might want to do error checking for the value of a also
 
enum // array_index
{
a = 1,
b,
c,
d,
e
};



Note that you can not use any of the enumerations for anythin other than what they indicate - a number.

Setting a equal to 1 forces the indexing to start from one, otherwise it will start from zero.
 
Back
Top