B
bpascal123
Hi,
I first thanks people who answered earlier posts. As I have started
learning c since 3-4 months, I can only deal with simple things but I
like to understand as much as i can and when i don't find something
coherent in my learning process I try to understand why.
Below is a code from a tutorial application that doesn't work as it
should when i'm able to deal well with the next tutorial application
supposed to be more difficult (you can find its code in the bottom of
this post).
The subject here as i'm doing my best to explain, is to send a
function parameter to main or wherever it is declared without using
"return sthg" . That is to say, the function is of a void cast : like
" void Sthg(*a, b) ; " ... The tutorial gives examples of this by
using pointers.
APPLICATION 1 - basic void function (not working ) :
#include <stdio.h>
void EntRe(int *nbr)
{
printf("\n Enter integer : \n") ;
scanf("%d", nbr) ;
}
int main(void)
{
void EntRe(int *nbr) ;
int a ;
printf("\nYou've entered : %d\n", EntRe(&a) ) ;
}
Compiler message : text14.c:14: error: invalid use of void expression
===================
Application 2 : Same "recipe" but it's working :
===================
#include <stdio.h>
#define NMAX 10
int nmax2 = 0 ;
int main(void)
{
void MaxMin(int n, int *Tab, int *max, int *min) ;
int i, cnt = 0 ;
int op = 0 ;
int rt, tr ;
int Tub[NMAX] ;
for ( i = 0 ; i < NMAX ; i++ )
{
Tub = op * 10 / 8 + 6 ;
op = Tub ;
printf("\nValeur %2d : %4d\n", cnt++, Tub ) ;
nmax2 = i ;
}
MaxMin(nmax2, Tub, &rt, &tr) ;
printf("\nIl y a %d valeurs. \n", nmax2) ;
printf("\nLa valeur min est %d .\n", tr) ;
printf("\nLa valeur max est %d .\n", rt) ;
}
void MaxMin(int n, int *Tab, int *max, int *min)
{
*max = *Tab ;
*min = *Tab ;
int i ;
for ( i = 0 ; i <= nmax2 ; i++ )
{
if ( *(Tab+i) > *max )
*max = *(Tab+i) ;
if ( *(Tab+i) < *min )
*min = *(Tab+i) ;
}
}
I first thanks people who answered earlier posts. As I have started
learning c since 3-4 months, I can only deal with simple things but I
like to understand as much as i can and when i don't find something
coherent in my learning process I try to understand why.
Below is a code from a tutorial application that doesn't work as it
should when i'm able to deal well with the next tutorial application
supposed to be more difficult (you can find its code in the bottom of
this post).
The subject here as i'm doing my best to explain, is to send a
function parameter to main or wherever it is declared without using
"return sthg" . That is to say, the function is of a void cast : like
" void Sthg(*a, b) ; " ... The tutorial gives examples of this by
using pointers.
APPLICATION 1 - basic void function (not working ) :
#include <stdio.h>
void EntRe(int *nbr)
{
printf("\n Enter integer : \n") ;
scanf("%d", nbr) ;
}
int main(void)
{
void EntRe(int *nbr) ;
int a ;
printf("\nYou've entered : %d\n", EntRe(&a) ) ;
}
Compiler message : text14.c:14: error: invalid use of void expression
===================
Application 2 : Same "recipe" but it's working :
===================
#include <stdio.h>
#define NMAX 10
int nmax2 = 0 ;
int main(void)
{
void MaxMin(int n, int *Tab, int *max, int *min) ;
int i, cnt = 0 ;
int op = 0 ;
int rt, tr ;
int Tub[NMAX] ;
for ( i = 0 ; i < NMAX ; i++ )
{
Tub = op * 10 / 8 + 6 ;
op = Tub ;
printf("\nValeur %2d : %4d\n", cnt++, Tub ) ;
nmax2 = i ;
}
MaxMin(nmax2, Tub, &rt, &tr) ;
printf("\nIl y a %d valeurs. \n", nmax2) ;
printf("\nLa valeur min est %d .\n", tr) ;
printf("\nLa valeur max est %d .\n", rt) ;
}
void MaxMin(int n, int *Tab, int *max, int *min)
{
*max = *Tab ;
*min = *Tab ;
int i ;
for ( i = 0 ; i <= nmax2 ; i++ )
{
if ( *(Tab+i) > *max )
*max = *(Tab+i) ;
if ( *(Tab+i) < *min )
*min = *(Tab+i) ;
}
}