473,804 Members | 3,396 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1821
tr*****@gmail.c om 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.c om 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.c om, 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.c om, 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
1380
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 = new int ; for(i=0; i<SIZE; i++) ptr_to_DAIA=i; // now say I want a pointer that points to the element that contains value TARGET
4
2919
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. With one exception: Inserting a initial css table-width like 10px in the html. In this case i can resize to smaller sizes. I can live with that (need to calc all td widths, set each and then put the table to 1px), but why can't i add this table...
9
2319
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
2599
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 = malloc(sizeof(*arr) * 4); /* I want to dynamically allocate
37
2793
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
8733
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 two-dimensional array, like this: int x; but I want it dynamically-allocated, and I want expressions that refer
1
1360
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 file. my problem is with functions, namely setting up parameters and later calling functions in the main(). here are the two functions (?) that i wrote so far, one is supposed to get total profit and the other calculate average
15
3731
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 element in Firefox.
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10577
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10332
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9150
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7620
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5521
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.