Convert structure to string

0

How can I convert an integer to string? Example: convert int cod to char cod[30] .

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <windows.h>



struct Tipo_Lista{
    int cod;
    struct Tipo_Lista *Prox;
};

struct Tipo_Lista *Primeiro;
struct Tipo_Lista *Ultimo;

void FlVazia(){
    struct Tipo_Lista *aux;
    aux = (struct Tipo_Lista*)malloc(sizeof(struct Tipo_Lista));
    Primeiro = aux;
    Ultimo = Primeiro;
}

int Insere(int x){
    struct Tipo_Lista *aux;
    aux = (struct Tipo_Lista*)malloc(sizeof(struct Tipo_Lista));
    aux->cod = x;
    Ultimo->Prox = aux;
    Ultimo = Ultimo->Prox;
    aux->Prox = NULL;
}

void Imprime(){
    struct Tipo_Lista *aux;
    aux = Primeiro->Prox;
    while(aux !=NULL){
        printf("\n\nItem = %d", aux->cod);
        aux = aux->Prox;
    }
}
    
asked by anonymous 04.11.2018 / 17:26

1 answer

1

If you have an integer and want to transform it into a string, you can use the sprintf function that does the opposite of atoi .

The sprintf

int sprintf(char *str, const char *format, ...);

creates a string with the same content to be printed if format was used in printf , but instead of printing, the content is stored in a buffer pointed to by str .

The buffer pointed to by str must be large enough to store the resulting string.

Read more about this feature here .

That is, what you want to do is:

int numero = 20180815;
char palavra[100];
sprintf(palavra, "%d", numero);
// Agora, palavra tem como conteudo "20180815".
    
04.11.2018 / 20:31