# Amicable Numbers

Page 1 of 1

## 5 Replies - 4750 Views - Last Post: 06 November 2009 - 05:39 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=137113&amp;s=e18cbc42ca308ed44ce418ae79bd724a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 csbvba

Reputation: 0
• Posts: 15
• Joined: 28-October 09

# Amicable Numbers

Posted 06 November 2009 - 11:09 AM

hello guys,i am having problem with this statement.It is giving an error "Invalid conversion from ‘void*’ to ‘int**’ "
Plz help me to debug this error......
#include<stdio.h>
#include<stdlib.h>
#include "amicablenumber.h"

struct amicable *getAmicablePairs(int startnum,int endnum)
{
//struct amicable *record;
int i,size=0,pair1,pair2;
struct amicable *record=(struct amicable*)malloc(sizeof(struct amicable));
[b] record->amicablePair=malloc(sizeof(int*));[/b]
for(i=0;i<2;i++)
record->amicablePair[i]=(int*)malloc(sizeof(int));

int sumofdivisors(int number);

for(i=startnum;i<=endnum;i++)
{
pair1=sumofdivisors(i);
pair2=sumofdivisors(pair1);

if((i==pair2) && (pair1>i))
{
record->amicablePair[size][0]=i;
record->amicablePair[size][1]=pair1;
size++;
}
}
record->size=size;
return record;
}

int sumofdivisors(int number)
{
int k,sum=0;

for(k=1;k<number;k++)
{
if(number%k==0)
sum +=k;
}
return sum;
}

int main()
{
// test case 1
{
int startnum = 100;
int endnum = 2000;
struct amicable *ami;//=(struct amicable*)malloc(sizeof(struct amicable));

ami = getAmicablePairs(startnum, endnum);

printf("{");
for(int i = 0; i<ami->size; i++)
{
printf("{%d, %d}",ami->amicablePair[i][0], ami->amicablePair[i][1]);
}
printf("}");

}
}
where the header file amicablenumber.h contains

#define C_OR_CPP 1 //  Change value to 1 here if you are submitting a C solution for this problem.
//  Change value to 2 here if you are submitting a C++ solution for this problem.

[b]struct amicable			// Dont change here
{
int **amicablePair;
int size;
};[/b]

void dsmain();

#if C_OR_CPP == 1  // Dont change here

struct amicable *getAmicablePairs(int startnum, int endnum);

#endif

#if C_OR_CPP == 2  // Dont change here

class AmicableNumber
{
public:
struct amicable *getAmicablePairs(int startnum, int endnum);
};

#endif

I am getting an error as :: Invalid conversion from ‘void*’ to ‘int**’
Plz help........

This post has been edited by csbvba: 06 November 2009 - 11:32 AM

Is This A Good Question/Topic? 0

## Replies To: Amicable Numbers

### #2 hackterr

• D.I.C Regular

Reputation: 21
• Posts: 293
• Joined: 13-August 09

## Re: Amicable Numbers

Posted 06 November 2009 - 11:12 AM

dereference it correctly.

### #3 csbvba

Reputation: 0
• Posts: 15
• Joined: 28-October 09

## Re: Amicable Numbers

Posted 06 November 2009 - 11:20 AM

hackterr, on 6 Nov, 2009 - 10:12 AM, said:

dereference it correctly.

When it is not allocating memory then how to derefernce ???

csbvba, on 6 Nov, 2009 - 10:17 AM, said:

hackterr, on 6 Nov, 2009 - 10:12 AM, said:

dereference it correctly.

When it is not allocating memory then how to derefernce ???

Which part of the program is giving that error ???

### #4 csbvba

Reputation: 0
• Posts: 15
• Joined: 28-October 09

## Re: Amicable Numbers

Posted 06 November 2009 - 11:27 AM

csbvba, on 6 Nov, 2009 - 10:20 AM, said:

hackterr, on 6 Nov, 2009 - 10:12 AM, said:

dereference it correctly.

When it is not allocating memory then how to derefernce ???

csbvba, on 6 Nov, 2009 - 10:17 AM, said:

hackterr, on 6 Nov, 2009 - 10:12 AM, said:

dereference it correctly.

When it is not allocating memory then how to derefernce ???

Which part of the program is giving that error ???

DO you know it or not ???

• Saucy!

Reputation: 6246
• Posts: 24,014
• Joined: 23-August 08

## Re: Amicable Numbers

Posted 06 November 2009 - 12:40 PM

record->amicablePair=malloc(sizeof(int*));

You're compiling this as a C++ program, so you need to cast the return of malloc (malloc returns void *, and C++ is a strongly-typed language):
record->amicablePair=(int **)malloc(sizeof(int *));

### #6 csbvba

Reputation: 0
• Posts: 15
• Joined: 28-October 09

## Re: Amicable Numbers

Posted 06 November 2009 - 05:39 PM

JackOfAllTrades, on 6 Nov, 2009 - 11:40 AM, said: