Amicable Numbers

Getting an error Invalid conversion from ‘void*’ to ‘int**’

Page 1 of 1

5 Replies - 3691 Views - Last Post: 06 November 2009 - 05:39 PM Rate Topic: -----

#1 csbvba  Icon User is offline

  • New D.I.C Head

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

Amicable Numbers

Post icon  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  Icon User is offline

  • D.I.C Regular

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

Re: Amicable Numbers

Posted 06 November 2009 - 11:12 AM

dereference it correctly.
Was This Post Helpful? 0
  • +
  • -

#3 csbvba  Icon User is offline

  • New D.I.C Head

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

Re: Amicable Numbers

Posted 06 November 2009 - 11:20 AM

View Posthackterr, on 6 Nov, 2009 - 10:12 AM, said:

dereference it correctly.


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

View Postcsbvba, on 6 Nov, 2009 - 10:17 AM, said:

View Posthackterr, 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 ???
Was This Post Helpful? 0
  • +
  • -

#4 csbvba  Icon User is offline

  • New D.I.C Head

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

Re: Amicable Numbers

Posted 06 November 2009 - 11:27 AM

View Postcsbvba, on 6 Nov, 2009 - 10:20 AM, said:

View Posthackterr, on 6 Nov, 2009 - 10:12 AM, said:

dereference it correctly.


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

View Postcsbvba, on 6 Nov, 2009 - 10:17 AM, said:

View Posthackterr, 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 ???
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6091
  • View blog
  • Posts: 23,605
  • Joined: 23-August 08

Re: Amicable Numbers

Posted 06 November 2009 - 12:40 PM

So is this your problem?
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 *));

Was This Post Helpful? 0
  • +
  • -

#6 csbvba  Icon User is offline

  • New D.I.C Head

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

Re: Amicable Numbers

Posted 06 November 2009 - 05:39 PM

View PostJackOfAllTrades, on 6 Nov, 2009 - 11:40 AM, said:

So is this your problem?
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 *));


Sorry bro that code is also not working now i am getting new error as " Your code has stopped its execution with a non-zero (failure) exit value.This is generally due to run time Exceptions like Memory Access Violation and Floating Point Exception. Please check your code for run time Exceptions and try again. "
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1