Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

storing and printing string in void pointer

I have written a linked list program which stores data member as void *. while trying to store annd print using scanf/printf functions, I am getting segmentation fault.

node definition -->

typedef struct node {
        struct node *next;
        void *data;
        }node;

main function -->

                head=(node *)malloc(sizeof(node));
                if (head==NULL){
                        printf("error in allocation of memory\n");
                        exit(EXIT_FAILURE);
                }
                tail=(node*)create(head);

create function -->

void *create(node *current)
{
        int user_choice;
        while(current){
                printf("\nEnter the data:");
                scanf("%s",current->data);
                printf("stored at %p\n",(void*)current->data);
                printf("%s",(char*)current->data);
                printf("\nType '1' to continue, '0' to exit:\n");
                scanf("%d",&user_choice);

                if(user_choice == 1){
                        current->next=(node*)malloc(sizeof(node));
                        current=current->next;
                }
                else{
                        current->next=NULL;
                }
        }
        return current;
}

can anyone tell what is the correct argument for scanf & prinf should be..?


working code after incorporating points given in answers...

void *create(node *current)
{
        node *temp;
        int user_choice;
        while(current){
                printf("\nEnter the data:");
                current->data=(char*)malloc(10*sizeof(char));
                scanf("%s",current->data);
                printf("stored at %p\n",(void*)current->data);
                printf("%s",(char*)current->data);
                printf("\nType '1' to continue, '0' to exit:\n");
                scanf("%d",&user_choice);

                if(user_choice == 1){
                        current->next=(node*)malloc(sizeof(node));
                }
                else{
                        current->next=NULL;
                        temp=current;
                }
                current=current->next;
        }
        return temp;
}
like image 522
Himanshu Sourav Avatar asked Nov 15 '25 05:11

Himanshu Sourav


2 Answers

In your code,

 scanf("%s",current->data);

is attempt to make use of an unitialized pointer, it invokes undefined behavior.

You need to follow either of bellow approach,

  • make the pointer point to valid chunk of memory (using malloc() and family for dynamic allocation, for example)
  • use an array.
like image 65
Sourav Ghosh Avatar answered Nov 17 '25 20:11

Sourav Ghosh


You should first initialize data member of structure because

current->data  = malloc("passes size here");

For putting data you have to typecast first this data because void is not storage type. void pointer can be used to point to any data type.

Like

*(char *)(current->data) = 1;
like image 32
Sumit Gemini Avatar answered Nov 17 '25 20:11

Sumit Gemini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!