Also argument 2 makes integer from a pointer with a cast. One argument is a struct declared in an included library. It's declared like:

struct pixelst *lzwArr;

lzwArr = (struct pixelst *) malloc(pixels); // pixels is an int

the second argument is an unsigned int 2D array. It's used in a function to store hex numbers. It's declared like:

int dlzwHeight = (int)lzwArr[0].height; //cast to int
        int dlzwWidth = (int)lzwArr[0].width; //cast to int

        unsigned int dlzwArr[ dlzwHeight ][ dlzwWidth ];
        dlzwFun(lzwArr, dlzwArr); //line giving the error

The reason I cast the struct variables to int is because they start as hex numbers. Can you see the problem from the code here?

Recommended Answers

All 3 Replies

> The reason I cast the struct variables to int is because they start as hex numbers
This makes no sense at all.
What does this do?

int a = 10;
int b = 0xa;
printf( "%d %d\n", a, b );

The fact that the constant was written in hex, or was read from a string in hex and converted to an int doesn't make a bean of difference.

> unsigned int dlzwArr[ dlzwHeight ][ dlzwWidth ];
You appear to be trying to declare a variable sized array. If your compiler allows this (at all), it is non-standard behaviour.

> lzwArr = (struct pixelst *) malloc(pixels);
1. You cast the result of malloc. There is no need to do this if your C program is correct, and it hides problems if your C program is wrong.
2. The form should be lzwArr = malloc ( pixels * sizeof *lzwArr ); I'm guessing you allocated a hell of a lot less than you though you had, and have been trashing lots of memory as a result.

How would I malloc my 2D array dlzwArr with variables?

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.