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: -
int **setMem(int **matrixPointer, int rows, int columns)
-
{
-
// Allocate memory for each row.
-
matrixPointer = calloc(rows, sizeof(int *)); //error here!!
-
-
// Allocate memory for each columns within each row.
-
for (int index1 = INIT; index1 < rows; index1++)
-
{
-
// error on line below
-
matrixPointer[index1] = calloc(columns, sizeof(int));
-
}
-
-
return matrixPointer;
-
}
-
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.
12 2176
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
Ive got
#include <stdlb.h>
its only C not C++.. still wont compile..
syntax correction:
#include <stdlib.h>
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.
Could you post the errors you are getting?
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.
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: -
*matrixPointer = calloc(rows * sizeof(int));
-
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: -
*matrixPointer = calloc(rows * sizeof(int));
-
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: -
int **setMem(int **matrixPointer, int rows, int columns)
-
{
-
// Allocate memory for each row.
-
*matrixPointer = calloc(rows, sizeof(int));
-
-
// Allocate memory for each columns within each row.
-
for (int index1 = INIT; index1 < rows; index1++)
-
{
-
*matrixPointer[index1] = calloc(columns, sizeof(int));
-
}
-
-
return matrixPointer;
-
}
-
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..
Banfa 9,065
Expert Mod 8TB -
*matrixPointer = calloc(rows * sizeof(int));
-
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).
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): -
*matrixPointer = (int *)calloc(rows, sizeof(int *));
-
and -
*matrixPointer[index1] = (int)calloc(columns, sizeof(int));
-
Thanks so much for everyones help :-D. Much appreciated.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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....
|
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...
|
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:
|
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...
|
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"
|
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,...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
|
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,...
| |