Skip to content

Instantly share code, notes, and snippets.

@tiagohm
Created February 19, 2018 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tiagohm/848a86c10f12a6effd3459499b3ab77b to your computer and use it in GitHub Desktop.
Save tiagohm/848a86c10f12a6effd3459499b3ab77b to your computer and use it in GitHub Desktop.
//a memória para os parâmetros será alocada dessa forma: [n][p0][p1][...][pn]
//por isso usamos o endereço de n para obter o endereço dos outros parametros.
unsigned calcular_media(char n, ... ) {
char i;
unsigned res = 0;
for(i = 1; i <= n; i++) {
res += ((char*)&n)[i]; //aponta para o i-esimo parametro e recupera seu valor.
}
return (res / n);
}
void main() {
//calcula a media aritmetica dos 10 números.
unsigned media = calcular_media(10, 11, 3, 4, 5, 6, 6, 8, 7, 6, 14);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment