473,799 Members | 2,997 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3550
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.*********@op en.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
10186
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 code... TCHAR myArray; DoStuff(myArray);
17
2274
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
2076
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 main(void) { char static_array_of_strings; fun ((char**)&static_array_of_strings, NUM_STRINGS);
11
4473
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 accomplish. // - - - - - - - - begin code - - - - - - - typedef int sm_t; typedef int bg_t; sm_t sm; bg_t bg;
4
6383
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 allocated a set of pointer to pointers as my multi-dimensional arrays. Here is my code (i have omitted checking calloc's return value to make this shorter): int **filter; filter = calloc( ylength, sizeof(int*) ); for( i = 0 ; i < ylength...
4
2506
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
8967
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 must be allocated (with known max length = 10) before the call to the dll function (the dll just fills it ,with no allocations). The definitions of Mystruct and :
8
3506
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
1479
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 dimension until runtime. I pass the pointer to a function which then determines the size and then creates and populates it. I can walk the array OK in the function, but the app crashes when I try to do so in the calling routine. Here's a small...
0
9687
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
9541
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
10485
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
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9073
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
7565
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
5463
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...
1
4141
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.