*p1 = *p2 copies the structure pointed by p2 into that of p1, but
p1 = p2 makes p1 to point to the structure pointed by p2.
Now,
lets build a linked list dynamically.
void add(char * item, struct list *oldhead)
{
struct list *new = malloc(sizeof(struct list));
if(new == NULL)
{
printf("out of mem\n");
exit(1);
}
new->item = item;
new->next = oldhead;
head=new;
}
int main()
{
struct list
{
char *item;
struct list *next;
};
struct list *head = NULL;
add("hi",head);
}
BTW this piece of C code interested me
#include
int main()
{
int width = 9;
printf("%10d\n",1);
printf("%*d",width,1);
}
Mainly using a * before d to decide the width run time.
printf vs fprintf
printf prints output in STDOUT whereas fprintf we can print in user defined stream
for ex:
FILE *f = fopen("hi.txt","w");
fprintf(f,"%s","Hello");
No comments:
Post a Comment