C - Printing the head of a linked list - c

I am trying to build a linked list from a string of integers separated by spaces. Each integer in the string will be added to the linked list except for -1. However, when I try to print the data in the head node of the list, I get the error Member reference base type 'Node *' (aka 'struct node *') is not a structure or union. Why can't I print head_ptr's data in that line?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node Node;
struct node {
int data;
Node *next;
};
void build_linked_list(Node **head_ptr) {
char *string = malloc(1028);
char *p = string, *found = string;
Node *nextNode = NULL;
if (fgets(string, 1028, stdin) != NULL) {
while ((found = strsep(&p, " \n")) != NULL) {
if (strcmp(found, "-1") == 1) {
Node node = {atoi(found), nextNode};
nextNode = &node;
}
}
}
*head_ptr = nextNode;
printf("%i\n", *head_ptr->data); // can't print data in head node
free(string);
}
int main() {
Node *head = NULL;
build_linked_list(&head);
return EXIT_SUCCESS;
}

you need to put parentheses around the head_ptr to make it work, like this: (*head_ptr)->data)
the problem arises since the compiler evaluates the expression as a dereference of a double pointer with a member of int, so first it tries to get the int member from the double pointer, which doesn't exist.
so that's why you need to put the parenthesis, so it will evaluate the head_ptr as a dereference of the double pointer, and will use the int member of that struct.

Related

Unable to pass NULL pointer to array

