:(i got a bit blur in the topic array..where can i find some tutorial on it??? and can a single index in array store more than 1 number or store a string more than 1 alphabet???:(
About Array...
Started by stephen0606, Oct 15 2007 09:32 PM
5 replies to this topic
#1
Posted 15 October 2007 - 09:32 PM
|
|
|
#2
Posted 16 October 2007 - 12:28 AM
Ok im not an expert but ill try to help by telling you how i understand this :P
one index in a array can hold one number like 1 or 11 or 111 or....
one string can hold one single ASCII char like A or b or ? or anything
(check American Standard Code for Information Interchange - Wikipedia, the free encyclopedia for a complete list of ASCII and a explanation)
Just like to add that i might be a bit off or maybe alot off so dont trust me to mutch :P
one index in a array can hold one number like 1 or 11 or 111 or....
one string can hold one single ASCII char like A or b or ? or anything
(check American Standard Code for Information Interchange - Wikipedia, the free encyclopedia for a complete list of ASCII and a explanation)
Just like to add that i might be a bit off or maybe alot off so dont trust me to mutch :P
#3
Posted 16 October 2007 - 06:43 AM
"stephen0606" said:
and can a single index in array store more than 1 number or store a string more than 1 alphabet???
It's not possible to store more than one value on a single address, but it's possible to store a pointer on a single address, and let it point on other addresses, which will work like they were at the same address. To do this, you could use structures, classes, or something alike.
struct RandomStructure
{
int OneValue;
int AnotherValue;
};
// ...
RandomStructure rs1 = {10, 20};
RandomStructure rs2 = {30, 40};
RandomStructure array[] = {rs1, rs2};
for(int i = 0; i < sizeof(array) / sizeof(RandomStructure); i++)
{
std::cout << "array[" << i << "] : OneValue : " << array[i].OneValue << std::endl;
std::cout << "array[" << i << "] : AnotherValue : " << array[i].AnotherValue << std::endl;
std::cout << "array[" << i << "] : Address : " << std::hex << &array[i] << std::endl;
std::cout << std::endl;
}
As you see, we're only using two indexes (0, 1,) but are getting four values (10, 20, 30, 40.)
#4
Posted 16 October 2007 - 06:58 AM
Similar to above*. You can store an array of character arrays in C, essentially storing a string in each index as opposed to each element being a character. I'm not sure if that is what the OP wanted. In any case, to do this you want something like
*difference being your storing pointers to arrays (or pointers to pointers depending how you look at it) as opposed to a pointers to a struct.
char *string[] = {"True", "False"};
printf("%s\n", string[0]);
*difference being your storing pointers to arrays (or pointers to pointers depending how you look at it) as opposed to a pointers to a struct.
#5
Posted 18 October 2007 - 12:43 AM
i have 1 more problem...which is when in a seperated function..within dat function i assign an array with character like Grade[0]is H, Grade[1] is L,but when i display back in the main function i found dat the value change..not H and L anymore..why?? Izit char cannot pass through function??? but my other array which is int can display properly in main onli char cannot...Help!!
#6
Posted 22 October 2007 - 09:49 PM
stephen0606 said:
within dat function i assign an array with character like Grade[0]is H, Grade[1] is L,but when i display back in the main function i found dat the value change..not H and L anymore..why??
void DoStuffWithGradeChar(char* arrayPtr)
rather than
void DoStuffWithGradeChar(char arrayVal)


Sign In
Create Account

Back to top









