Enviado em 27/12/2014 - 13:15h
Ola pessoal..#include <stdlib.h> #include <stdio.h> #include <string.h> struct Test{ char *nome; int x; int y; }; int main(){ struct Test t01; t01.nome="daniell"; t01.x=5; t01.y=9; char c = t01.x + '0'; char d = t01.y + '0'; unsigned int tam = strlen(t01.nome)+sizeof(t01.x)+sizeof(t01.y)+1; printf("tam: %u\n",tam); char test[tam]; printf("len test: %zu\n",sizeof(test)); test[strlen(test)] = c; test[strlen(test)] = d; strcat(test,t01.nome); printf("%s\n",test); printf("%d\n",strlen(test)); return 0; }
Enviado em 28/12/2014 - 00:04h
Acima eu disse o mais simples. Agora deixe eu mostrar o problema no seu programa.char test[tam];
Enviado em 27/12/2014 - 17:14h
#include <stdlib.h> #include <stdio.h> #include <string.h> struct Test{ char *nome; int x; int y; }; int main(){ struct Test t01; t01.nome="daniell"; t01.x=5; t01.y=9; char c = t01.x + '0'; char d = t01.y + '0'; unsigned int tam = strlen(t01.nome)+sizeof(t01.x)+sizeof(t01.y)+1; printf("tam: %u\n",tam); char test[tam]; printf("len test: %zu\n",sizeof(test)); test[strlen(test)] = c; test[strlen(test)] = d; strcat(test,t01.nome); printf("%s\n",test); printf("%d\n",strlen(test)); return 0; }
Enviado em 27/12/2014 - 17:40h
sim sim.. vc ta certo o vetor so armazena 1 tipo de dado..Enviado em 27/12/2014 - 23:51h
Acho que a maneira mais simples (e portável) de fazer o que você quer é com snprintf() (não se guie só pelo exemplo: leia a documentação!!).int len; char *str; /* Primeiro, uma rodada sem gravar, só para ver o tamanho. */ len=1+snprintf(NULL, 0, "%s%d%d\n", t01.nome, t01.x, t01.y); /* Aloca os caracteres contados acima (mais byte nulo). */ if((str=malloc(len))==NULL){ perror("malloc()"); exit(1); } /* Agora, sim, preenche o conteúdo. */ snprintf(str, len, "%s%d%d", t01.nome, t01.x, t01.y); /* ... Usa str. ... */ /* Libera str. */ free(str);
char *str; if(asprintf(&str, "%s%d%d", t01.nome, t01.x, t01.y)==-1){ perror("asprintf()"); exit(1); } /* ... Usa str. ... */ /* Libera str. */ free(str);
Enviado em 28/12/2014 - 00:42h
char test[tam];
Enviado em 28/12/2014 - 02:52h
Você não respondeu exatamente, mas falou em "mesmo ele estando vazio".char test[size]; memset(test, 0, size);
Enviado em 28/12/2014 - 08:25h
char test[size]; memset(test, 0, size);
Resolver problemas de Internet
Como compartilhar a tela do Ubuntu com uma Smart TV (LG, Samsung, etc.)
Descritores de Arquivos e Swappiness
Fez porcaria no teu repositório Git? Aprenda a restaurar uma versão anterior do seu código!
Restaurando Fontes de Download do Hydra no Linux
Atualizando "na marra" o YT-DLP quando começa a dar erro de downloads
Como instalar o WPS com interface e corretor ortográfico em PT-BR no Arch Linux
Controle do PS5 no Linux Mint funciona? [RESOLVIDO] (5)
Bluetooth CSR 4.0 Dongle (Alternativo) (0)
Atualização do Linux para 21 "Vanessa... (3)