If I have this reference variable:
float* image
How can I get the length of this image in C? The length = (width * height) but how can I get this value?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Simply points at a single float. It can also be used with array syntax — ie
image[n]to get the nth float from the location of image. People will use pointers and malloc to create a dynamic array of floats. However, there’s no information associated with the size stored with the data itself. That’s a detail the coder has to care about in C. Often in these kinds of cases in C when you’re passed such a pointer in a function, you’re also passed an accompanying size, ie:It sounds like though that
imagemight point to a block of contiguous memory treated as a 2d array. That is something with a width and height. So instead of strictly a numberOfFloats you might have something like this “3×3” image:image -> 0 1 2 3 4 5 6 7 8 0 0 0 1 1 1 2 2 2There’s a total of 3×3 == 9 floats here with the first 3 corresponding to row 1, the next 3 row 2, and the last one as row 4. This is simply a single dimensional array used to represent 2d data.
Like I said before, this is a detail of how you’ve decided to use the language and not a part of it. You’ll have to pass along the width/height with the pointer for safe use:
You can also avoid this by simply creating a struct to store your image, being sure to always initialize/maintoin the width and height correctly
then always pass around the struct to functions like foo
Another trick could be to overallocate float to store the width and height in the buffer itself:
In any case, there’s these and many other implementation specific ways to store/pass this data. Whoever is giving you this pointer should be telling you how to get the length/height and telling you how to use the pointer. There’s no way C can figure out how it’s being done, its an implementation decision.