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);
Programa IRPF - Guia de Instalação e Resolução de alguns Problemas
Criando uma Infraestrutura para uma micro Empresa
Criar entrada (menuentry) ISO no Grub
Como gerar qualquer emoji ou símbolo unicode a partir do seu teclado
Instalando o Pi-Hole versão v5.18.4 depois do lançamento da versão v6.0
Instalar o VIM 9.1 no Debian 12
Como saber o range de um IP público?
Muitas dificuldades ao instalar distro Linux em Notebook Sony Vaio PCG-6131L (VPCEA24FM)
O FIM da minha distro predileta: ARCOLINUX ...que pena (0)
problema para barrar sites/downloas via HTTPS. (4)
Não acesso a conta do Banco do Brasil pelo Google Chrome (0)