Error of expression syntax - c

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node;
node *create_cll(node *head,int n);
void main()
{
clrscr();
node *head,*p;
int n;
printf("Enter the no.of elements:");
scanf("%d",&n);
head=create_cll(*head,int n);
getch();
}
node *create_cll(node *head,int n)
{
node *rear,*p;
int i;
head=p;
for(i=1;i<n;i++)
{
p=(node*)malloc(sizeof(node));
scanf("%d",&p->data);
p=rear;
rear->next=p;
p=p->next;
}
printf("Circular linked list created..!");
return (head);
}
Code is about creating the circular linked list.
But here I have an error of expression syntax which I am unable to solve.
The error is in the line in the main() section where head is equalled to the function.
So I need help...

You need to pass a ptr-to-node, not a node; and remove the int keyword:
head = create_cll(head, n);
In other news:
It's int main in C, not void main
Don't cast the return from malloc in C
Not testing the return value from scanf is a sure recipe for surprises.
printf format strings usually have a newline "\n" at the end.
<conio.h> and getch are not C (even though Mr. Gates wants you to believe that.) Use getchar() instead.

head is already a pointer
*head in your code could be changed to head as the method definition follows.

Related

While loop works fine in main function but does not run when defined inside a function in c

I was implementing linked list in c. For it i created a generation() function for making linked list when i call it inside main() the while loop don't work but when i put the generate code block in main it works fine.
#include<stdio.h>
#include<stdlib.h>
void generation(void);
void first(void);
void end(void);
void middle(void);
int n=0;
struct node{
int data;
struct node *next;
};
struct node *head,*newnode,*temp,*prevnode;
void main(){
generation();
first();
end();
middle();
}
void generation(void){
head=0;
int choice;
while(choice){
newnode=(struct node*)malloc(sizeof(struct node));
printf("Enter data\n");
scanf("%d",&newnode->data);
newnode->next=0;
if(head==0)head=temp=newnode;
else{
temp->next=newnode;
temp=newnode;
}
printf("Enter for going further(0,1)\n");
scanf("%d",&choice);
n++;
}
temp=head;
printf("Your data is:\n");
while(temp!=0){
printf("Values are %d\n",temp->data);
temp=temp->next;
}
}
Unlike some programming languages, C/C++ does not initialize most variables to a given value (such as zero) automatically
You're not initializing the choice variable before the loop, thus you don't know what value it contains
More details here What happens to a declared, uninitialized variable in C? Does it have a value?

Error, linked lists using C programming

I'm trying to code simple push function and everything is fine until run time, executed code crashes. Anyone can clarify the reason, please ?
#include<stdio.h>
#include<stdlib.h>
typedef struct list{
int order;
struct list *next;
}list;
void push(struct list **arg,int i);
int main()
{
struct list **ptr=NULL;
for(int i=0;i<10;++i){
push(ptr,i);
}
return 0;
}
void push(struct list **arg,int i){
struct list *temp;
temp= malloc(sizeof(list));
temp->order=i;
temp->next=*arg;
*arg=temp;
}
Write
list *ptr=NULL;
^^^
for(int i=0;i<10;++i){
push( &ptr,i);
^^^^
}
Otherwise if to declare like this
struct list **ptr=NULL;
then this dereferencing
*ptr
results in undefined behavior.

segmentation error in ubuntu

