Global variables sent to a function
Ok I wanna create a program that displays some numbers. I don't wanna use pointers for a reason but its kinda complicated. Anyway heres my code can anyone tell me a way to make it work?
Working version(modified just to work with one struct):
Code:
#include <stdio.h>
#include <conio.h>
typedef struct
{
int x[3];
int y;
int total;
}Data;
void Display(Data tempx);
Data numbers;
int main()
{
numbers.total=3;
numbers.y=0;
numbers.x[0]=5;
numbers.x[1]=3;
numbers.x[2]=1;
while(1)
Display(numbers);
getch();
}
void Display(Data tempx)
{
if(numbers.y>=numbers.total)
{
numbers.y=0;
}
else{
printf("%d \n", numbers.x[numbers.y]);
numbers.y++;
getch();
}
}
Not Working:
Code:
#include <stdio.h>
#include <conio.h>
typedef struct
{
int x[3];
int y;
int total;
}Data;
void Display(Data tempx);
Data ;
int main()
{
numbers.total=3;
numbers.y=0;
numbers.x[0]=5;
numbers.x[1]=3;
numbers.x[2]=1;
while(1)
Display(numbers);
getch();
}
void Display(Data tempx)
{
if(tempx.y>=tempx.total)
{
tempx.y=0;
}
else{
printf("%d \n", tempx.x[tempx.y]);
tempx.y++;
getch();
}
}
Can anyone make the non working thing to work so that I can also send other structs to Display. I would prefer not to use pointers if possible.
Also is there anyway to maybe pass by reference instead of using pointers like in C++? Maybe someone knows a library someone came up with to make it work. If you don't know what I mean. I'm talking about something like this:
void Display(Data &tempx);
So that you can call upon it like this:
Display(numbers);