What reason? Pointers are the essence of the C-language. If you don't like to see them, use another language.
What do you mean 'to work'. The parameter 'tempx' is here, but it is not used at all.
(Reformatted, fixed and commeted)
Fixed. While this code is technically correct, it's considered bad practice (and it is very unefficient) to pass a copy of a structure to a functions. Pointers avoid that.
Additionally, the behaviour of the code is not the one you expect (I guess) because you want to alter some data. In that case, the pointer is
not an option.
No way. If you want C++, you know what to do.
Also, don't write infinite loops in your testing code.
I suggest the following:
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int x[3];
int y;
int total;
}
Data;
void Display (Data *this)
{
if (this->y >= this->total)
{
this->y = 0;
}
else
{
printf ("%d \n", this->x[this->y]);
this->y++;
}
}
int main (void)
{
Data numbers;
numbers.total = 3;
numbers.y = 0;
numbers.x[0] = 5;
numbers.x[1] = 3;
numbers.x[2] = 1;
{
int i;
for (i = 0; i < 10; i++)
{
Display (&numbers);
}
}
system ("pause");
return 0;
}
That produces:
Code:
5
3
1
5
3
1
5
3
Appuyez sur une touche pour continuer . . .