Jump to content

About Array...

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
stephen0606

stephen0606

    Newbie

  • Members
  • Pip
  • 8 posts
:(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???:(

#2
Nille

Nille

    Learning Programmer

  • Members
  • PipPipPip
  • 41 posts
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

#3
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts

"stephen0606" said:

and can a single index in array store more than 1 number or store a string more than 1 alphabet???
In theoretical sense, no. In practically sense, yes.

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
G_Morgan

G_Morgan

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 537 posts
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

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
stephen0606

stephen0606

    Newbie

  • Members
  • Pip
  • 8 posts
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
limo

limo

    Newbie

  • Members
  • PipPip
  • 14 posts

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??
You're passing in the array by value. You need to pass it by reference or use pointers.

void DoStuffWithGradeChar(char* arrayPtr)

rather than

void DoStuffWithGradeChar(char arrayVal)