Re: display module needs fixing
From: Pascal Bourguignon (spam_at_thalassa.informatimago.com)
Date: 10/27/03
- Next message: André Pönitz: "Re: display module needs fixing"
- Previous message: Villy Kruse: "Re: mv within the same filesystem, inode change allowed?"
- In reply to: ehab: "display module needs fixing"
- Next in thread: André Pönitz: "Re: display module needs fixing"
- Reply: André Pönitz: "Re: display module needs fixing"
- Reply: Irrwahn Grausewitz: "Re: display module needs fixing"
- Reply: Floyd Davidson: "Re: display module needs fixing"
- Reply: LeEnEx: "Re: display module needs fixing"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 27 Oct 2003 09:56:17 +0100
hpy_azizy@juno.com (ehab) writes:
> Please try to help me give me direct syntz to fix my display module
> down below The display module does no give me right answers of the
> previous array .
> float array[50];
> input_table(&array[elements]);
In C, the arrays are represented as a pointer to the first element.
That means that:
float array[50];
is equivalent to:
float* array;
and array[elements] is of type float
and &(array[elements]) is of type float*
(with your young mind, you'll probably be able to remember the complex
operator precendence rules of C, but I can't remember them, so I use
always parenthesis to be sure).
So, with your function call:
input_table(&array[elements]);
you're giving input_table an address, that of the element number
`elements' in the array `array'.
> input_table(array_e)
> float array_e;
> {
> printf ("Enter an array element : ");
> scanf("%f",&array_e);
> }
You could write it in ANSI C as:
void input_table(float array_e)
{
printf ("Enter an array element : ");
scanf("%f",&array_e);
}
In anycase, here, input_table expects a float. Since main did push on
the stack a pointer, the bit pattern of a pointer will be used as the
value of the float array_e. Needless to say that the value will be
"random" if at all valid. But it does not even matter, since the only
thing that is done by the function to array_e, is to take it's address
(the address of the parameter that has been pushed on the stack), and
give it to scanf. So scanf stores at this address, the value of the
floating-point number read. Then input_table returns, and the stack
is freed, and nothing has been done to array.
In C, there is only one way to pass parameters: BY VALUE. That is,
only INPUT parameters can be specified. You cannot have OUTPUT
parameters (like VAR in pascal) or INOUT parameters.
To let a function modify data outside of its local environment, you
must pass a POINTER to the data that you want it to modify.
void input_table(float* element)
{
//...
scanf("%f",element); // and of course, since we have already a
// pointer, we can pass it directly to scanf.
}
Note that your functions DO NOT DO what they say. input_table only
inputs ONE elements, and display_table only prints ONE elements. So
you should call them input_element and display_element. And once
you're working on single elements, it does not matter if they come
from an array or from some other place, so you could as well call
them: input_float and display_float:
void input_float(float* value)
{
//...
scanf("%f",value);
}
void display_float(float value)
{
printf"... %f ...\n",value);
}
Then you could encapsulate your loops into the true input_table and
display_table:
int input_table(float* table,int max_elem)
// it should prove usefull to return the number of elements read.
{
char answer='y';
int index=0;
while((rep=='y')&&(index<max_elem)){
input_float(&(table[index++]));
scanf("\n");
printf ("more array elements (y/n) : ");
scanf("%c",&answer);
}
return(index);
}
void display_table(float* table,int num_elem)
{
int index=0;
while(index<num_elem){
printf ("The element no %d = %f\n",index,table[index]);
}
}
Then your main() would be greatly simplified:
int main(void)
{
float table[50];
int num_elem;
// printf...
num_elem=input_table(table,sizeof(table)/sizeof(*table));
// printf...
display_table(table,num_elem);
return(0);
}
An alternative of using sizeof to compute the maximum number of
elements would be to use a constant:
#define MAX_ELEM (50)
float table[MAX_ELEM];
num_elem=input_table(table,MAX_ELEM);
But then, you'll note that there is three items related to the table:
- the array containing the elements,
- the allocated size of this array (MAX_ELEM),
- the actual number of elements in the array (num_elem).
To avoid any error, you should keep track of them in one structure:
typedef struct {
float* elements;
int allocated;
int element_count;
} array_t;
Just add two simple functions to allocate and free this structure:
array_t* new_array(int max_elem)
{
array_t* table=malloc(sizeof(*table));
table->elements=malloc(sizeof(float)*max_elem);
table->allocated=max_elem;
table->element_count=0;
return(table);
}
void free_array(array_t* table)
{
free(table->elements);
free(table);
}
and your program becomes even simplier with no risk of error when you
start to handle several arrays of different sizes:
void input_table(array_t* table)
{
char answer='y';
table->element_count=0;
while((rep=='y')&&(table->element_count<table->allocated)){
input_float(&(table->elements[table->element_count++]));
scanf("\n");
printf ("more array elements (y/n) : ");
scanf("%c",&answer);
}
}
void display_table(array_t* table)
{
int index=0;
while(index<table->element_count){
printf ("The element no %d = %f\n",index,table->elements[index]);
}
}
int main(void)
{
array_t* table=new_array(50);
// printf...
input_table(table);
// printf...
display_table(table);
return(0);
}
-- __Pascal_Bourguignon__ http://www.informatimago.com/
- Next message: André Pönitz: "Re: display module needs fixing"
- Previous message: Villy Kruse: "Re: mv within the same filesystem, inode change allowed?"
- In reply to: ehab: "display module needs fixing"
- Next in thread: André Pönitz: "Re: display module needs fixing"
- Reply: André Pönitz: "Re: display module needs fixing"
- Reply: Irrwahn Grausewitz: "Re: display module needs fixing"
- Reply: Floyd Davidson: "Re: display module needs fixing"
- Reply: LeEnEx: "Re: display module needs fixing"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|