program that return an address of multi dimensional array

A

ashu

look at code
#include<stdio.h>
int *mult(void);
int main(void)
{
int *ptr,i;
ptr=mult;
for(i=0;i<6;i++)
{
printf("%d",*(ptr++));
}
}

int *mult(void)
{
int ar[2][3]={1,2,3,4,5,6};
return = (int*) ar;
}
this program will not work, actually i am unable to reurn the address
of multi dimensional array
 
W

Walter Roberson

look at code
#include<stdio.h>
int *mult(void);

That declares that mult is a function taking no arguments and
returning a pointer to an int .
int main(void)
{
int *ptr,i;

That declares that ptr is a pointer to an int .
ptr=mult;

That tries to set ptr (a pointer to an int) to the address of
the mult() function itself (i.e., a pointer to a function that
returns a pointer to an int). This is a type conflict and your
compiler probably complained about this.
for(i=0;i<6;i++)
{
printf("%d",*(ptr++));
}
}

int *mult(void)
{
int ar[2][3]={1,2,3,4,5,6};

This declares an automatic variable named ar as a 2D array of int,
and initializes it to some values.
return = (int*) ar;

This takes the address of ar (a 2D array of int), reinterprets the
type as being a pointer to int, and returns that value.
}
this program will not work, actually i am unable to reurn the address
of multi dimensional array

You aren't calling mult, you are only referring to it. And if you
did call it, you would be trying to return from it the address
of an automatic variable -- an address that would go out of scope
and so be invalid as soon as the mult() function returned. Never
return the address of an automatic variable -- at least not
in any context where you intended to try to access storage there.
 
B

Ben Bacarisse

look at code
OK

return = (int*) ar;

return = ??

This is a small point, include it when considering the other useful advice
I see has been posted already.
 
K

Keith Thompson

ashu said:
look at code
#include<stdio.h>
int *mult(void);
int main(void)
{
int *ptr,i;
ptr=mult;
for(i=0;i<6;i++)
{
printf("%d",*(ptr++));
}
}

int *mult(void)
{
int ar[2][3]={1,2,3,4,5,6};
return = (int*) ar;
}
this program will not work, actually i am unable to reurn the address
of multi dimensional array

Please post the actual code that you compiled (cut-and-paste, don't
re-type). The code you posted contains a syntax error; the "return ="
should be just "return".

And don't just tell us it "will not work"; tell us *how* it doesn't
work. Does your program fail to compile? Do you get any diagnostics
from the compiler? Does the program crash at runtime? Do you get
unexpected output, and if so, what output did you expect?

Assuming that's the only difference between your actual code and the
code you posted, your compiler should have given you some warnings.
If it didn't, use whatever options your compiler requires to enable
more warnings, and pay attention to them.

