Hi all,

I am trying to malloc some memory and then pass the address of the first array into another function however I keep getting the same error (see thread name).

Error:error C2664: cannot convert parameter 1 from 'float **__w64  ' to 'float *

Here is a short version of my code that repeats the same error.

void vGetPixelValueIntoArray(float *,const char*, s_Header*);

int main(int argc, char **argv) {
   

    float **ppfImageData = 0;/* a pointer to a series of 2d arrays */
	s_Filename *psFile;
	s_Header *psHeader;
	float *pfResFunc;
	size_t nCountI,nCountJ;

	if(!(ppfImageData = (float **) malloc(psHeader->nPhotosToBeCounted))){
        fprintf(stderr, "Failed to allocate memory for imageMatrix array!\n");
		return NULL;
	}

	nCountI = 0;
	vGetPixelValueIntoArray(&ppfImageData[nCountI],  "c:\testfile.ppm", psHeader); //parameter 1 that causes the problems

	for (nCountJ=0; nCountJ<psHeader->nPhotosToBeCounted; nCountJ++){
			free(ppfImageData[nCountJ]);
    }
    free(ppfImageData);
    return 0;
}
void vGetPixelValueIntoArray(float *pfImageData, const char* pccFilename, s_Header* psHeader ) {// returns NULL if error 
    size_t nCounter, nSecondCounter;
    FILE *pFTemp_file;
	//float *pfImage;

	psHeader->nWantedPixels = 5; //Hack to only take in certain amount of pixels
	if (!(pFTemp_file = fopen(pccFilename,"rb"))){ //open file
        fprintf(stderr, "Can't open file!\n");
        exit(-1);
    }
	if (!(pfImageData = (float *) malloc(psHeader->nWantedPixels))){ //malloc array for image data
        fprintf(stderr, "Failed to allocate memory in *pfGetPixelValueIntoArray");
        exit(-1); //cant return a null >???? need a error function
    }	
	for(nSecondCounter=0;nSecondCounter<psHeader->nHeaderEnd;nSecondCounter++){fgetc(pFTemp_file);}
    
	for (nCounter=0; nCounter<psHeader->nWantedPixels; nCounter++){
            pfImageData[nCounter] = (unsigned char) fgetc(pFTemp_file);
            //skip next two values are they make up the rgb pixel and are therefore not needed
			//only do this if there are pixels to jump over and if it is a 3 channel operation
			fgetc(pFTemp_file); 
			fgetc(pFTemp_file);
            if (feof(pFTemp_file)) break;
        }
    fclose(pFTemp_file);
	
}

