finding element width in dynamically allocated arrays

T

tricard

Good day all,

I have created a two dimensional array (matrix for my purposes) whose
size is dynamically allocated. (i.e. rowSize and colSize are both taken
as input, then malloc() is used to dynamically allocate the required
memory). After the matrix is returned to main I want to pass it to a
function, printMatrix() and have it displayed on screen. However, I do
not want to send the rowSize and colSize arguments; instead I want to
have the function identify the sizes. Is there any way to do this, or
should I just send the rowSize and colSize as arguments?

Thanks for the help

Tim
 
A

Antonio Contreras

Good day all,

I have created a two dimensional array (matrix for my purposes) whose
size is dynamically allocated. (i.e. rowSize and colSize are both taken
as input, then malloc() is used to dynamically allocate the required
memory). After the matrix is returned to main I want to pass it to a
function, printMatrix() and have it displayed on screen. However, I do
not want to send the rowSize and colSize arguments; instead I want to
have the function identify the sizes. Is there any way to do this, or
should I just send the rowSize and colSize as arguments?

Thanks for the help

There's not portable way to do what you want to do. At least no way
that I am aware of.

Passing colsize and rowsize as arguments is an option, but it's error
prone since you have to make sure that the three variables change
accordingly. What I would do is define a struct like:

struct dynamic_matrix {
size_t rowsize;
size_t colsize;
base_type **matrix;
}

HTH
 
C

CBFalconer

I have created a two dimensional array (matrix for my purposes) whose
size is dynamically allocated. (i.e. rowSize and colSize are both taken
as input, then malloc() is used to dynamically allocate the required
memory). After the matrix is returned to main I want to pass it to a
function, printMatrix() and have it displayed on screen. However, I do
not want to send the rowSize and colSize arguments; instead I want to
have the function identify the sizes. Is there any way to do this, or
should I just send the rowSize and colSize as arguments?

Since an array is passed as a pointer to its first element, there
is no way for a function to know the sizes unless specifically
passed them. So you must pass those values.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 
A

Abhishek

Or probably you could do that this way..
after u have assigned all the values in the matrix, you can assign a
totally different value(A value u think which is definitely not there
in the matrix) at the last position.
Inside the function to which u pass just the address of the first
element, you can scan through all the elements till you find the
representing element. once you have found the element, you know how
many elements it contains. but I am not sure if this technique can be
used for all the dimensions of the array. But I am sure it will work
for one dimension.
probably someone else can add more to this.
bye
 
V

Vladimir S. Oka

Abhishek said:
Or probably you could do that this way..

Do what? Who? (certainly not Chuck)
after u have assigned all the values in the matrix, you can assign a
totally different value(A value u think which is definitely not there
in the matrix) at the last position.
Inside the function to which u pass just the address of the first
element, you can scan through all the elements till you find the
representing element. once you have found the element, you know how
many elements it contains. but I am not sure if this technique can be
used for all the dimensions of the array. But I am sure it will work
for one dimension.
probably someone else can add more to this.
bye

It's highly unlikely that anyone can add anything to what you just said,
as it's not at all clear what are you talking about, and not even /who/
are you replying to. Many won't even try.

Also, using text-speak makes it /very/ difficult to read your post.

Please quote what (and who!) you're replying to. If you use Google, use
the instructions:

"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top