nunolevezinho Posted June 5, 2013 at 08:51 PM Report #511234 Posted June 5, 2013 at 08:51 PM (edited) Boas, estou a ter dificuldades em fazer uma função se aceite argumentos default. Por ex: DumpData(int a, int b, int c = 0); Onde o argumento c, se não for prrenchido é automaticamente dado como sendo 0. Sei que em php isto dá para fazer. Em C, não estou a conseguir. Será que não existe ou estarei a mandar alguma cabeçada na sintaxe ? int DumpData(int a, int b, int c=0) { if(c != 0) { //stuff } //banana } /* Chamada de Função */ DumpData(1, 2); Edited June 5, 2013 at 08:58 PM by nunolevezinho
pikax Posted June 5, 2013 at 08:57 PM Report #511237 Posted June 5, 2013 at 08:57 PM fata-te o tipo de b.... int DumpData(int a,int b, int c=0) Por muito mais que que estude só aprendo uma coisa, que ainda tenho muita coisa para aprender. A beleza de um código está em decompor problemas complexos em pequenos blocos simples. "learn how to do it manually first, then use the wizzy tool to save time." "Kill the baby, don't be afraid of starting all over again. Fail soon, learn fast."
nunolevezinho Posted June 5, 2013 at 08:59 PM Author Report #511238 Posted June 5, 2013 at 08:59 PM fata-te o tipo de b.... int DumpData(int a,int b, int c=0) My bad, copiei mal. Tem o int definido no código. Não é disso :/
pikax Posted June 5, 2013 at 09:02 PM Report #511241 Posted June 5, 2013 at 09:02 PM tem que funcionar, qual e' o erro que esta' a dar? Por muito mais que que estude só aprendo uma coisa, que ainda tenho muita coisa para aprender. A beleza de um código está em decompor problemas complexos em pequenos blocos simples. "learn how to do it manually first, then use the wizzy tool to save time." "Kill the baby, don't be afraid of starting all over again. Fail soon, learn fast."
Rui Carlos Posted June 5, 2013 at 09:05 PM Report #511242 Posted June 5, 2013 at 09:05 PM O C já tem suporte para isso? Estava na ideia que só o C++ é que tinha algo do género. Rui Carlos Gonçalves
nunolevezinho Posted June 5, 2013 at 09:12 PM Author Report #511243 Posted June 5, 2013 at 09:12 PM Coloquei a dúvida no StackOverflow e pelo que me disseram o C não tem suporte para isto ( too bad :/ ), apenas C++ tem. Mas deram-me uma alternativa: int DumpData(int a, int b) { return DumpDataABC(a, b, 0); } int DumpDataABC(int a, int b, int c) { //stuff }
HappyHippyHippo Posted June 5, 2013 at 09:13 PM Report #511244 Posted June 5, 2013 at 09:13 PM em C, esse tipo de construção não existe. existe uma solução intermédia que envolve o uso de variadic functions, mas nunca será a mesma coisa, pois existe a limitação de nunca saberes se foi dado algum valor extra e quantos : #include <stdarg.h> int foo(int a, int b, ...) { int c = 0; // <--- valor por defeito va_list args; va_start(args, b); // ler os valores extra com va_arg // mas não existe uma maneira directa de saber quantos existem !!! va_end(args); return 0; } int main(void) { foo(1, 2); // <-- chamada válida foo(1, 2, 3); // <-- chamada válida foo(1, 2, 3, NULL); // <-- chamada válida foo(1, 2, 3, NULL, "ping"); // <-- chamada válida foo(1, 2, 3, NULL, "ping", 3.234); // <-- chamada válida return 0; } o meu concelho para o teu caso é usar macros (obviamente estás a popular o mar de entities, mas não tens outra solução simples) : #define foo_simplify(a,b) (foo(a,b,0)) int foo(int a, int b, int c) { // do stuff return 0; } int main(void) { foo(1, 2, 3); // a = 1, b = 2, c = 3 foo_simplify(1, 2); // a = 1, b = 2, c = 0 } IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
pikax Posted June 5, 2013 at 09:15 PM Report #511245 Posted June 5, 2013 at 09:15 PM My Bad... ja' estou enferrujado em C... Por muito mais que que estude só aprendo uma coisa, que ainda tenho muita coisa para aprender. A beleza de um código está em decompor problemas complexos em pequenos blocos simples. "learn how to do it manually first, then use the wizzy tool to save time." "Kill the baby, don't be afraid of starting all over again. Fail soon, learn fast."
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now