Help with variable scope in C !!
I wanted to find out the factors of a number. I decided later that i'd store all the factors of the number in a linked list so that i could get more familiar with them instead of using an array.
Here's the code
Code:
#include<stdlib.h>
#include<stdio.h>
int nextPrime(int num)
{
if(num==1 || num ==0)
return 2;
num++;
int i;
int flag;
for(i=2;i<=num/2;++i)
{
if(num%i==0)
{
flag=0;
break;
}
}
if(flag==0)
return nextPrime(num);
else
return num;
}
struct Node
{
int data;
struct Node *link;
};
//typedef struct Node Node;
void append(struct Node *start,int value)
{
while(start->link != NULL)
{
start=start->link;
}
start->link = (struct Node *)malloc(sizeof(struct Node));
start=start->link;
start->data=value;
start->link=NULL;
}
void traverse(struct Node *start)
{
while(start != NULL)
{
printf("%d\n",start->data);
start=start->link;
}
}
void calculateFactors(int number)
{
int Den,reminder;
Den = nextPrime(0);
while(number > 1 )
{
reminder = number % Den;
if(reminder != 0)
Den=nextPrime(Den);
else
{
number=number / Den;
append(start,Den); // Error here. Start undeclared first use in function.
}
}
}
void main()
{
int number, i;
struct Node *head, *start;
head->link = NULL;
start=head;
scanf("%d",&number);
calculateFactors(number);
//append(start,10);
//traverse(head);
}
I was wondering why it shows such an error as I have already initialized the node in the main program. Should it not be available in the functions as well? Can someone explain how to fix this and the scope of variable in this context