By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,770 Members | 1,856 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,770 IT Pros & Developers. It's quick & easy.

program that return an address of multi dimensional array

P: n/a
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

Feb 12 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
In article <11**********************@f14g2000cwb.googlegroups .com>,
ashu <ri*********@yahoo.com> wrote:
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.

--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton
Feb 12 '06 #2

P: n/a
On Sat, 11 Feb 2006 20:27:51 -0800, ashu wrote:
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.

--
Ben.

Feb 12 '06 #3

P: n/a
"ashu" <ri*********@yahoo.com> writes:
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/>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 12 '06 #4

P: n/a
ashu wrote:

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 */
--
pete
Feb 12 '06 #5

P: n/a
ashu wrote:
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 '=' */
}

Feb 12 '06 #6

P: n/a
On 11 Feb 2006 20:27:51 -0800, "ashu" <ri*********@yahoo.com> wrote:

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
Feb 13 '06 #7

P: n/a
In article <2i********************************@4ax.com>
Barry Schwarz <sc******@doezl.net> wrote:
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.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Feb 20 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.