473,811 Members | 2,979 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

function arguments and pointers revisited

Hi Everybody,

I am having trouble with a fairly simple problem, I am sure.

I have a structure that contains 2D arrays:

struct S {
float array[4][4];
.....
snip
.....
} *ptr;

and a function: int Trans(float data_array) {

int i, j, jj;
float array[16];

for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
array[jj] = data_array[i][j];
}
}
return 1;
}

I want to pass the array to the function like this:
res = Trans(ptr->array);

For brevity, I have not included any error checking in the call.

This way gives me a pointer type error and I don't want to send the
entire structure to the function.

Does anyone how to quickly do this?

Appreciate your help.
/S

Nov 7 '07 #1
4 2090
In article <11************ **********@y42g 2000hsy.googleg roups.com>,
Sheldon <sh******@gmail .comwrote:
>Hi Everybody,

I am having trouble with a fairly simple problem, I am sure.

I have a structure that contains 2D arrays:

struct S {
float array[4][4];
....
snip
....
} *ptr;

and a function: int Trans(float data_array) {

int i, j, jj;
float array[16];

for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
array[jj] = data_array[i][j];
}
}
return 1;
}

I want to pass the array to the function like this:
res = Trans(ptr->array);

For brevity, I have not included any error checking in the call.

This way gives me a pointer type error and I don't want to send the
entire structure to the function.

Does anyone how to quickly do this?
<http://www.c-faq.com/aryptr/pass2dary.html>
(Short answer: "int Trans(float data_array[][4])")

Whether this is in fact the best way to do it may depend on what you're
Really Trying To Do.
If the reason you don't want to pass the entire structure to the
function is because you don't want to have to copy a bunch of data
around, all you need to pass is a pointer:
--------
int trans(struct S *data)
{
/*...*/
array[jj]=data->array[i][j];
/*...*/
}
--------
The struct itself doesn't need to be copied; it stays where it is and
the callee accesses it through the pointer. (But this won't help you
much if the reason you don't want to pass the entire structure is
because it doesn't make sense to do so.)
dave

Nov 7 '07 #2
Sheldon wrote On 11/07/07 11:44,:
Hi Everybody,

I am having trouble with a fairly simple problem, I am sure.

I have a structure that contains 2D arrays:

struct S {
float array[4][4];
....
snip
....
} *ptr;

and a function: int Trans(float data_array) {
ITYM `float data_array[4][4]'.

--
Er*********@sun .com
Nov 7 '07 #3
On Nov 7, 10:44 am, Sheldon <shejo...@gmail .comwrote:
Hi Everybody,

I am having trouble with a fairly simple problem, I am sure.

I have a structure that contains 2D arrays:

struct S {
float array[4][4];
....
snip
....

} *ptr;

and a function: int Trans(float data_array) {
This prototype expects data_array to be a single float, not a 2-d
array of float.

Remember that when you pass an array as an argument, what actually
gets passed is a pointer to the first element of the array. Let's
start with a 1-d example:

float array[4];

The type of "array" is "4-element array of float". In most contexts
(such as passing the array as a parameter to a function), the type is
implicitly converted to "pointer to float", and what actually gets
passed is a pointer to the first array element:

void func(float *array) {...}
int main(void)
{
float array[4];
...
func(array);
...
}

Now let's look at a 2-d example:

float array[4][4];

the type of "array" is "4-element array of 4-element array of float".
When passed to a function, the type is converted to "pointer to a 4-
element array of float", so your prototype needs to be

int trans(float (*data_array)[4])
{
}

Honestly, you save yourself some headaches by passing a pointer to the
whole struct, rather than trying to pass a multi-dimensional array
object.
Nov 7 '07 #4
On 7 Nov, 18:56, John Bode <john_b...@my-deja.comwrote:
On Nov 7, 10:44 am, Sheldon <shejo...@gmail .comwrote:
Hi Everybody,
I am having trouble with a fairly simple problem, I am sure.
I have a structure that contains 2D arrays:
struct S {
float array[4][4];
....
snip
....
} *ptr;
and a function: int Trans(float data_array) {

This prototype expects data_array to be a single float, not a 2-d
array of float.

Remember that when you pass an array as an argument, what actually
gets passed is a pointer to the first element of the array. Let's
start with a 1-d example:

float array[4];

The type of "array" is "4-element array of float". In most contexts
(such as passing the array as a parameter to a function), the type is
implicitly converted to "pointer to float", and what actually gets
passed is a pointer to the first array element:

void func(float *array) {...}
int main(void)
{
float array[4];
...
func(array);
...
}

Now let's look at a 2-d example:

float array[4][4];

the type of "array" is "4-element array of 4-element array of float".
When passed to a function, the type is converted to "pointer to a 4-
element array of float", so your prototype needs to be

int trans(float (*data_array)[4])
{
}

Honestly, you save yourself some headaches by passing a pointer to the
whole struct, rather than trying to pass a multi-dimensional array
object.
You are right, it would be much easier to send in the entire struct
but that would require rewriting someone else's code and I don't have
time to do that. I tried prototype as you mentioned and it works. I
appreciate the help everyone. Thanks a lot!

/S :)

Nov 7 '07 #5

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

Similar topics

1
6741
by: Alex | last post by:
Is there any problem with sending function pointers through in a variable argument list? I have a function like the following: typedef (*ptr2FuncType1)( int ); typedef (*ptr2FuncType2)( double ); void SomeClass::DoSomething( unsigned int num_something, unsigned long num_something_else, ptr2FuncType1 handler_one, ... ) The functions I am passing in are in a different cpp file, but they
8
2235
by: chook | last post by:
How i can call my function with some parameters, if i have a pointer to this function
4
3636
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's useful to help me to solve some basic problem which I may not perceive before. I appreciate your help, sincerely.
3
1827
by: Randy Yates | last post by:
Hi, We know we can build arrays of variables of the same type and arrays of functions of the same "type" (i.e., same return value and same parameters), but is there a way to automate the calling of a sequence of functions with arbitrary return types and/or parameters? -- Randy Yates Sony Ericsson Mobile Communications Research Triangle Park, NC, USA
27
2496
by: Marlene Stebbins | last post by:
I am experimenting with function pointers. Unfortunately, my C book has nothing on function pointers as function parameters. I want to pass a pointer to ff() to f() with the result that f() prints the return value of ff(). The code below seems to work, but I would appreciate your comments. Have I got it right? Does the function name "decay" to a pointer? #include <stdio.h> /* declares a function which takes an argument that is a...
7
1856
by: Alfonso Morra | last post by:
How can this work ? I have the following declarations in a header: Header ========= typedef void (*VFPTR)(void) ; void FOO_Callback(void*, char*, char*, int, unsigned long, void*,void*); int bar(int *, char *, VFPTR, void *);
3
3661
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules' that takes arguments of type (void *) because the ADT must be able to deal with any type of data. In my actual code, I will code the function to take arguments of their real types, then when I pass this pointer through an interface function, I...
12
7876
by: claudiu | last post by:
Hi, I'll go straight to the first question. Why does the code below compile? void f(int i = 0); int main() { (&f)();
18
2648
by: mdh | last post by:
May I ask the following. By K&R's own admission, the example used to describe function pointers is complex ( on P119). In addition, the use of casts has been stated by some on this group as being, again, a poor/bad example of it's use. For the moment, accepting these criticisms, I would still like to get some insight into why/how some things work as even poor code is enlightening, to me at least. The example uses K&R's version of...
0
9604
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
10644
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
10127
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...
1
7665
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
6882
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5552
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
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
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.