#include <stdio.h>
#include <stdlib.h>
typedef struct nodeWords
{
char * word;
int index;
struct nodeWords *left;
struct nodeWords *right;
} nodeWords;
int main(void)
{
nodeWords * node = malloc(sizeof(*node));
printf("%lu\n",sizeof(*node));
node->left = NULL;
node->right = NULL;
nodeWords * ihash = malloc(2 * sizeof(*ihash));
printf("%p \n", node->left);
//this part not working
ihash[0] = *node->left;
printf("%p\n",ihash[0]);
}
How can I assign node->left to ihash[0] and then be able to print out ihash[0], which should point to NULL?
There are two errors in your code and a few other 'minor issues' (I've commented these in the code posted below).
The first error is that you want to create an array of pointers to nodeWords, so you will need two stars in the declaration of ihash (one star will create an array of structure objects).
Second, in ihash[0] = *node->left;, you are dereferencing node twice (once with the preceding star operator, and once again with the -> operator.
The following code fixes these issues:
#include <stdio.h>
#include <stdlib.h>
typedef struct nodeWords {
char* word;
int index;
struct nodeWords* left;
struct nodeWords* right;
} nodeWords;
int main(void)
{
nodeWords* node = malloc(sizeof(*node));
printf("%zu\n", sizeof(*node)); // Should really use "%zu" for size_t
node->left = NULL;
node->right = NULL;
nodeWords** ihash = malloc(2 * sizeof(*ihash)); // You want an array of POINTERS so you need two ** in the type!
printf("%p \n", (void*)node->left); // Pedantic: %p expects a void*
//this part not working
ihash[0] = node->left; // The "*" preceding "node" was an error: the "->" inherentlt derefernces node
// ihash[0] = (*node).left; // An alternative way of dong the same thing
printf("%p\n", (void*)ihash[0]); // Pedantic: %p expects a void*
// Don't forget to free the allocated memory...
free(ihash);
free(node);
return 0; // Always good practice to put this EXPLICIT return statement in your "main"
}

Linked list of chars in C

I'm trying to create a linked list holding char type data.
For some reason, the code does not work. The GCC compiler's warning for function "add_bottom_ListEl" is
"warning: passing argument 2 of 'add_bottom_listEl' makes integer from pointer without a cast"
and
"note: expected 'char' but argument is of type 'char * "
I suspect that there's something wrong about the way I use pointers, but I've tried many, many combinations, passing pointers to the function etc... But nothing seemed to work.
Here's main function and all the others used. MAX_CHAR is defined in all files (#define MAX_CHAR 30)
int main()
{
char name[MAX_CHAR];
scanf("%s", name);
ListEl *head = malloc(sizeof(ListEl));
strcpy(head->name, name);
head->next = NULL;
printf("%s", head->name);
add_bottom_listEl(head, name);
print_listEl(head);
return 0;
}
void add_bottom_listEl (ListEl *head, char name)
{
ListEl *newEl;
while(head->next!=NULL)
{
head=head->next;
}
newEl = (ListEl*) malloc(sizeof(ListEl));
strcpy(newEl->name, name);
newEl->next = NULL;
}
void print_listEl(ListEl* head)
{
puts("print");
ListEl* current = head;
while (current!=NULL)
{
int i=1;
printf("%d.%s\n", i, current->name);
++i;
current = current -> next;
}
}
The ListEl structure is just a regular element of a linked list
struct ListEl
{
char name[MAX_CHAR];
struct ListEl* next;
};
Obviously, I used
typedef struct ListEl ListEl;
Every linked list tutorial on the internet or this site is only showing how to handle lists with integers or numbers in general, but not arrays (chars). Can anyone help me out here?
Your function "add_bottom_listEl" takes one character called "name", not a character array (or a pointer to a character). My guess is you want it to be:
add_bottom_listEl(ListEl *head, char *name)
If your intention in add_bottom_listEl is to modify and pass back head, then head has to be passed as a pointer to a pointer:
void add_bottom_listEl(ListEl** head, char* name) {
if ( head == NULL ) {
//head is invalid, do nothing
return;
}
//Use calloc instead of malloc to initialise the memory area
ListEl* newEl = (ListEl*)calloc(1, sizeof(ListEl));
//Ensure only name of the permissible length is copied
strncpy(newEl->name, name, MAX_CHAR-1);
//No need to do this now...calloc will initialise it to NULL
//newEl->next = NULL;
if ( *head == NULL ) {
//No nodes in list yet, this is the first
*head = newEl;
} else if ( *head != NULL ) {
//Find the end of the list
while((*head)->next!=NULL) {
*head = (*head)->next;
}
}
//Add the new node to the list
*head = newel;
}
When you call this modified version of the function pass the address of the pointer:
add_bottom_listEl(&head, name);
You can make your typedef more readable by doing this:
typedef struct _listEl {
char name[MAX_CHAR];
struct _listEl* next;
} ListEl;
The line
void add_bottom_listEl (ListEl *head, char name)
should be
void add_bottom_listEl (ListEl *head, char* name)

Trouble with signature of function to add node to end of linked list

In a program I'm writing I need a linked list, so it's a pretty specific implementation. It needs:
the ability to add a node to the end
the ability to remove a node whose data matches a specified value
The data is a cstring, no more than 20 characters in length. I'm not very experienced with C and am getting errors with the following signature void addToEnd(llist root, char entery[51]). I tried replacing llist with node but then the error is "unknown type name node". How can I get rid of this?
Here's the code
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
typedef struct node
{
char entery[51];
struct node* next;
} llist;
/*may be losing root address permanently*/
void addToEnd(llist root, char entery[51])
{
while(root->next != NULL)
root = root->next;
node last = malloc(sizeof(struct node));
root->next = last;
strcpy(last, entery);
}
int main()
{
struct node *root = malloc(sizeof(struct node));
root->next = NULL;
strcpy(root->entery, "Hello");
struct node *conductor = root;//points to a node while traversing the list
if(conductor != 0)
while(conductor->next != 0)
conductor = conductor->next;
/* Creates a node at the end of the list */
conductor->next = malloc(sizeof(struct node));
conductor = conductor->next;
if (conductor == NULL)
{
printf( "Out of memory" );
return EXIT_SUCCESS;
}
/* initialize the new memory */
conductor->next = NULL;
strcpy(conductor->entery, " world\n");
addToEnd(root, " at the");
addToEnd(root, " end");
/*print everything in list*/
conductor = root;
if(conductor != NULL)
{
while(conductor->next != NULL)
{
printf("%s", conductor->entery);
conductor = conductor->next;
}
printf("%s", conductor->entery);
}
return EXIT_SUCCESS;
}
One thing I'm unclear about, is in all the examples I've seen is they typedef the struct. Why? Let me elaborate: how do you know if you want to be passing just node or struct node. Also I don't really see the point, struct node isn't that much longer than a single typedef'd name.
Problems:
line 12: void addToEnd(llist root, char entery[51]) shall be void addToEnd(llist *root, char entery[51]). Here root must be a pointer type or you actually can not modify its value inside the function and make it visible outside the function.
line 16: node last = malloc(sizeof(struct node)); shall be struct node *last = malloc(sizeof(struct node));. Since in C you must reference a type name with the keyword struct, and also it shall be a pointer or it cannot be initialized with malloc.
As for your typedef question, I believe it is optional and people use it only for convenience. Personally I don't use typedef on a struct very often.
EDITED:
Also your code comes with bugs. Sorry I was only focusing on the syntax before.
Please notice that malloc in C don't assure you that the allocated memory is zeored, it's actually could be anything inside. So you need to fill it manually: to add a line last->next = NULL; at the end of addToEnd.
To refer to your struct of the linked list, use struct node, after the typedef, you can also use llist. You can also ues, as the linked question uses.
typedef struct node
{
char entery[51];
struct node* next;
} node;
In this style, you can use node the same as struct node.
The syntax error you are facing is, you misused the arrow operator ->, it's used with pointers of struct. For struct, use the dot operator .
So for the function
void addToEnd(llist root, char entery[51])
{
while(root->next != NULL)
root = root->next;
You should pass in a pointer:
void addToEnd(llist* root, char entery[51])

linked list && structures

I would like to know if design of my program is correct, as well as to understand if my commented area is doing what it supposed to be doing. I get these compile errors that are associated probably with commented segments of my code, and I would lie to receive some help. THANKS!
part1.c:15:6: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'insert'
part1.c: In function 'main':
part1.c:43:14: error: incompatible types when assigning to type 'struct point' from type 'int'
part1.c:49:44: error: invalid type argument of '->' (have 'struct point')
part1.c:49:59: error: invalid type argument of '->' (have 'struct point')
part1.c:55:5: error: incompatible type for argument 1 of 'free'
/usr/include/stdlib.h:488:13: note: expected 'void *' but argument is of type 'struct point'
char *chars[3]= {"a","b","c"};
int nums[3]= {5,8,9};
struct point {char *letter;
int number;
struct point *next;};
struct point* insert(struct point list[],char *rqdLetters, int rqdNums)
{
struct point *new;
new = (struct point*)malloc(sizeof(struct point));
if(new == NULL)
fprintf(stderr,"error!");
new->letter = rqdLetters;
new->number = rqdNums;
new->next = head;
head = new;
//not sure if i'm returning the a pointer to the start of new list
return head;
}
int main(int argc, char **argv)
{
//not sure if i need to declare these here or in the insert
struct point list[3];
struct point *head = NULL;
struct point *next;
struct point *new;
int i;
for (i = 0; i < 3; i++)
{
//return result put back into the pointer to the start of the list
head[i] = insert(list[i], chars[i], nums[i]);
}
int j;
for(j = 0; j < 3; j++)
{
printf("letter %s and number %d\n", list[j]->letter, list[j]->number);
}
int z;
for(z = 0; z < 3; z++)
{
free(list[z]);
}
return 0;
}
At a glance, there are several issues with your code. Firstly, you're not declaring your variables correctly.
new = list;
should be:
struct point* new;
Your function signature also looks a little suspect. If you're returning a pointer to your data structure, it should be something like:
struct point* insert(...) { ... }
At a more general level, I does seem like your idea of a linked list may be a little off. To represent a list, you should only need to hold on to the head and tail of the list, instead of keep an array of your points.
It usually helps if you create a data structure to hold these pointers. You can then pass this structure around to functions that operate on the list e.g. the insert() function.
As a quick example (untested):
struct node {
struct node *next;
char letter;
int number;
}
struct list {
struct node *head;
struct node *tail;
}
/* create a new list */
struct list* list_new(void) {
struct list *L = malloc(sizeof(struct list));
L->head = NULL;
L->tail = NULL;
}
/* add a new node to the list */
void list_insert(struct list *list, char in_letter, int in_number) {
struct node *node = malloc(sizeof(struct node));
node->letter = in_letter;
node->number = in_number;
node->next = NULL;
if (list->head == NULL) { /* empty list */
list->head = node;
list->tail = node;
} else { /* append to list */
list->tail->next = node;
list->tail = node;
}
}
You can then use it as such:
int i;
char chars[3]= {"a","b","c"};
int nums[3]= {5,8,9};
struct list *mylist = list_new();
for (i = 0; i < 3; i++)
{
list_insert(mylist, chars[i], nums[i]);
}
In response to:
... and i am not sure if i am supposed to declare it inside insert or main, i did in the main however
This depends on where you intend to use the variables and the intended lifespan of these variables. As stated in the comments above, you might want to polish up on your understanding of scoping rules.

Troubleshooting compile time circular linked list errors

Okay, so I'm getting these errors upon compile with gcc:
prelab6.h: In function âinsertHeadCircularâ:
prelab6.h:45: error: incompatible types in assignment
prelab6.h:46: error: incompatible types in assignment
prelab6.c: At top level:
prelab6.c:41: warning: data definition has no type or storage class
prelab6.c:41: warning: parameter names (without types) in function declaration
prelab6.c:41: error: conflicting types for âprintInOrderâ
prelab6.h:81: error: previous definition of âprintInOrderâ was here
prelab6.c:42: warning: data definition has no type or storage class
prelab6.c:42: warning: parameter names (without types) in function declaration
prelab6.c:42: error: conflicting types for âprintReverseâ
prelab6.h:112: error: previous definition of âprintReverseâ was here
I've tried and tried, but to no avail to fix these errors. Thanks for any and all help.
Here's my .c file:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "my.h"
int main ( int argc, char **argv )
{
char firstname[100];
char lastname[100];
int monthsEmployed;
FILE *fptr;
fptr = fopen(argv[1], "r");
if (fptr == NULL)
printf ("Incorrect file reading!");
if (argc != 2)
printf ("Incorrect number of arguments!");
employeeInfo *insert;
insert = malloc(sizeof(employeeInfo));
employeeList *head;
head = NULL;
while(!feof(fptr))
{
fscanf (fptr, "%100s %100s %d", firstname, lastname, &monthsEmployed);
strcpy(insert->firstname, firstname);
strcpy(insert->lastname, lastname);
insert->monthsEmployed = monthsEmployed;
head = insertHeadCircular(head, insert);
}
}
printInOrder(head); // display the linked list
printReverse(head); // display the linked list in reverse
And my .h file (note things are commented out because I tried things differently with no results):
typedef struct employeeInfo{
char firstname[100];
char lastname[100];
int monthsEmployed;
}employeeInfo;
//Struct containing pointers to the next and previous used to make a circular linked list
typedef struct list{
employeeInfo emp;
struct list *next;
struct list *previous;
}employeeList;
employeeList *insertHeadCircular(employeeList *head, employeeInfo *emp);
void printInOrder(employeeList head);
void printReverse(employeeList head);
employeeList *insertHeadCircular(employeeList *head, employeeInfo *emp)
{
employeeList *theprevious = head;
employeeList *current;
employeeList *thenext = head;
current = malloc(sizeof(employeeList));
employeeInfo *employee;
if(thenext==NULL)
{
current->next = current;
current->previous = current;
}
else
{
current->next = thenext;
thenext->previous = current;
while(theprevious->next != thenext)
{
theprevious = theprevious->next;
}
current->previous = theprevious;
theprevious->next = current;
}
current->emp = (employeeInfo *)malloc(sizeof(employeeInfo));
employee = current->emp;
employee = malloc(sizeof(employeeInfo));
strcpy(employee->firstname, emp->firstname);
strcpy(employee->lastname, emp->lastname);
employee->monthsEmployed = emp->monthsEmployed;
/*
employeeList *newcell, *first = head;
if(head == NULL)
{
newcell = (struct list *)malloc(sizeof(struct list));
strcpy(newcell->firstname, emp->firstname);
strcpy(newcell->lastname, emp->lastname);
newcell->monthsEmployed = emp->monthsEmployed;
return newcell;
}
while(head->next != first)
{
head = head->next;
}
newcell = (struct list *)malloc(sizeof(struct list));
head->next = newcell;
strcpy(newcell->firstname, emp->firstname);
strcpy(newcell->lastname, emp->lastname);
newcell->monthsEmployed = emp->monthsEmployed;
newcell->next = first;
*/
return current;
}
void printInOrder(employeeList head)
{
/*employeeInfo *first = head;
if (head == NULL)
{
printf("The circularly linked list is empty!\n");
return;
}
do
{
printf("%s %s %d\n", emp.firstname, emp.lastname, head.monthsEmployed);
head = head->next;
} while(head != first);
*/
/*employeeInfo current = head;
employeeInfo start = head;
int loop = 0;
printf("--------------\n");
while(current != start || loop==0)
{
loop++;
printf("Employee: %s %s\nMonths Employed: %d", current->firstname, current->lastname, current->monthsEmployed);
printf("--------------\n");
current=current->next;
}*/
}
void printReverse(employeeList head)
{/*
employeeList current = head
employeeInfo start = head
int theloop=0;
printf("--------------\n");
while(current! = start || loop==0)
{
loop++;
printf("Employee: %s %s\nMonths Employed: %d", current->firstname, current->lastname, current->monthsEmployed);
printf("--------------\n");
current=current->previous;
}*/
}
EDITED PROGRAM
Error:
file.c: In function âmainâ:
file.c:37: error: incompatible type for argument 2 of âinsertHeadCircularâ
The .c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "file.h"
int main ( int argc, char **argv )
{
char firstname[100];
char lastname[100];
int monthsEmployed;
FILE *fptr;
fptr = fopen(argv[1], "r");
if (fptr == NULL)
printf ("Incorrect file reading!");
if (argc != 2)
printf ("Incorrect number of arguments!");
employeeInfo *insert;
insert = malloc(sizeof(employeeInfo));
employeeList *head;
head = NULL;
while(!feof(fptr))
{
fscanf (fptr, "%100s %100s %d", firstname, lastname, &monthsEmployed);
strcpy(insert->firstname, firstname);
strcpy(insert->lastname, lastname);
insert->monthsEmployed = monthsEmployed;
head = insertHeadCircular(head, insert);
}
printInOrder(head); // display the linked list
printReverse(head); // display the linked list in reverse
}
The .h:
typedef struct employeeInfo{
char firstname[100];
char lastname[100];
int monthsEmployed;
}employeeInfo;
typedef struct list{
employeeInfo emp;
struct list *next;
struct list *previous;
}employeeList;
typedef employeeList *listnode;
employeeList *insertHeadCircular(employeeList *head, employeeInfo emp);
void printInOrder(employeeList *head);
void printReverse(employeeList *head);
employeeList *insertHeadCircular(employeeList *head, employeeInfo emp)
{
listnode newPtr;
listnode firstPtr;
listnode tempPtr;
newPtr = (employeeList *)malloc(sizeof(employeeList));
strcpy(newPtr->emp.firstname, emp.firstname);
strcpy(newPtr->emp.lastname, emp.lastname);
newPtr->emp.monthsEmployed = emp.monthsEmployed;
if(head == NULL)
{
newPtr->next = newPtr;
newPtr->previous = newPtr;
head = newPtr;
firstPtr = newPtr;
}
else
{
tempPtr = firstPtr;
newPtr->next = tempPtr;
tempPtr->previous = newPtr;
newPtr->previous = head;
head->next = newPtr;
firstPtr = newPtr;
}
return head;
}
void printInOrder(employeeList *head)
{
listnode currentPtr = head;
do
{
printf("%s %s %d\n",currentPtr->emp.firstname, currentPtr->emp.lastname, currentPtr->emp.monthsEmployed);
currentPtr= currentPtr->previous;
}
while(currentPtr !=head);
}
void printReverse(employeeList *head)
{
listnode currentPtr = head->next;
do
{
printf("%s %s %d\n",currentPtr->emp.firstname, currentPtr->emp.lastname, currentPtr->emp.monthsEmployed);
currentPtr = currentPtr->next;
}
while(currentPtr != head->next);
}
In your insertHeadCircular() function, you treat the emp member of employeeList as though it were an employeeInfo *. For example, you declare:
employeeInfo *employee;
but then later do this:
employee = current->emp;
However, your employeeList type contains an instance of employeeInfo, not a pointer to one:
typedef struct employeeInfo{
char firstname[100];
char lastname[100];
int monthsEmployed;
}employeeInfo;
/* ... */
typedef struct list{
employeeInfo emp; /* see? Not a pointer. */
struct list *next;
struct list *previous;
}employeeList;
So basically you need to correct your code so that you stop assigning a structure to a pointer, and instead assign the address of the structure to the pointer.
Most likely your printInOrder() and printReverse() functions should take employeeList * arguments, rather than employeeList ones, as well... and you should check the code you use for them to make sure you don't confuse the two anywhere.
It's also a good idea to define your functions somewhere other than the header file, such as in a separate .c source file. The header file should just contain function prototypes, macros, and other declarations that may be needed for other source files; you don't need the function bodies in there, since the linker can find them in the object files created from your other sources. Defining functions in header files like that will cause endless headaches when the header file is #includeed by more than one file.
The error you get with your updated code, file.c:37: error: incompatible type for argument 2 of âinsertHeadCircularâ is pointing out that the type of argument you're passing to insertHeadCircular() isn't the type you gave in its declaration -- which is true. You've declared and defined that function to take an employeeInfo as its second argument:
employeeList *insertHeadCircular(employeeList *head, employeeInfo emp)
...but in main() you pass it a pointer instead:
employeeInfo *insert;
...
head = insertHeadCircular(head, insert);
So you need to change one or the other. Either dereference insert when you call the function from main, or change insertHeadCircular() to take a pointer instead (and update the body accordingly). The latter is probably better, since it avoids copying the entire structure onto the stack when you call the function.
Some other things to point out:
You should really check the return from scanf() in your loop in main(). It would let you know whether all the fields were actually read; right now, if they weren't, your program just proceeds with whatever junk the variables already had (like whatever was read on the previous iteration, possibly). Checking other return values (like the return from malloc()) is a good idea too, but in this case the scanf() return is particularly important.
You also don't free insert at the end of your program; the OS will (almost certainly) clean it up when your program exits, but it's good practice to do it yourself when you're done with it. The way you use it, though, you didn't really need to dynamically allocate it anyway; you could have just declared an employeeInfo and taken its address (works either way, though).

Resources