3

I am having a structure:

struct K 
{
  char a[10];
  char b[10];
};

I wish to convert this structure to a char* pointer and print the value on Uart. Uart takes char* pointer as input.

My main function looks like:

void main()
{
    struct K x= { "Hello","Pollo"};
    struct K *revert;
    char *buffer;
    buffer = (char *)&x;
    revert = (struct K *) buffer;
    printf("%s %s", revert->a,revert->b);
}

Note: printf() won't work, I am using a UART.

I want to print the buffer value on UART when it is done with converting structure pointer to char * pointer. Is it possible to do that?

Jens
  • 69,818
  • 15
  • 125
  • 179
Pradeep Ch
  • 103
  • 3
  • 11
  • 2
    Please do not use `void main`. Take one of the entry points defined in the standard. – nvoigt Aug 10 '15 at 07:02
  • 1
    @nvoigt You statement is self-contradictory. Standard defines that entry point can be implementation defined. – this Aug 10 '15 at 07:28
  • @this You realize that "implementation defined" is a bad thing, right? – nvoigt Aug 10 '15 at 11:58
  • @nvoigt You should call Microsoft( and many, many microcontroller companies, which use void main in they compilers ) and tell them that. Maybe they will hire you. On the other hand, they will most likely ignore your ridiculous statements. https://msdn.microsoft.com/en-us/library/windows/desktop/ms633559%28v=vs.85%29.aspx https://duckduckgo.com/?q=c+microcontroller+%22void+main%28%22&t=ffsb #DontBeIgnorant – this Aug 10 '15 at 12:05
  • My ridiculous statements lead to working programs that are cross-platform. You may ignore them, but you won't get cross-platform, standard compliant code. It's your choice if you want to aim for less. – nvoigt Aug 10 '15 at 13:13
  • @nvoigt You seem to live in a self imposed bubble. Your arguments are black-and-white thinking, which makes them non-arguments. We live in a very diverse world of computing, where for example, platform centric software clearly thrives. – this Aug 10 '15 at 14:51
  • @this That's fine. Let them thrive. I don't mind (matter of fact, I'm part of it). But I won't teach non-standard behaviour to newbies. Once they are experts, they can decide that for themselves. – nvoigt Aug 10 '15 at 15:26
  • @nvoigt And we come full circle. Teaching that void main isn't defined by C is incorrect. It may be non-standard, whatever your definition of standard is, but it is definitely implicitly defined by the C Standard. I was tempted to call you out on the "standard" definition before, I will definitely do it the next time. – this Aug 10 '15 at 15:41
  • @this This is a free site, feel free to do whatever you please. – nvoigt Aug 10 '15 at 16:13

1 Answers1

0

Another approach to splitting and recombining the struct elements into a char* string is with the string functions sprintf and then strncpy. There are many, many ways to do this. Simple pointer arithmetic will do, etc.. But this approach is fairly clean and straightforward:

#include <stdio.h>
#include <string.h>

struct K 
{
char a[10];
char b[10];
};

int main (void)
{
    char tmp[21] = {0};
    char *buf = tmp;
    struct K x = { "Hello","Pollo"};
    struct K rev = {{0},{0}};

    /* combine x.a & x.b into single string in buf */
    sprintf (buf, "%s%s", x.a, x.b);

    /* print the combined results */
    printf ("\n combined strings: %s\n\n", buf);

    /* get original lenght of x.a & x.b */
    size_t alen = strlen(x.a);
    size_t blen = strlen(x.b);

    /* copy from buf into rev.a & rev.b as required */
    strncpy (rev.a, buf, alen);
    strncpy (rev.b, buf+alen, blen);

    printf (" recombined: rev.a: %s  rev.b: %s\n\n", rev.a, rev.b);

    return 0;
}

Output

$ ./bin/struct2str

 combined strings: HelloPollo

 recombined: rev.a: Hello  rev.b: Pollo
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85