theartist33
theartist33

Reputation: 482

Inserting at the end of queue in C

I am trying to insert node at the end of queue and facing below error . This is simple fundamental error while compiling the code but making my life hard.

#include<stdio.h>
#include<stdlib.h>

typedef struct UNIX  {

char str[20];

struct UNIX *next;
}examp;

examp *head=NULL;

int insert_last(char *s)

{
    examp *new,*slide;
    slide=head;
    new = (examp *)malloc(sizeof(examp));

      if(!new)
           return(EXIT_FAILURE);
    while(slide->next!=NULL)
        slide=slide->next;
    slide->next=new;
    new->str=s;
    new->next=NULL;
    if(head==NULL)
    {   head=new;
        return 1;
    }
    return 1;
}
void display (void);
int main()

{


insert_last("hello ");
insert_last("how ");
insert_last("have ");
insert_last("you ");
insert_last("been ");
insert_last("! ");
display();

}

void display(void)
{
    examp *slide;
    slide=head;
    while(slide->next!=NULL)
    { printf("%s   ",slide->str);
       slide=slide->next;
    }

}

Error :stack_queue.c:27:10: error: assignment to expression with array type new->str=s;

Update : Using strncpy reolved the error , but code is not working as expected and stopping unexpectedly.

Upvotes: 0

Views: 973

Answers (5)

maxpovver
maxpovver

Reputation: 1600

Your error is in the first call insert_last("hello "):

int insert_last(char *s)
{
examp *new,*slide;
slide=head;
new = (examp *)malloc(sizeof(examp));

  if(!new)
       return(EXIT_FAILURE);
while(slide->next!=NULL)
    slide=slide->next;

when you call it first time, head is NULL, so slide becomes NULL, but you don't check it, and call it in

while(slide->next!=NULL)

Here slide is null for the frst function call

Upvotes: 0

Frankie_C
Frankie_C

Reputation: 4877

You cannot assign a string to an array! An array have its own memory, you can write or read elements in the array, but cannot assign an address.

You can eventually copy the string s contents to the array:

strncpy(new->str, s, 19);
new->str[19] = '\0';    //Close the string in case of overflow.

We used strncpy to limit the copied characters to the array size (19 chars + the ending '\0').

Upvotes: 2

Pedro Isaaco
Pedro Isaaco

Reputation: 404

As already stated you will have to use strcpy (or strncpy) in order to assign the string.

Asides from that I wanted to mention two things:

  1. Do not forget to free your memory allocated by malloc. Create a method to free a single node (examp) then you can also provide a method to destroy the whole list.

  2. I would suggest to rename the variable new to avoid confusion (pure C compiler may deal with it, but C/C++ compiler will most likely get into trouble).

Considering your update: Take a look at the following line

while(slide->next!=NULL)

At this time slide does not even exist (it is NULL), still you perform an operation on the pointer. This is the reason why the program crashes.

Upvotes: 0

Sakib Ahammed
Sakib Ahammed

Reputation: 2480

You can try it. Only replace in new->str=s; to strcpy(new->str, s); (ie. s will be copy in to new->str)

#include<stdio.h>
#include<stdlib.h>

typedef struct UNIX  {

char str[20];

struct UNIX *next;
}examp;

examp *head=NULL;

int insert_last(char *s)

{
    examp *new,*slide;
    slide=head;
    new = (examp *)malloc(sizeof(examp));

      if(!new)
           return(EXIT_FAILURE);
    while(slide->next!=NULL)
        slide=slide->next;
    slide->next=new;
    strcpy(new->str, s);
    new->next=NULL;
    if(head==NULL)
    {   head=new;
        return 1;
    }
    return 1;
}
void display (void);
int main()

{


insert_last("hello ");
insert_last("how ");
insert_last("have ");
insert_last("you ");
insert_last("been ");
insert_last("! ");
display();

}

void display(void)
{
    examp *slide;
    slide=head;
    while(slide->next!=NULL)
    { printf("%s   ",slide->str);
       slide=slide->next;
    }

}

Upvotes: 0

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 70929

You can't assign to a static array like that. Consider using strcpy or strncpy to copy the contents of the character string instead.

Upvotes: 2

Related Questions