D
dam_fool_2003
Hai,
I thank those who helped me to create a single linked list with int
type. Now I wanted to try out for a void* type. Below is the code:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<stddef.h>
struct node
{
void *data;
struct node *next;
};
struct node *insert(void *data,struct node **p)
{
void *temp;
struct node *q;
q = malloc(sizeof *q);
if(q == NULL)
{
printf("MEM ERROR\n");
exit(EXIT_FAILURE);
}
else
{
temp = malloc(sizeof *temp);
if(temp == NULL)
{
printf("MEM ERROR2\n");
exit(EXIT_FAILURE);
}
else
{
data = temp;
q ->data = data;
q -> next = *p;
q-> next = NULL;
*p = q;
return *p;
}
}
return NULL;
}
void disp(struct node *p)
{
for(;p != NULL; p=p->next)
printf("%s",p->data);
}
int main(void)
{
char *data;
int s=49,*a = &s;
struct node *p=NULL;
scanf("%s",data);
insert(data,&p);
disp(p);
insert(a,&p);
disp(p);
return 0;
}
OUTPUT:
latiÿÿÿÿ e
I have some questions:
1) When we use printf function to print something we use format
specifier for printing. (%s,%d etc). But when it comes to void*, do we
have a format specifier? If not then how will we print the void* in
the above type of program where the printing definition is in one
function and that function is called for the printing job?
2) The output I got is totally unexcepted. What ever I type I get the
same above out put. Why?
3) When we copy two void pointers shall we use just the assignment
operator or do we have to use memcpy, memset functions. Is the out put
in the above is different because of this being not used?
Thanks in advance.
I thank those who helped me to create a single linked list with int
type. Now I wanted to try out for a void* type. Below is the code:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<stddef.h>
struct node
{
void *data;
struct node *next;
};
struct node *insert(void *data,struct node **p)
{
void *temp;
struct node *q;
q = malloc(sizeof *q);
if(q == NULL)
{
printf("MEM ERROR\n");
exit(EXIT_FAILURE);
}
else
{
temp = malloc(sizeof *temp);
if(temp == NULL)
{
printf("MEM ERROR2\n");
exit(EXIT_FAILURE);
}
else
{
data = temp;
q ->data = data;
q -> next = *p;
q-> next = NULL;
*p = q;
return *p;
}
}
return NULL;
}
void disp(struct node *p)
{
for(;p != NULL; p=p->next)
printf("%s",p->data);
}
int main(void)
{
char *data;
int s=49,*a = &s;
struct node *p=NULL;
scanf("%s",data);
insert(data,&p);
disp(p);
insert(a,&p);
disp(p);
return 0;
}
OUTPUT:
latiÿÿÿÿ e
I have some questions:
1) When we use printf function to print something we use format
specifier for printing. (%s,%d etc). But when it comes to void*, do we
have a format specifier? If not then how will we print the void* in
the above type of program where the printing definition is in one
function and that function is called for the printing job?
2) The output I got is totally unexcepted. What ever I type I get the
same above out put. Why?
3) When we copy two void pointers shall we use just the assignment
operator or do we have to use memcpy, memset functions. Is the out put
in the above is different because of this being not used?
Thanks in advance.