You have
ptr=mult;
A function name not followed by parentheses evaluates the function
name and yields a function pointer; it *doesn't* call the function.
You want
ptr = mult();
(Perhaps that's what you had in your original program, but we can't
tell because you didn't post it.)

Inside your mult function, you declare a two-dimensional array "ar".
The initialization is ok, but it would be better to be more explicit:
int ar[2][3] = {{1,2,3}, {4,5,6}};

Note the additional whitespace; it makes the code much easier to read.

In the return statement (ignoring the "="), the expression ar (an
array name) is implicitly converted to a pointer to its first element;
in this case, it's a pointer to an array of 3 ints. You then use a
cast to convert this to a pointer-to-int.

Any cast should arouse suspicion. A cast, aside from specifying a
type conversion, tells the compiler "Don't bother me, I know what I'm
doing" -- but casts are too often used by programmers who *don't* know
what they're doing. Pointer casts are particularly problematic.

It looks like you're trying to treat a multidimensional 2-by-3 array
as if it were a one-dimensional array with 6 elements. This is
*probably* ok, but it's an odd thing to do. I can't tell from your
posted code why you can't just declare a one-dimensional array in the
first place.

Finally (and this is the most serious problem in your code, apart from
the syntax error), you attempt to return the address of a local
variable. As soon as the function returns, that local variable no
longer exists, and the caller has a dangling pointer value; any
attempt to deference that pointer value, or even examine it, invokes
undefined behavior. See questions 7.5a and 7.5b in the comp.lang.c
FAQ, <http://www.c-faq.com/>.
 
P

pete

ashu said:
look at code
#include<stdio.h>
int *mult(void);
int main(void)
{
int *ptr,i;
ptr=mult;
for(i=0;i<6;i++)
{
printf("%d",*(ptr++));
}
}

int *mult(void)
{
int ar[2][3]={1,2,3,4,5,6};
return = (int*) ar;
}
this program will not work, actually i am unable to reurn the address
of multi dimensional array

It's actually undefined to step through two consective arrays
with any kind of pointer except a pointer to a char type,
even though attempting to do that, works on my machine.

/* BEGIN new.c */

#include<stdio.h>

int *mult(void);

int main(void)
{
int *ptr, i;

ptr = mult();
for (i = 0; i < 6; i++) {
printf("%d", *(ptr++));
}
putchar('\n');
return 0;
}

int *mult(void)
{
static int ar[] = {1, 2, 3, 4, 5, 6};

return ar;
}

/* END new.c */
 
M

Martin Ambuhl

ashu said:
look at code
#include<stdio.h>
int *mult(void);
int main(void)
{
int *ptr,i;
ptr=mult;
for(i=0;i<6;i++)
{
printf("%d",*(ptr++));
}
}

int *mult(void)
{
int ar[2][3]={1,2,3,4,5,6};
return = (int*) ar;
}
this program will not work, actually i am unable to reurn the address
of multi dimensional array

There are enough things wrong with your small program to suggest that
you need to start reading your C-book from the beginning. Pay attention
this time.
I'm not at all happy with return the address of an int[2][3] as a int *,
but check the following:

#include <stdio.h>
int *mult(void);
int main(void)
{
int *ptr, i;
ptr = mult(); /* mha: note '()' */
for (i = 0; i < 6; i++) {
printf("%d", *(ptr++));
}
putchar('\n'); /* mha: note EOL char at EOL */
return 0; /* mha: returning a value from a
function promising to return a value
*/
}

int *mult(void)
{
static int ar[2][3] = { {1, 2, 3}, {4, 5, 6} };
/* mha: note 'static' */

return (int *) ar; /* mha: note no '=' */
}
 
B

Barry Schwarz

snip non-working code
this program will not work, actually i am unable to reurn the address
of multi dimensional array

To return the address of a multi-dimensional array, you need:

1 - A function with the correct return type.
2 - A return statement that returns the correct type
3 - Some insurance that the array will remain in existence
after the function returns

It is probably desirable to have:

4 - A variable in the calling function to receive the type

If you typedef array_t to the type of array whose address you want to
return, as in the example
typedef int array_t[5][7][93];
then you can use
array_t* function(...); /* function prototype */
and
array_t* function(...){
static array_t x; /* array exists forever */
...
return &x;
} /* function definition */
and
array_t *ptr;
...
ptr = function(...); /* in calling function */

Since you specified returning the address of the array, return x in
function would not be appropriate since it returns a pointer to the
first element of the array (which has the same address but the wrong
type).

If you don't use the typedef, you can get some pretty ugly
declarations such as
int (*)[5][7][93] function(...); /* prototype */


Remove del for email
 
C

Chris Torek

To return the address of a multi-dimensional array ...
[snip a bunch of correct stuff]
If you don't use the typedef, you can get some pretty ugly
declarations such as
int (*)[5][7][93] function(...); /* prototype */

Minor but significant syntactic correction here: you mean:

int (*function(args))[5][7][93];

(I prefer to wrap complicated array-of-array types in structures
when dealing with code like this. Using "struct" not only gives
you a user-defined abstract type -- the keyword does stand for
STRange spelling for User-defined abstraCt Type, after all :) --
it also avoids the need for a typedef-alias.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top