Advertisements

kaul wrote:
> i want to create a 2-d array containg r rows and c columns by dynamic
> memory allocation in a single statement so that i will be able to access
> the ith and jth index as say arr[j]
> how is that possible?
>
try this:

"kaul" <> writes:
> i want to create a 2-d array containg r rows and c columns by dynamic
> memory allocation in a single statement so that i will be able to access
> the ith and jth index as say arr[j]
> how is that possible?

The return from malloc is guaranteed to be properly aligned for any
type of variable, including int* and int.
> cursor = (int*)int_matrix + width;

This arithmetic is performed in units of sizeof(int). If sizeof(int)
is less than sizeof(int*), the value assigned to cursor will not leave
enough room for the "width" int* that are initialized in the next
loop.
> for (i = 0; i < width; i++) {
> int_matrix = cursor;
> cursor += height;
> }
> return int_matrix;
>}

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!