caloc errors in compile

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dameon99
    New Member
    • Jun 2007
    • 57

    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:

    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.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    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

    Comment

    • Dameon99
      New Member
      • Jun 2007
      • 57

      #3
      Ive got
      #include <stdlb.h>

      its only C not C++.. still wont compile..

      Comment

      • Dameon99
        New Member
        • Jun 2007
        • 57

        #4
        syntax correction:

        #include <stdlib.h>

        Comment

        • Dameon99
          New Member
          • Jun 2007
          • 57

          #5
          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.

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Could you post the errors you are getting?

            Comment

            • Dameon99
              New Member
              • Jun 2007
              • 57

              #7
              Originally posted by sicarie
              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.

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Originally posted by Dameon99
                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:
                [code=c]
                *matrixPointer = calloc(rows * sizeof(int));
                [/code]

                Comment

                • Dameon99
                  New Member
                  • Jun 2007
                  • 57

                  #9
                  Originally posted by weaknessforcats
                  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:
                  [code=c]
                  *matrixPointer = calloc(rows * sizeof(int));
                  [/code]
                  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: "*matrixPoi nter = calloc(rows, * sizeof(int));") ?

                  my code as it stands now is:

                  [code=c]
                  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;
                  }
                  [/code]

                  Comment

                  • Dameon99
                    New Member
                    • Jun 2007
                    • 57

                    #10
                    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..

                    Comment

                    • Banfa
                      Recognized Expert Expert
                      • Feb 2006
                      • 9067

                      #11
                      Originally posted by weaknessforcats
                      [code=c]
                      *matrixPointer = calloc(rows * sizeof(int));
                      [/code]
                      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).

                      Comment

                      • Dameon99
                        New Member
                        • Jun 2007
                        • 57

                        #12
                        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):

                        Code:
                        	*matrixPointer = (int *)calloc(rows, sizeof(int *));
                        and

                        Code:
                        	*matrixPointer[index1] = (int)calloc(columns, sizeof(int));
                        Thanks so much for everyones help :-D. Much appreciated.

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          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

                          Comment

                          Working...