Localhost Posted October 24, 2009 at 11:02 AM Report Share #293139 Posted October 24, 2009 at 11:02 AM Olá pessoal, então tudo bem? 😛 Bem, estou numa fase muito complexa do C, os ponteiros, aliás, ponteiros em funções do género fazer funções tipo strcpy etc. Será que alguém me podia ajudar com isto? Estou mesmo aos papéis eheeheh Já tentei fazer a função strcat mas está um bocado difícil... #include <stdio.h> #include <conio.h> void StrCat(char *destino, char *origem) { int i; for(i=0; *origem != '\0'; *origem++) { if(*origem) *destino = *origem; *destino++; } *destino = '\0'; } int main() { char str[1024], str2[1024]; printf("Destino: "); gets(str); printf("Origem: "); gets(str2); StrCat(str, str2); printf("%s" ,str); getch(); return 0; } Isto foi o que fiz mas está mal Abraços 😄 EDIT: Alguém me pode dizer como se põe códigos aqui no forúm é que não consigo, e já pus inserir código © here since 2009 Link to comment Share on other sites More sharing options...
Ferreira Posted October 24, 2009 at 11:38 AM Report Share #293142 Posted October 24, 2009 at 11:38 AM Tens de redimensionar a string de destino (função realloc) para que tenha espaço para a nova string e depois copia-la para o novo espaço. http://twitter.com/ferreira Link to comment Share on other sites More sharing options...
zecapistolas Posted October 24, 2009 at 12:12 PM Report Share #293148 Posted October 24, 2009 at 12:12 PM Na tua função "StrCat" tens que primeiro por o apontador de "destino" a apontar para o fim, porque é no fim de "destino" que tu queres adicionar os caracteres de "origem".... Para isso, a "StrCat" fica assim: void StrCat(char *destino, char *origem) { /* Percorrer a string "destino" até ao final */ do { *destino++; } while (*destino != '\0'); /* Percorrer a string "origem" até ao final e vai acrescentando a "destino" sempre no final da mesma */ do { *destino++ = *origem++; } while (*origem != '\0'); *destino = '\0'; } No mais invés de declarares as string's logo com [1024] acho melhor fazeres um "malloc" ou "calloc" e reservares espaço para a variável.... Outra coisa, nunca utilizes "gets", como diz no na Wiki aqui do fórum, "O gets() é uma função manhosa, devido ao possivel buffer overflow".... Qualquer dúvida, apita.... cumps 😛 Link to comment Share on other sites More sharing options...
Baderous Posted October 24, 2009 at 01:11 PM Report Share #293155 Posted October 24, 2009 at 01:11 PM Podes fazer simplesmente: char * strCat(char *dest,char *orig) { strcpy(dest+strlen(dest),orig); return dest; } Link to comment Share on other sites More sharing options...
zecapistolas Posted October 24, 2009 at 02:32 PM Report Share #293171 Posted October 24, 2009 at 02:32 PM Podes fazer simplesmente: char * strCat(char *dest,char *orig) { strcpy(dest+strlen(dest),orig); return dest; } É uma solução, mas acho que ele quer mesmo fazer a função dele, não usar cenas extra.... Para aprender a mexer com apontadores.... cumps 😛 Link to comment Share on other sites More sharing options...
Localhost Posted October 24, 2009 at 03:26 PM Author Report Share #293180 Posted October 24, 2009 at 03:26 PM Pessoal, vocês são os maiores, sempre prontos para ajudar, Baderous, eu queria mesmo fazer a minha própria função, como o zecapistolas disse mas mais uma vez obrigado a todos! 😛 Mas só mais uma dúvida, como se mete códigos aqui xD? here since 2009 Link to comment Share on other sites More sharing options...
bruno1234 Posted October 24, 2009 at 03:34 PM Report Share #293183 Posted October 24, 2009 at 03:34 PM com as tags: '['code=C']' ... '['/code']' Sem as ' claro. Matraquilhos para Android. Gratuito na Play Store. https://play.google.com/store/apps/details?id=pt.bca.matraquilhos Link to comment Share on other sites More sharing options...
Localhost Posted October 24, 2009 at 03:39 PM Author Report Share #293184 Posted October 24, 2009 at 03:39 PM Já está, obrigado miguel 😛 here since 2009 Link to comment Share on other sites More sharing options...
Localhost Posted October 24, 2009 at 04:01 PM Author Report Share #293187 Posted October 24, 2009 at 04:01 PM Alguém sabe onde é que se arranja aquela cena que o zecapistolas tem na assinatura? here since 2009 Link to comment Share on other sites More sharing options...
Baderous Posted October 24, 2009 at 04:09 PM Report Share #293188 Posted October 24, 2009 at 04:09 PM Chama-se "userbar". Mas já é off-topic. Link to comment Share on other sites More sharing options...
Localhost Posted October 24, 2009 at 05:00 PM Author Report Share #293195 Posted October 24, 2009 at 05:00 PM Eheheh, obrigado. here since 2009 Link to comment Share on other sites More sharing options...
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