You can do it as follows:
#include <stdio.h>
typedef struct _date {
char d[11], t[6], s[4]; // +1 to size for null-terminator ('')
struct _date *next;
} *date;
int main() {
struct _date a = { "15.07.2017", "16:00", "Foo", NULL };
date a_ptr = &a;
printf("Description: %s
Date: %s
Time: %s
", a_ptr->s, a_ptr->d, a_ptr->t);
return 0;
}
The brace-enclosed, comma-separated list in the example above is the struct initializer.
To respond to the edits of your question, if you wish to dynamically allocate struct _date
instances and initialize them in a function, then use malloc
as follows:
date neuerTermin(const char* d, const char* t, const char* s) {
date cal = (date)malloc(sizeof(struct _date));
strncpy(cal->d, d, 10);
strncpy(cal->t, t, 5);
strncpy(cal->s, s, 3);
cal->next = NULL;
return cal;
}
In this case you have to fill the memory block pointed by cal
member-by-member. Sample usage:
date root = neuerTermin("15.07.2017", "16:00", "Foo");
root->next = neuerTermin("27.07.2017", "10:00", "Bar");
root->next->next = neuerTermin("01.08.2017", "12:30", "Baz");
Important: if you used malloc
to allocate memory, you have to deallocate it too with using free
when you don't need it anymore.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…