Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C++ pointer problem

Status
Not open for further replies.

rsshetty

Programmer
Dec 16, 2003
236
US
I wrote a recursive program in C.It works. Now I modifed it for C++ on Unix. I have been trying for ages.
I just can't get it to work. The problem is in the Print function.
Is there a mistake in the way Print is called recursively?


Strategy* Print(const int resalloc[],int nusers,Strategy *head)
{
int i;
Strategy *curr;
if(head == NULL)
{
head = (Strategy*)malloc(sizeof(Strategy));
head->next = NULL;
for(i = 0; i < nusers; i++)
{
head->com = resalloc;
}
printf("head == null\n");
}
else
{
/**************the next stmt is giving me trouble*********/
head->next = Print(resalloc,nusers,head->next);
}

printf("\n");

return(head);
}

Strategy *RecNum(int puser[],int nusers,int resalloc[],int icurr,Strategy *head)
{
int i,j;
int last, temp;
temp = icurr + 1;

if(temp == nusers)
last = 1;
else
last = 0;

printf("nusers %d icurr %d \n",nusers,icurr);

for(i = 0; i <= puser[icurr]; i++)
{
printf("puser[icurr] %d\n",puser[icurr]);
resalloc[icurr] = i;
if(last)
{
printf("in\n");
head = Print(resalloc,nusers,head);
}
else
{
head = RecNum(puser,nusers,resalloc,icurr+1,head);
}
}
return(head);
}

void generateStrategy(ResourceCenter *&r, Crisis *&c, int &cri_num, int &res_num)
{
Strategy *set;
Strategy *current;
int i;
int num;
int *p, *res;
int count = 0;

p = (int*)malloc(sizeof(int)*res_num);
res = (int*)malloc(sizeof(int)*res_num);

for(int i = 0; i < res_num; i++)
{
res = r.res_left;
}


for(int j = 0; j < cri_num; j++)
{

set = RecNum(res,res_num,p,0,current);

c[j].first = set;
}
}


rsshetty.


It's always in the details.
 
It's alright. I figured it out. I had failed to declare the first node NULL.

Thanks.

rsshetty.

It's always in the details.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top