FOREACH ARRAY

HAY PEOPLE,

I HAVE A TINY PROBLEM WITH AN ARRAY.
THIS IS THE PROBLEM:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
#include <string.h>
int a;
char b;
__int8 string1[100] = "ŽIVELA JUGOSLAVIJA!";
void subfunction(__int8 string1[]);
void main(void)
{
	a = 0;
	for each (char b in string1)
	{
		printf("%c", string1[a]);
		a++;
	}
	subfunction(string1);
}
void subfunction(__int8 string1[])
{	
	a = 0;
	for each (char b in string1)
	{
		printf("%c", string1[a]);
                a++;
	}
	for (a = 0; a < strlen(string1); a++)
	{
		printf("%c", string1[a]);
	}
}


THE OUTPUT OF THIS PROGRAM IS:
 
Error	1	error C3285: for each statement cannot operate on variables of type 'char []'	C:\ZADACI\ZADATAK\PRVI.cpp	20	1	ZADATAK


THE PROGRAM WILL NOT COMPILE.
I DO NOT KNOW WHY.
I AM TRYING TO PRINT ONE BY ONE LETTER OF A STRING IN A FUNCTION, BUT IT IS NOT WORKING.
IF I TRY IN MAIN FUNCTION, IT WORKS FINE.
IF A PASS A STRING IN THE SUB FUNCTION, IT WORKS FINE AS LONG AS I USE FOR LOOP.
HOWEVER, IF I USE FOREACH LOOP IN THE SUBFUNCTION, IT DOES NOT WORK.

BUT, THE SAME FOR EACH LOOP WORKED INSIDE MAIN FUNCTION.

HOW COME THAT THE SAME LOOP WORKS IN MAIN, BUT DOES NOT WORK IN A FUNCTION CALLED FROM MAIN?

THANXY
Last edited on
First. Do not shout. (All capital text is considered shouting and thus rude.)

Second
for each
This non-standard keyword is available in MS Visual Studio. However, its use is not recommended. Consider using a standard Range-based for Statement (C++) instead.


Third. It is not the same loop.
The loop in your main() does refer to the global array, whose size is known.
The loop in subfunction() does refer to the function's parameter, which is not a real array and thus lacks size information.
Last edited on
1.) Please. Stop yelling.

2.) Also, please put an empty line between functions; it makes it a lot easier to read.

3.) It is because the parameter uses the same identifier (name) as the array value. Change the parameter's name, and it might work.

4.) the main() should ALWAYS return an integer. Usually it should be 0 if everything goes right.

5.) A lot of your code is for VS only. You should never use such code.

6.) I would recommend using the tutorial series rather found here or learncpp.com
The code used for both are fairly OS-Independent. You should never start your learning journey with OS-Dependent keywords.
Last edited on
I AM NOT SHOUTING. ;)
THIS IS JUST MY STYLE OF WRITING. :D
I ALLWAYS USE CAPITAL LETTERS.

OKEY.

OH, SO ONLY AN ARRAY DEFINED INSIDE A FUNCTION CAN BE USED IN THAT FUNCTION WITH FOR EACH LOOP.
OR A GLOBAL ARRAY.
BUT NOT THE ONE PASSED FROM PARAMETERS.
I SEE.

THANX MAN.
1. NO, NO, IT IS NOT YELLING. I AM REALLY CALM AND HAPPY AT THE MOMENT, TRUST ME.
2. OKEY.
3. OKEY. IT WORKS ONLY IF GLOBAL ARRAY IS USED INSIDE A FUNCTION. BUT THEN, THERE IS NO POINT OF PASSING IT AS A PARRAMETER TO THE FUNCTION.
4. I KNOW, BUT, I USE RETURNS ONLY WHEN I NEED TO RETURN SOMETHING TO PARRENT PROGRAM.
I AM USUALLY WRITING PROGRAMS IN CMD LANGUAGE (SCRIPTS), AND IN POWERSHELL LANGUAGE, SOMETIMES IN NET FRAMEWORK.
BUT SOMETIMES, JUST SOMETIMES I USE C (AND OTHER LANGUAGES WHICH NEED TO BE COMPLILED), TO DO SOME JOB WHICH I CAN NOT DO FOR EXAMPLE INSIDE CMD LANGUAGE.
AND THEN I WRITE A FUNCTION I C, AND CALL IT FROM CMD SCRIPT.
IF THE FUNCTION NEEDS TO RETURN SOME VALUE TO THE PARRENT, AND THAT IS CMD BATCH SCRIPT, SO THAT I CAN USE THAT VALUE INSIDE CMD SCRIPT, THEN I USE INT MAIN OR BOOL MAIN. BUT IF THE FUNCTION DOES NOT NEED TO RETURN ANYTHING IMPORTANT TO THE PARRENT PROGRAM, THEN, I DO NOT USE IT. I JUST USE VOID MAIN. BUT, I THINK THAT IT IS NOT IMPORTANT TO DOS WETHER IT RETURNS SOMETHING OR NOT, BECAUSE USUALLY, THE PROGRAMS I WRITE IN C (OR OTHER LANGUAGES LIKE C#, QBASIC...) AFTER THEY FINISH EXECUTION RETURN TO PARRENT PROGRAM, AND THAT IS CMD SCRIPT. SINCE CMD PROGRAM DOES NOT HAVE TO RETURN ANYTHING, IT IS OKEY. CMD.EXE BY ITSELF IS A PROGRAM WHICH COMUNICATE WITH WINDOWS, AND RETURNS A VALUE WHEN I EXIT CMD. SO, WHY SHOULD WINDOWS CARE FOR DOS-LIKE APPLICATION IF IT RETURNS SOMETHING OR NOT, BECAUSE IT IS RUN INSIDE CMD APPLICATION.
SO, I DO NOT RETURN VALUES UNLESS THEY ARE MEANINGLESS.
AND IF I RETURN A VALUES FROM C APPLICATION, THAT IS RARELY 0, IT IS USUALLY SOME OTHER NUMBER. FOR EXAMPLE IF I WRITE A MENU APPLICATION WHICH HAS A GRAPHIC MENUES WHEWRE USER COULD CHOOSE AN OPTION, THIS APPLICATION WOULD RETURN TO CMD THE OFFSED OF CHOSEN MENU OPTION. AND CMD SCRIPT WILL USE IT TO PERFORM A JOB USER SELECTED IN THAT MENU. IN THOSE SITUATIONS IT IS REALLY RARELY 0, THE RETURN VALUE.

5. I KNOW, BUT I AM ONLY MICROSOFT USER, ID EST, I USE ONLY MS-DOS AND WINDOWS OPERATING SYSTEMS.
SO, I AM NOT PLANNING TO PROGRAM ANYTHING FOR ANY OTHER OPPERATING SYSTEM EXCEPT MS-DOS AND WINDOWS. SO IT IS NOT SUCH A BIG PROBLEM FOR ME. AT LEAST NOT FOR NOW.
BUT YES, I KNOW, YOU ARE RIGHT.

6. YES, I KNOW.

THANXY. ;)
All Caps = Yelling on the Internet.

Good day...
Topic archived. No new replies allowed.