473,387 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

finding element width in dynamically allocated arrays

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

Jan 23 '06 #1
5 1774
tr*****@gmail.com wrote:
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

Jan 23 '06 #2
That is exactly what I was moving towards :). Thanks for the help.

Tim

Jan 23 '06 #3
tr*****@gmail.com wrote:

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/>
Jan 23 '06 #4
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

Jan 23 '06 #5
Abhishek wrote:
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/>

--
My mother loved children -- she would have given anything if I had been
one.
-- Groucho Marx

Jan 23 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: songfire | last post by:
Hi everybody! Just wondering if it is possible to point to variables in the heap. For example, is this okay? int * ptr_to_DAIA; // pointer to dynamically allocated integer array ptr_to_DAIA =...
4
by: andres obrero | last post by:
i want to resize dynamically table cols. But some strange behaviour occurs under mozilla. I cannot resize a col smaller than the largest element, even with overflow hidden and table-layout fixed....
9
by: Luke Wu | last post by:
Hello, I'm having some problems understanding 2 dimensional arrays. My problem relates to the following code: #include <stdio.h> #define M 3 #define N 3
10
by: junky_fellow | last post by:
What is the correct way of dynamically allocating a 2d array ? I am doing it the following way. Is this correct ? #include <stdlib.h> int main(void) { int (*arr)(3); arr =...
37
by: yogpjosh | last post by:
Hello All, I was asked a question in an interview.. Its related to dynamically allocated and deallocated memory. eg. //start char * p = new char; ...
7
by: Serpent | last post by:
The C-FAQ describes some techniques here: http://c-faq.com/aryptr/dynmuldimary.html I was using something slightly different from the C-FAQ and I was wondering if it was legal. Say I want a...
1
by: SneakyElf | last post by:
hi all, im super new with c++ (and no background in programming whatsoever!) i have a task to make functions to calculate total profit and the average number of things sold. data is read from a...
15
by: Sunny | last post by:
Hi, I can change the lement opacity in IE using. abc.style.filter = 'alpha(opacity=' + 10 + ')'; But this dont work in firefox, In firefox it throws error. How I can change the opacity of an...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.