What happens in function vGetPixelValueIntoArray is not really important as its the call line for this in Main that causes the error :(.

Any advice would be appreciated.

Recommended Answers

All 11 Replies

>Here is a short version of my code that repeats the same error
I just get a bunch of syntax errors. How about posting something complete that we can cut/paste and compile? By the way, your code as it is is filled with problems.

// OneChannelWin32.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "ctype.h"
using namespace std;


//float *pfGetPixelValueIntoArray(const char*, s_Header*);
void vGetPixelValueIntoArray(float *);

void vGetPixelValueIntoArray(float *pfImageData ) {// returns NULL if error 

	if (!(pfImageData = (float *) malloc(5))){ //malloc array for image data
        fprintf(stderr, "Failed to allocate memory in *pfGetPixelValueIntoArray");
        exit(-1); //cant return a null >???? need a error function
    }	
	
}

int main() {
   
	float **ppfImageData;/* a pointer to a series of 2d arrays */
	int photostobecounted=16, nCountJ=0;
	
	if(!(ppfImageData = (float **) malloc(photostobecounted))){
        fprintf(stderr, "Failed to allocate memory for imageMatrix array!\n");
		return NULL;
	}
		vGetPixelValueIntoArray(&ppfImageData[0]); //so mallocing the first

	// deallocate memory 
	for (nCountJ=0; nCountJ<photostobecounted; nCountJ++){
			free(ppfImageData[nCountJ]);
    }
    free(ppfImageData);
    return 0;
}

Hopefully that should compile[or at least give you the same error I am getting] for you, I shouldnt worry about the problems as I believe most of them were caused by me trying to shorten the code in the first place :S. Anyway have a go with this,

Cheers,
Giant

testpp.cpp:
Error E2209 testpp.cpp 3: Unable to open include file 'stdafx.h'
Error E2268 testpp.cpp 11: Call to undefined function 'malloc' in function vGetPixelValueIntoArray(float *)
Warning W8060 testpp.cpp 11: Possibly incorrect assignment in function vGetPixelValueIntoArray(float *)
Error E2268 testpp.cpp 12: Call to undefined function 'fprintf' in function vGetPixelValueIntoArray(float *)
Error E2451 testpp.cpp 12: Undefined symbol 'stderr' in function vGetPixelValueIntoArray(float *)
Error E2268 testpp.cpp 13: Call to undefined function 'exit' in function vGetPixelValueIntoArray(float *)
Warning W8057 testpp.cpp 16: Parameter 'pfImageData' is never used in function vGetPixelValueIntoArray(float *)
Error E2268 testpp.cpp 23: Call to undefined function 'malloc' in function main(int,char * *)
Error E2451 testpp.cpp 24: Undefined symbol 'stderr' in function main(int,char * *)
Error E2268 testpp.cpp 24: Call to undefined function 'fprintf' in function main(int,char * *)
Error E2451 testpp.cpp 25: Undefined symbol 'NULL' in function main(int,char * *)
Error E2034 testpp.cpp 27: Cannot convert 'float * *' to 'float *' in function main(int,char * *)
Error E2342 testpp.cpp 27: Type mismatch in parameter 'pfImageData' (wanted 'float *', got 'float * *') in function main(int,char * *)
Error E2268 testpp.cpp 31: Call to undefined function 'free' in function main(int,char * *)
Warning W8013 testpp.cpp 31: Possible use of 'ppfImageData' before definition in function main(int,char * *)
Warning W8057 testpp.cpp 35: Parameter 'argc' is never used in function main(int,char * *)
Warning W8057 testpp.cpp 35: Parameter 'argv' is never used in function main(int,char * *)
*** 12 errors in Compile ***

sorry bout that, forgot about header file, it _should_ work now (he said with vast amounts of hope), I editted my last post to incorporate some changes.

giant.

p.s. can i ask what development software/compiler/environments you use ?

vGetPixelValueIntoArray(&ppfImageData[0]); //so mallocing the first

Since &ppfImageData[0] is the same as ppfImageData, which is a float**, and since vGetPixelValueIntoArray receives a float*, do you instead mean to write this?

vGetPixelValueIntoArray(ppfImageData[0]); //so mallocing the first

[edit]And use angle brackets for standard headers.

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "ctype.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

[edit2]Why use so much C in a C++ source?

vGetPixelValueIntoArray(&ppfImageData[0]); //so mallocing the first

Since &ppfImageData[0] is the same as ppfImageData, which is a float**, and since vGetPixelValueIntoArray receives a float*, do you instead mean to write this?

vGetPixelValueIntoArray(ppfImageData[0]); //so mallocing the first

No, I don't, I want to pass the address of the first pointer. In the full program I go through all the malloced memory address but i was trying to isolate the problem so only passed in the first address, it could easily have been the 5th adress.

[edit2]Why use so much C in a C++ source?

I am using visual studio and thats how it saved it, it _is_ meant to be written in C, though I know some C++ may have slipped in as VS lets you away with it, it has to be noted at this point that I am fairly incompetent at programming (as I have only been using vs2003 and C/C++ for 6 weeks now), and am just coming back from a year out of anything remotely thought provoking :/.

vGetPixelValueIntoArray(&ppfImageData[0]); //so mallocing the first

Since &ppfImageData[0] is the same as ppfImageData, which is a float**, and since vGetPixelValueIntoArray receives a float*, do you instead mean to write this?

vGetPixelValueIntoArray(ppfImageData[0]); //so mallocing the first

No, I don't, I want to pass the address of the first pointer. In the full program I go through all the malloced memory address but i was trying to isolate the problem so only passed in the first address, it could easily have been the 5th adress.

I just found out this is cross-posted (Quzah gave you the same answer right away too).

Think about this some more:Since &ppfImageData[0] is the same as ppfImageData, which is a float**, and since vGetPixelValueIntoArray receives a float*

I am using visual studio and thats how it saved it, it _is_ meant to be written in C, though I know some C++ may have slipped in as VS lets you away with it, it has to be noted at this point that I am fairly imcompetent at programming (as I have only been using vs2003 and C/C++ for 6 weeks now), and am just coming back from a year out of anything remotely thought provoking :/.

Save it as a .c file so you can compile as C and save yourself from added confusion.

okay if I do just simply write

ppfImageData[0]

surely I am only passing a value to the function, rather than the address of first pointer, which I need.

Let me take a different tack. It seems like you are trying for something along this line.

#include <stdio.h>
#include <stdlib.h>

void foo(float **array2d, size_t index)
{
   array2d[index] = malloc(5 * sizeof *array2d[index]);
}

int main()
{
   size_t i, j, size = 16;
   float value = 0, **array2d = malloc(size * sizeof *array2d);
   if ( array2d )
   {
      for ( i = 0; i < size; ++i )
      {
         foo(array2d, i);
         if ( !array2d[i] )
         {
            fprintf(stderr, "Failed to allocate memory for array!\n");
            goto cleanup;
         }
         for ( j = 0; j < 5; ++j )
         {
            array2d[i][j] = ++value;
         }
      }
      for ( i = 0; i < size; ++i )
      {
         for ( j = 0; j < 5; ++j )
         {
            printf("array2d[%d][%d] = %g\n", (int)i, (int)j, array2d[i][j]);
         }
      }
   cleanup:
      for ( j = i, i = 0; i < j; ++i )
      {
         free(array2d[i]);
      }
      free(array2d);
   }
   return 0;
}

yeah I kind of see whats going on there, and that definatly solves my problem, I just wish I understood why I cant seem to pass the address...oh well such is life, time to move on and stop wasting your time.

Thanks for all your help, hopefully I wont have to bother you again ;) (yeah right....)
Giant.

If you tried to pass merely a float* you would be passing a copy of the pointer's address. Modifying the copy would not change the original.

In C if you want to change the value of an object passed to a function (by malloc'ing memory to it for example), you need to simulate pass-by-reference by passing a pointer to the object. If the object is itself a pointer, you will need to pass a pointer to a pointer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.