473,385 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Linked list w/ structure

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

Dec 11 '06 #1
3 2000
Never mind...

strcpy(cur->first_name, "First Name");

bjhe...@gmail.com wrote:
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;
}
Dec 11 '06 #2

<bj*****@gmail.comwrote in message
news:11*********************@j72g2000cwa.googlegro ups.com...
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.

<snip>}
>
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Dec 11 '06 #3
On 11 Dec 2006 01:33:45 -0800, bj*****@gmail.com wrote:
>Never mind...

strcpy(cur->first_name, "First Name");

bjhe...@gmail.com wrote:
>http://rafb.net/paste/results/tJoB4z75.html

After executing the following code I receive the errors:
I assume you meant after compiling.
>a.c: In function `insert':
a.c:59: error: incompatible types in assignment
snip
> cur=(RECORD *)malloc(sizeof(RECORD));
It would also behoove you to remove the cast.
Remove del for email
Dec 12 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jeffrey Silverman | last post by:
Hi, all. I have a linked list. I need an algorithm to create a tree structure from that list. Basically, I want to turn this: $list = array( array( 'id' => 'A', 'parent_id' => null, 'value'...
7
by: Chris Ritchey | last post by:
Hmmm I might scare people away from this one just by the title, or draw people in with a chalange :) I'm writting this program in c++, however I'm using char* instead of the string class, I am...
10
by: Fabio | last post by:
Hi everyone, Is there anybody who can suggest me a link where I can find information about 'Persistent linked list' ? I need to implement a linked list where every node is a structure like the...
5
by: John | last post by:
Hi all, Can a linked list be a member of a structure? If so, when I add or remove an element from the linked list, the size of the structure will change. Will it cause any problem? Thanks a...
5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
6
by: Steve Lambert | last post by:
Hi, I've knocked up a number of small routines to create and manipulate a linked list of any structure. If anyone could take a look at this code and give me their opinion and details of any...
5
by: Y2J | last post by:
I am working through this book on C++ programming, the author is speaking of using linked lists. He gave and example which I found confusing to say the least. So I rewrote the example in a way that...
9
by: william | last post by:
When implementing Linked list, stack, or trees, we always use pointers to 'link' the nodes. And every node is always defined as: struct node { type data; //data this node contains ... node *...
2
by: Subodh | last post by:
Hi All, I want to use a C++ API in a static lib that returns a linked List in C# I am planning to P/Invoke to perform the Interop, I would like to know which way will be better to interop a...
1
by: yaarnick | last post by:
Create a linked list data structure library to hold strings. Then library should support the following linked list operations. Create() – Creates the linked list Insert() – Insert a string into the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.