1

My exercise is to use a structure to initialise values and then print them (sounds easy enough) but instead of using the structure to print them, we have to use the pointer *p. I know this probably has a really simple answer, but help would be appreciated!

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

struct info
{
    int total;
    char *str;
};

int main()
{

    struct info s, *p = &s;

    s.total = 50;
    s.str = "hello";

    printf("Info total: %i\n", s.total);
    printf("Info *str: %s\n", s.str);

    return 0;
}
myaut
  • 11,174
  • 2
  • 30
  • 62
Danny Wilson
  • 331
  • 2
  • 11
  • Did you read you the paragraph on structures in the C book? – ouah Oct 23 '15 at 22:01
  • 1
    Have you heard about [arrow operator `->`](http://stackoverflow.com/questions/2575048/arrow-operator-usage-in-c)? – myaut Oct 23 '15 at 22:01
  • 1
    `s.total` <=> `p->total` or `(*p).total` – BLUEPIXY Oct 23 '15 at 22:05
  • Thank you myaut and BLUEPIXY! I have got it to work now. And ouah, we don't have a module book this year, just a large book of notes written by the lecturer and there is no mention of this arrow operator. – Danny Wilson Oct 23 '15 at 22:08

1 Answers1

3
s.total <=> p->total or (*p).total

Thanks to you all for the answers!

Danny Wilson
  • 331
  • 2
  • 11