473,396 Members | 2,011 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,396 software developers and data experts.

Passing a contiguously allocated 2D array to a function expecting a 1 dimesnional array

Hi

I have a 2 dimensional array allocated as a contiguous chunk of
memory, as described in Question 6.16 in the FAQ (i.e. in the manner
of array 2 on the page http://www.eskimo.com/~scs/C-faq/q6.16.html).
I have a function declared as func(..., array[],...) and I wish to
pass my 2D array to that function. As I understand it, this should be
allowable since the 2D array is contiguous in memory, and if i
therefore call it as func(..., my2darray[0], ...) the data types
should match (double * in both cases). Is this the correct way to do
this?

Many thanks
Jonathan.
Nov 13 '05 #1
2 3533
Jonathan Underwood wrote:
Hi

I have a 2 dimensional array allocated as a contiguous chunk of
memory, as described in Question 6.16 in the FAQ (i.e. in the manner
of array 2 on the page http://www.eskimo.com/~scs/C-faq/q6.16.html).
I have a function declared as func(..., array[],...) and I wish to
pass my 2D array to that function. As I understand it, this should be
allowable since the 2D array is contiguous in memory, and if i
therefore call it as func(..., my2darray[0], ...) the data types
should match (double * in both cases). Is this the correct way to do
this?


Should work, but this makes it look like you are trying to conform to the
f2c version of Fortran 77, rather than programming to the style of any
programming language.
--
Tim Prince
Nov 13 '05 #2
"Jonathan Underwood" <j.*********@open.ac.uk> wrote:
I have a 2 dimensional array allocated as a contiguous chunk of
memory, as described in Question 6.16 in the FAQ (i.e. in the manner
of array 2 on the page http://www.eskimo.com/~scs/C-faq/q6.16.html).
I have a function declared as func(..., array[],...) and I wish to
pass my 2D array to that function. As I understand it, this should be
allowable since the 2D array is contiguous in memory, and if i
therefore call it as func(..., my2darray[0], ...) the data types
should match (double * in both cases). Is this the correct way to do
this?


Yes, this is fine with the definition as in FAQ 6.16:
int **array2 = (int **)malloc(nrows * sizeof(int *));
array2[0] = (int *)malloc(nrows * ncolumns * sizeof(int));

However, if it were a normal array with a definition like:
double my2darray[4][7];

The expression my2darray[0] has type 'double[7]' or 'array of 7 double'.
It is undefined behaviour to attempt to access beyond the 7th element
of my2darray[0], that is beyond my2darray[0][6], so you're not supposed
to access the whole of my2darray through my2darray[0].

This could generate problems on a really good bounds-checking
implementation of C. Most implementations won't catch it though.

--
Simon.
Nov 13 '05 #3

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

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
17
by: BlindHorse | last post by:
Help!!! I need someone to tell me why I am getting the err msg error C2440: '=' : cannot convert from 'char *' to 'char' //==================== #include <iostream>
11
by: MackS | last post by:
I've come across the following difficulty, related to questions 6.12, 6.13 and 6.18 in the FAQ, which I am unable to overcome: void fun(char **array_of_strings, int num_elements); int...
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
4
by: entitledX | last post by:
Hi, I'm trying to use the HDF library to read a few HDF files that I need to process. The data in each file varies in rows, but the columns remain constant. Because of that, I had dynamically...
4
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
6
by: =?Utf-8?B?QWxleGFuZGVyZmU=?= | last post by:
Hi, I have a C# program that uses an unmanaged dll that has a function similar to the signature below : void f(out MyStruct arr, out int num); // num = actual array length returned The array...
8
by: S. | last post by:
Hi all, Can someone please help me with this? I have the following struct: typedef struct { char *name; int age; } Student;
5
by: CapCity | last post by:
I'm sure I'm missing something simple - I do not code in C regularly, and the breaks are long enough for me to forget. The situation I have is I need to create an array but I do not know the...
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
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
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
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,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.