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 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
That is exactly what I was moving towards :). Thanks for the help.
Tim 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/>
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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...
|
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
|
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
|
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;
...
| |
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
|
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
|
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.
|
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...
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |