473,386 Members | 1,741 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,386 software developers and data experts.

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 2075
In article <11**********************@y42g2000hsy.googlegroups .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
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...
8
by: chook | last post by:
How i can call my function with some parameters, if i have a pointer to this function
4
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...
3
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...
27
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...
7
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*);...
3
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'...
12
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
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.