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;
}
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;
}