Please tell me why the segmentation error is there in my program there is no error .
I also tried to debug it but it never goes inside the for statement.
#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
struct node* link;
} *start;
main()
{
int i,n,m;
start=NULL;
printf("enter the number of nodes you want");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the element you want to insert");
scanf("%d",&m);
create_list(m);
}
}
create_list(int data)
{
struct node *q,*temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=data;
temp->link=NULL;
if(start==NULL)
start=temp;
else
{
while(q->link!=NULL) q=q->link;
q->link=temp;
}
}
You forgot to initialize q before using it:
q = start;
while(q->link!=NULL)
1.You haven't initialized q in create_list() and used it -
while(q->link!=NULL)
Intialize q=start; before this loop.
2.Also free the allocated memory for temp in function.
3.main() should be int main(void) and what is type of create_list? Declare its prototype before main.
In the function create_list local pointer q was not initialized
struct node *q,*temp;
^^^
However it is accessed in the loop
while(q->link!=NULL)
I think you mean the following
else
{
q = start;
while ( q->link != NULL ) q = q->link;
q->link = temp;
}
Take into account that the function should be declared before its usage. Place it declaration for example before main. And its return type shall be void and specified explicitly. Also function main shall have return type int.
For example
void create_list( int data );
int main( void )
{
//...
And it is a good idea to free all dynamically allocated memory before exiting the program.
Also header <malloc.h> is not a standard C header. You should use <stdlib.h> instead.
After all suggestions from the above it will be clearly in the future if you try to show some work of your clear codding and to respect the minimum standard.
Here is a way of how should be looking your code:
#include<stdio.h>
#include<stdlib.h> /* You need stdlib not malloc */
void create_list(int data); /* If you don't declare your function the compiler doesn't know nothing about create_list */
struct node{
int data;
struct node* link;
}*start;
int main(void){ /* Here return type of main is int and if no arg needed should be used void */
int i,n,m;
start=NULL;
printf("enter the number of nodes you want");
if((scanf("%d",&n)) != EOF) /* always check scanf's return */
for(i=0;i<n;i++){
printf("enter the element you want to insert");
if((scanf("%d",&m)) != EOF) /* here the same: always check scanf's return */
create_list(m);
}
return 0; /* return of main should be 0 or one of the following: EXIT_SUCCESS or EXIT_FAILURE, but 0 will be ok because it is standard*/
}
void create_list(int data){ /* here should be explicit what kind of function is */
struct node *q,*temp;
temp=(struct node *)malloc(sizeof(struct node)); /* if you allocate memory dynamically ..... */
temp->data=data;
temp->link=NULL;
if(start==NULL){
start=temp;
}else{
q = start;
while(q->link!=NULL){
q=q->link;
q->link=temp;
}
}
free(temp); /* .....then always free it */
}

linked list implementation using pointer to pointer in C

I am unable to append a new node in the linked list. I have already identified the problem area but after much research and trying many things I am still unable to resolve the issue.
The problem is with the for loop in insert_node(char,struct **) function and traverse(struct *) function both of which never seem to terminate:
// program that stores name of the user using linkedlist
#include<stdio.h>
#include<stdlib.h>
typedef struct LIST{
int flag;
char name;
struct LIST *next;
} LISTNODE;
LISTNODE *head=NULL,*newnode=NULL;// global pointers
LISTNODE* initialize(); //initializes struct node with default values and returns a node
void insertNode(char c,LISTNODE** temp);
void traverselist(LISTNODE *temp);
int main(){
char ans,ch;
printf("\n\nEnter your name and hit enter-\n\n");
do{
printf("your name:");
fflush(stdin);
scanf("%c",&ch);
insertNode(ch,&head);
printf("\n\ninsertnode-back to main()");
printf("Want to continue?(Y?N):");
fflush(stdin);
scanf("%c",&ans);
}while(ans=='y'||ans=='Y');
printf("\n\ntraverselist-leaving main()");
traverselist(head);
printf("\n\ntraverselist-back to main()");
return 0;
}
void insertNode(char c, LISTNODE **temp){
printf("\n\ninto insertnode: before initialize");
LISTNODE* temp2;
newnode=initialize();
printf("\n\nback to insertnode:after initialize");
//printf("\nnewnode->name=%c",newnode->name);
//printf("\nnewnode->flag=%d",newnode->flag);
newnode->name=c;
//printf("\nnewnode->name=%c",newnode->name);
//printf("\nnewnode->flag=%d",newnode->flag);
//for(;(*temp)!=NULL;temp=&(*temp)->next);
/*while((*temp)->next!=NULL){
temp=&(*temp)->next;
printf("\n\nIn while!");
}
*/
for(;*temp!=NULL;temp=&((*temp)->next))
printf("\n\nIn for!") ;
//printf("\n\nout of while!");
(*temp)=newnode;
}
LISTNODE* initialize(){
static int count=0;
LISTNODE *tempnewnode;
printf("\n\nINto inintialize!");
tempnewnode=(LISTNODE*)malloc(sizeof(LISTNODE));
if(tempnewnode==NULL){
printf("No memory available. Aborting!");
exit(0);
}
else{
tempnewnode->flag=0;
tempnewnode->name='*';
tempnewnode->next=NULL;
if(count==0){
head=tempnewnode;
count++;
}
}
return tempnewnode;
}
void traverselist(LISTNODE *temp){
printf("\n");
for(;temp!=NULL;temp=temp->next){
printf("%c",temp->name);
}
}
Please help!
The problem is inside the insert_node function, specifically with the loop:
for(;*temp!=NULL;temp=&((*temp)->next))
printf("\n\nIn for!");
You'd be better advised not to use the reference temp in your loop, as it overwrites the head->next back to itself. Create another temporary pointer.
I changed the insertNode(char, LISTNODE**) to the following:
void insertNode(char c, LISTNODE *temp){
LISTNODE** temp2=&temp;
newnode=initialize();
printf("\n\nback to insertnode:after initialize");
newnode->name=c;
for(;(*temp2)!=NULL;temp2=&(*temp2)->next)
printf("\n\nIn for!") ;
(*temp2)=newnode;
}
and function is called like this:
insertNode(ch,head);
It works just fine!
The problem is this portion of your insertNode function
for(;*temp!=NULL;temp=&((*temp)->next))
printf("\n\nIn for!") ;
//printf("\n\nout of while!");
(*temp)=newnode;
Here you should first check if the link list is empty or not if it is empty then you can create the new node and assign the address of it to temp.
If not then depending upon whether you want to insert the new element in the end, beginning or middle of the list you should traverse the list then perform the insertion.
For example if you want to perform insertion at the beginning then after creating the new node you should assign the address of the start pointer of the list to the next of newly created node and move the start to the new node as you have to keep track of the start pointer.

map/reduce/filter on a linked list... failing at map! (in C)

So here's my code... My understanding is that I'm supposed to create a function "map" that takes a function as an argument. It's not going as planned. Any help would be completely amazing.
Here's a compilable (well not compilable, but scaled down) version of the code:
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
int main(int argc, const char *argv[])
{
//it should be apparent that I am quite new to C, I have some java experience.
struct linkedList {
int count;
int num;
struct linkedList *next;
};
struct linkedList *head, *tail, *curr;
int count1=0;
int i=0;
int square(int v) {return v=v*v;}
void map(int (*func)(int v), struct linkedList){
struct linkedList2 *head, *tail, *curr;
for(curr=head; curr!=NULL; curr=curr->next){
curr->num=func(curr->num);
printList();
}
}
void start(){
printf("This program will ONLY accept integer input.\n");
head=NULL;
for(i=1;i<=4;i++) {
count1++;
curr=malloc(sizeof(struct linkedList));
curr->count=count1;
printf("Enter a number: ");
scanf("%d", &curr->num);
if(head==NULL) { head=curr; }
else { tail->next=curr; }
tail=curr;
tail->next=NULL;
}
printf("A list has been created.\n");
}
void printList(){
printf("The list now contains these numbers: ");
for(curr=head;curr!=NULL;curr=curr->next){
printf("%d, ", curr->num);
}
printf("\n");
}
start();
printList();
map(square, linkedList);
printList();
system("PAUSE");
return 0;
}
Defining all of those structs and functions inside of main is not how you're supposed to write C. Move the int main(int argc, const char *argv[]) { to right after the definition of printList so that main only contains the actual code for main.
Moreover, your definition of map appears to have an unfinished prototype. Instead of void map(int (*func)(int v), struct linkedList), in which the second parameter is unused, you want void map(int (*func)(int v), struct linkedList* head) (and then get rid of the declaration of head on the next line). Moreover, linkedList2 here presumably should be changed to linkedList. In addition, your attempt to call map in main with map(square, linkedList) is nonsensical; you want to use map(square, head).

Resources