473,396 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

caloc errors in compile

Hi, when i try to compile my code it comes up with two errors both in the same function.

The errors are both the same:

106assig-4.cpp invalid conversion from `void*' to `int**'

Here is my code:

Expand|Select|Wrap|Line Numbers
  1. int **setMem(int **matrixPointer, int rows, int columns)
  2. {
  3.     // Allocate memory for each row.
  4.     matrixPointer = calloc(rows, sizeof(int *)); //error here!!
  5.  
  6.     // Allocate memory for each columns within each row. 
  7.     for (int index1 = INIT; index1 < rows; index1++)
  8.     {
  9.                                 // error on line below 
  10.         matrixPointer[index1] = calloc(columns, sizeof(int));
  11.     }
  12.  
  13.     return matrixPointer;
  14. }
  15.  
matrixPointer is an array of arrays using double pointers (or 2D array). rows are the first arrays size and columns is the second. It is an integer array. the module is supposed to assign memory to my array using the pointer.

PLease could some one let me know how I can fix this!? I will provide the rest of my code if necessary. Please request any details required.
Oct 5 '07 #1
12 2176
JosAH
11,448 Expert 8TB
If you're compiling this as C everything should be fine as long as you've included
the <stdlibh> header file. If you're compiling this as C++ code you also have to
explicitly cast the return value of the calloc() function.

kind regards,

Jos
Oct 5 '07 #2
Ive got
#include <stdlb.h>

its only C not C++.. still wont compile..
Oct 5 '07 #3
syntax correction:

#include <stdlib.h>
Oct 5 '07 #4
how would you write the above lines in error for the same purpose?

Ive looked through books and manuals galore for the last few hours and ive found nothing wrong with my code.
Oct 5 '07 #5
sicarie
4,677 Expert Mod 4TB
Could you post the errors you are getting?
Oct 5 '07 #6
Could you post the errors you are getting?
they are posted above:

invalid conversion from void* to int**

thats all the information it gives me and that is for both sections I have marked in the above code.

any help is hugely appreciated.. im entirely stumped.. and ill admit a bit frustrated with the language. Im using the Bloodshed Dev-C++ compiler that was recommended to me on this forum if that is any help.
Oct 5 '07 #7
weaknessforcats
9,208 Expert Mod 8TB
matrixPointer = calloc(rows, sizeof(int *)); //error here!!
The error is that calloc is returning the addess of an int (int*) and matrixPointer is the address of an int pointer (int**). The compiler is producing the error because it knows that to allow the assignment will result in pointer aritmetic errors.

What you mean to say is:
Expand|Select|Wrap|Line Numbers
  1.  *matrixPointer = calloc(rows * sizeof(int)); 
  2.  
Oct 5 '07 #8
The error is that calloc is returning the addess of an int (int*) and matrixPointer is the address of an int pointer (int**). The compiler is producing the error because it knows that to allow the assignment will result in pointer aritmetic errors.

What you mean to say is:
Expand|Select|Wrap|Line Numbers
  1.  *matrixPointer = calloc(rows * sizeof(int)); 
  2.  
Thanks, I feel that I am getting closer to a solution. I copied and pasted this into my code but still no banana. it wouldnt compile saying that there was too many arguments for calloc. could you please put fill in a bit more on the syntax and Ill try it again? are there supposed to be commas somewhere in the calloc parametres (like: "*matrixPointer = calloc(rows, * sizeof(int));")?

my code as it stands now is:

Expand|Select|Wrap|Line Numbers
  1. int **setMem(int **matrixPointer, int rows, int columns)
  2. {
  3.     // Allocate memory for each row.
  4.     *matrixPointer = calloc(rows, sizeof(int));
  5.  
  6.     // Allocate memory for each columns within each row. 
  7.     for (int index1 = INIT; index1 < rows; index1++)
  8.     {
  9.         *matrixPointer[index1] = calloc(columns, sizeof(int));
  10.     }
  11.  
  12.     return matrixPointer;
  13. }
  14.  
Oct 5 '07 #9
im still trudging away with the code.. after the changes now I am getting a very similar message for the same code sections.

invalid conversion from void* to int*

I still dont understand..
Oct 6 '07 #10
Banfa
9,065 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1.  *matrixPointer = calloc(rows * sizeof(int)); 
  2.  
No he had it right originally wfc, note it is calloc NOT malloc and calloc takes 2 parameters, the number of items in the array and the size of each item.

Dameon99

You say you are using Dev-C++ are you absolutely sure you are not compiling for C++ because that would definitely produce the errors you are getting. Are you sure your file extension is .c, are you sure that there are no command line switches in use that are forcing compilation as C++ even though the file has a c extension (some compilers have switches for this). Are you sure that Dev-C++ is capable of compiling C (as opposed to C++).

It may be time to check your compiler documentation (if you have already done that of course).
Oct 6 '07 #11
Problem Fixed!!!!!

For some reason I had to typecast calloc to an int. I think it may have been compiling as a C++ file :-S. I just read in a guide on the net that you should always typecast it even in C.

Solution was (for those who are curious):

Expand|Select|Wrap|Line Numbers
  1.     *matrixPointer = (int *)calloc(rows, sizeof(int *));
  2.  
and

Expand|Select|Wrap|Line Numbers
  1.     *matrixPointer[index1] = (int)calloc(columns, sizeof(int));
  2.  
Thanks so much for everyones help :-D. Much appreciated.
Oct 6 '07 #12
JosAH
11,448 Expert 8TB
Casting the return value of calloc (a void*) to an int is dead wrong. Reread my
reply #2; the answer is already there.

kind regards,

Jos
Oct 6 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Adam Carmichael | last post by:
------=_NextPart_000_4BC8_01C34EFD.A4A62470 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hello, I am trying to report a bug, and I would like to...
1
by: Ravi Chaudhary | last post by:
Hi, We are using VS.Net 2003 and coding in VB.net. The solution has 38 projects; most of the projects in the solution reference other projects (without any circular references) and all the...
7
by: Holger (David) Wagner | last post by:
Hi Group, I've searched the Web for precompilers that compile ASPX/ASCX pages just like it can be done with JSPs, but so far, I've only found approaches targetted at increasing the performance....
1
by: Ravi Chaudhary | last post by:
Hi, We are using VS.Net 2003 and coding in VB.net. The solution has 38 projects; most of the projects in the solution reference other projects (without any circular references) and all the...
7
by: sophie | last post by:
Hi everyone, I've got an exam in c++ in two days and one of the past questions is as follows. Identify 6 syntax and 2 possible runtime errors in this code: class demo { private:
22
by: Tomás Ó hÉilidhe | last post by:
I've been developing a C89 microcontroller application for a while now and I've been testing its compilation using gcc. I've gotten zero errors and zero warnings with gcc, but now that I've moved...
9
by: beet | last post by:
Hi, I am really not good at c/c++. Some simple code.. #include <stdio.h> #include <stdlib.h> #include <math.h> #include "simlibdefs.h"
2
by: Andrus | last post by:
I need compile in-memory assembly which references to other in-memory assembly. Compiling second assembly fails with error Line: 0 - Metadata file 'eed7li9m, Version=0.0.0.0, Culture=neutral,...
1
by: =?Utf-8?B?SG93YXJkIFBpbnNsZXk=?= | last post by:
I'm trying to convert a Web Site to the Web Application project model and I'm running into compile errors that do not seem to be covered by the guidance I found at "Converting a Web Site Project to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.