Hello all I have this unique problem that I cant figure out. I am trying to rewrite this script in c from a matlab code. In the function I have this:

Code:
function [nbBytes, matlabType] = convertType(tiffType)
switch (tiffType)
    case 1
        nbBytes=1;
        matlabType='uint8';
    case 2
        nbBytes=1;
        matlabType='uchar';
    case 3
        nbBytes=2;
        matlabType='uint16';
    case 4
        nbBytes=4;
        matlabType='uint32';
    case 5
        nbBytes=8;
        matlabType='uint32';
    case 11
        nbBytes=4;
        matlabType='float32';
    case 12
        nbBytes=8;
        matlabType='float64';
    otherwise
        error('tiff type %i not supported', tiffType)
end
return;

%===================sub-functions that reads an IFD entry:===================

function  entry = readIFDentry()

global TIF;
entry.tiffType = fread(TIF.file, 1, 'uint16', TIF.BOS);
entry.cnt      = fread(TIF.file, 1, 'uint32', TIF.BOS);
disp(['tiffType =', num2str(entry.tiffType),', cnt = ',num2str(entry.cnt)]);

[ entry.nbBytes, entry.matlabType ] = convertType(entry.tiffType);

if entry.nbBytes * entry.cnt > 4
    %next field contains an offset:
    offset = fread(TIF.file, 1, 'uint32', TIF.BOS);
    %disp(strcat('offset = ', num2str(offset)));
    fseek(TIF.file, offset, -1);
end

if TIF.entry_tag == 33629   %special metamorph 'rationals'
    entry.val = fread(TIF.file, 6*entry.cnt, entry.matlabType, TIF.BOS)
else
    if entry.tiffType == 5
        entry.val = fread(TIF.file, 2*entry.cnt, entry.matlabType, TIF.BOS);
        disp(strcat('Val 1 = ', num2str(entry.val)));
    else
        entry.val = fread(TIF.file, entry.cnt, entry.matlabType, TIF.BOS)
        disp(strcat('Val 2 = ', num2str(entry.val)));
    end
end
if ( entry.tiffType == 2 ); entry.val = char(entry.val');end

return;
You can see how for every entry we read, we get a cnt and type and depending on the type we fread certain number of bytes into dynamically allocated memory into the same variable entry.val. This is the problem in c is that how do I read the data (cnt*n) into the memory ? If I just difine entry.val as long, I get seg fault. Please help

P.S. If you need to take a look at the entire matlab code, here it is.

http://www.cytosim.org/other/tiffread.m

Thanks.