I got a question about array locations

1
2
3
4
foo[0] = a;
foo[a] = 75;
b = foo [a+2];
foo[foo[a]] = foo[2] + 5;


I am reviewing what I have watched, and things I had missed are getting appeared.
The above is one of them. what I thought is, "foo" is name of the array, and the number between "[]" is supposed to be how many spaces the array has or the location in the array.

So, the first line is assigning a to the first place in the array "foo",
and the second line doesn't make sense to me.
the third line is assigning the value in the place two blocks next to block 'a', but how can character 'a' be a place in array?
Last edited on
So, the first line is assigning a to the first place in the array "foo",

Correct.

and the second line doesn't make sense to me.

The second line assigns 75 to the a'th entry in foo.
You don't give a type or value for a, but let's assume it's an int with a value of 10.
Then the second line assign 75 to foo[10].

but how can character 'a' be a place in array?

Again, you haven't shown the definition of a, so I can only assume what it might be.
There is a difference between a and 'a'. a is a variable. 'a' is a character literal.
Even if the declaration is:
 
char a;

a would still be treated as integer type capable of holding a number from -127 to + 127.

The fourth line takes the value in foo[2] and adds 5. The value in foo[a] is then used as a subscript into foo to store the result of the calculation on the right side.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
The value in foo[a] is used as a subscript into foo to store the result.

Does it mean that the number stored in 'a'th place in foo becomes the place in array foo of the result of calculation on the right side ?
Here's what I understood :
assuming 'a' is 3, foo[a] is the numeric value stored in forth block in array foo.
assuming that foo is :

int foo [] = { 10, 20, 30, 40, ... }

foo[a] equals 40, and foo[foo[a]] equals foo[40], thus 410.

-----------------------------------------------------------------------------------------------------
I tried to store the value -50 in 'a' of type char, which is between -127 and +127.
but it failed. is there some specific way or rule to store numeric value in character literal?
Last edited on
foo[a] equals 40, and foo[foo[a]] equals foo[40], thus 410.

Yes, assuming all the values of foo increment by 10.

I tried to store the value -50 in 'a' of type char, which is between -127 and +127.
but it failed.

How did it fail? Compile error? run-time crash? Or? Please be specific and please show your code. foo[-50] is not going to be a valid reference and should cause an illegal reference trap.

Last edited on
I guess it failed because 'a' is of type char, and integer values can be stored only in int variables.

I use visual studio so it just doesn't show up console window when it fails. so I have no idea why or how it failed, though some complicated messages helpful to figure out appears down side.
I guess it failed because 'a' is of type char, and integer values can be stored only in int variables.

Not true. Integer values can be stored in char variables. The following is perfectly valid:
1
2
  char a;
  a = 10;


As I noted before, there is a difference between a and 'a'. a is a variable, 'a' is character literal.
Please don't enclose a in single quotes if you're referring to a variable named a. It causes confusion.

I use visual studio so it just doesn't show up console window when it fails. so I have no idea why or how it failed, though some complicated messages helpful to figure out appears down side.

Visual Studio is very good about catching errors and showing you where they are. You just have to learn how to read those messages.

As I requested before, if you're still having problems, please post your code and any error messages VS is displaying.

I have settled the problem, the problem was I added single quotes on both side of a.

Thank you very much for your kind and detailed explain : )

I understood because of you what I was confused about arrays and the difference between character literal ( which is constant) and variable a. ( the latter one is something I didn't even realized I was confused)
Topic archived. No new replies allowed.