Linked list w/ structure

B

bjhecht

http://rafb.net/paste/results/tJoB4z75.html

After executing the following code I receive the errors:
a.c: In function `insert':
a.c:59: error: incompatible types in assignment

I have no problem when first_name in the structure is an int. I can
easily assign a default value in the insert function. But I would like
to allow full strings to be sent to the field from the insert
function....Any ideas?

The code is:


Code:
/*Program to illustrate construction of linked list
Written by Ron
April 2006

Language: C (gcc target)
*/



#include <stdio.h>
#include <stdlib.h> /*malloc is in stdlib, malloc.h not required*/

typedef struct member
{int number; int id_number; char first_name[30];
struct member *next;
}RECORD;

RECORD* insert(RECORD *it, int value); /*Prototype for insert*/
void traverse_and_print(RECORD *head);

int max = 0;
int main(void)
{
int i, result, count;
RECORD *head, *p;
head=NULL;
result=1;
for (i=1; i<=5; i++)
head=insert(head,i);
/* DRAW PICTURE OF LINKED LIST AT THIS POINT OF PROGRAM*/
traverse_and_print(head);
return 0;
}


RECORD* insert(RECORD *it, int value)
/*Function to insert a record into a linked list
Written by Ron
April 2006

Language: C (Borland 5.01)
*/
{
RECORD *cur, *q;
int i; char first_name[30]; char last_name[30];
//printf("Enter an employee number\n");
//scanf( "%d", &i );
//printf("Enter first name\n");
//scanf( "%s", &first_name );
//printf("Enter last name\n");
//scanf( "%s", &last_name );



// printf("Entering insert, value is %d\n", value);
cur=(RECORD *)malloc(sizeof(RECORD));
cur->number=value;
cur->id_number=i;
cur->first_name="First Name";

cur->next=NULL;
if (it==NULL)
it=cur;
else
{q=it;
while(q->next!=NULL)
q=q->next;
q->next=cur;
}
return(it);
}

void traverse_and_print(RECORD *head)
/*Function to traverse linked list, print values in records, and
mutiply them*/

{ RECORD *p;
int result;
result=1;
p=head;
while (p!=NULL)
{result=p->number;
//printf("num is %d", p->id_number);
if(result > max){max = result;}
p=p->next;
}
printf("MAX IS %d", max);

while (p!=NULL)
{result=p->number;
//printf("num is %d", p->id_number);
if(result > max){max = result;}
p=p->next;
}


return;
}
 
F

Fred Kleinschmidt

http://rafb.net/paste/results/tJoB4z75.html

After executing the following code I receive the errors:
a.c: In function `insert':
a.c:59: error: incompatible types in assignment

I have no problem when first_name in the structure is an int. I can
easily assign a default value in the insert function. But I would like
to allow full strings to be sent to the field from the insert
function....Any ideas?

The code is:


Code:
/*Program to illustrate construction of linked list
Written by Ron
April 2006

Language: C (gcc target)
*/



#include <stdio.h>
#include <stdlib.h> /*malloc is in stdlib, malloc.h not required*/

typedef struct member
{int number; int id_number; char first_name[30];

OK, you define first_name as a character array here.
struct member *next;
}RECORD;

RECORD* insert(RECORD *it, int value); /*Prototype for insert*/
void traverse_and_print(RECORD *head);

int max = 0;
int main(void)
{
int i, result, count;
RECORD *head, *p;
head=NULL;
result=1;
for (i=1; i<=5; i++)
head=insert(head,i);
/* DRAW PICTURE OF LINKED LIST AT THIS POINT OF PROGRAM*/
traverse_and_print(head);
return 0;
}


RECORD* insert(RECORD *it, int value)
/*Function to insert a record into a linked list
Written by Ron
April 2006

Language: C (Borland 5.01)
*/
{
RECORD *cur, *q;
int i; char first_name[30]; char last_name[30];
//printf("Enter an employee number\n");
//scanf( "%d", &i );
//printf("Enter first name\n");
//scanf( "%s", &first_name );
//printf("Enter last name\n");
//scanf( "%s", &last_name );



// printf("Entering insert, value is %d\n", value);
cur=(RECORD *)malloc(sizeof(RECORD));
cur->number=value;
cur->id_number=i;
cur->first_name="First Name";

.... first_name is not a pointer, but a character array.
You need to use strcpy()
Also, you chould check for size to prevent overflow.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top