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

returning function pointer

Hi,
in this program i cannot retrive the values from the pointer j....
The code :-

--------------------------------------------------------------------------------------------------------------

#include <stdio.h>

void func(int [], int);
int*func2();

void main()
{
int a[] = {3,2,4};

int*j[8];
int i;

func(a, 2);

*j = func2();
printf("\nTraversing second list\n");

printf("\n%d", *j);

}

void func(int a[], int size)
{
int i=0;
for (i=0; i<=size; i++)
printf("%d\n", a[i]);
}

int*func2()
{

int b[] = {112,3,4,5,6,7,9,4,10};

return *b;
}

-------------------------------------------------------------------------------------------------------------

Any help

Thanks
Pradyut
http://pradyut.tk
India
Jun 26 '07 #1
10 2530
Pradyut Bhattacharya wrote:
Hi,
in this program i cannot retrive the values from the pointer j....
The code :-

----------------------------------------------------------------------
----------------------------------------

#include <stdio.h>

void func(int [], int);
int*func2();

void main()
main() returns int ALWAYS.
{
int a[] = {3,2,4};

int*j[8];
int i;

func(a, 2);
The number of elements in your array is three. Why pass two?
>
*j = func2();
printf("\nTraversing second list\n");

printf("\n%d", *j);
You're obviously not traversing, but accessing the first element.
>
}

void func(int a[], int size)
{
int i=0;
for (i=0; i<=size; i++)
Change that to i < size
printf("%d\n", a[i]);
}

int*func2()
{

int b[] = {112,3,4,5,6,7,9,4,10};

return *b;
Yikes. You pass back an int from a function that's supposed to be
returning an int *. Presumbably you meant to type:

return b;

However, that's not right either. b is a local array, you can NOT use
it outside of its scope, which happens to be the function func2().

You'll either have to dynamically allocate an array and populate it, or
make b static.

Any help
Turn up the warning level on your compiler.


Brian
Jun 26 '07 #2
Default User wrote:
>
Pradyut Bhattacharya wrote:
Hi,
in this program i cannot retrive the values from the pointer j....
The code :-

----------------------------------------------------------------------
----------------------------------------

#include <stdio.h>

void func(int [], int);
int*func2();

void main()

main() returns int ALWAYS.
{
int a[] = {3,2,4};

int*j[8];
int i;

func(a, 2);

The number of elements in your array is three. Why pass two?

*j = func2();
printf("\nTraversing second list\n");

printf("\n%d", *j);

You're obviously not traversing, but accessing the first element.

}

void func(int a[], int size)
{
int i=0;
for (i=0; i<=size; i++)

Change that to i < size
printf("%d\n", a[i]);
}

int*func2()
{

int b[] = {112,3,4,5,6,7,9,4,10};

return *b;

Yikes. You pass back an int from a function that's supposed to be
returning an int *. Presumbably you meant to type:

return b;

However, that's not right either. b is a local array, you can NOT use
it outside of its scope, which happens to be the function func2().

You'll either have to dynamically allocate an array
and populate it, or make b static.
Any help

Turn up the warning level on your compiler.

/* BEGIN new.c */

#include <stdio.h>

#define NINE 9

void func(int a[], int size);
int *func2(void);

int main(void)
{
int a[] = {3,2,4};
int *(*j)(void) = func2;

func(a, sizeof a / sizeof *a);
printf("\nTraversing second list\n");
func(j(), NINE);
return 0;
}

void func(int a[], int size)
{
int i;

for (i = 0; size i; i++) {
printf("%d\n", a[i]);
}
}

int *func2(void)
{
static int b[NINE] = {112,3,4,5,6,7,9,4,10};

return b;
}

/* END new.c */
--
pete
Jun 26 '07 #3
Default User wrote:
Pradyut Bhattacharya wrote:
Hi,
in this program i cannot retrive the values from the pointer
j.... The code :-

--------------------------------------------------------------------
-- ----------------------------------------

#include <stdio.h>

void func(int [], int);
int*func2();

void main()

main() returns int ALWAYS.
{
int a[] = {3,2,4};

int*j[8];
int i;

func(a, 2);

The number of elements in your array is three. Why pass two?

*j = func2();
printf("\nTraversing second list\n");

printf("\n%d", *j);

Oops, I just noticed that you are printing an int pointer as if it were
an int. Yet another mistake. j is an array of int pointers. Possibly
you wanted it to be just an int pointer, which would make the printf ok.


Brian
Jun 26 '07 #4
pete wrote:
void func(int a[], int size);
int *func2(void);

int main(void)
{
int a[] = {3,2,4};
int *(*j)(void) = func2;

func(a, sizeof a / sizeof *a);
printf("\nTraversing second list\n");
func(j(), NINE);
return 0;
}
Hmmm, points for working part of the subject into the solution, but the
OP said RETURNING function pointer.


Brian
Jun 26 '07 #5
Default User wrote:
>
pete wrote:
void func(int a[], int size);
int *func2(void);

int main(void)
{
int a[] = {3,2,4};
int *(*j)(void) = func2;

func(a, sizeof a / sizeof *a);
printf("\nTraversing second list\n");
func(j(), NINE);
return 0;
}

Hmmm, points for working part of the subject into the solution,
but the OP said RETURNING function pointer.
I think I did better than that.

OP said "cannot retrive the values from the pointer j"
I made j into a function pointer.
I used the return value from a function call made through j
to access nine values.
And that's how I got OP's original line of code:
printf("\nTraversing second list\n");
to say what it means.

--
pete
Jun 26 '07 #6
On Wed, 27 Jun 2007 02:17:16 +0530, in comp.lang.c , "Pradyut
Bhattacharya" <pr******@gmail.comwrote:
>int*func2();
funct returns a pointer to an int...
int*j[8];
j is an array of pointers to ints. I don't think you wanted that...
*j = func2();
you set the first pointer in j to whatever func2 returns.
int b[] = {112,3,4,5,6,7,9,4,10};
b is a local array which is discarded when func2 returns.
return *b;
*b is the value of the first element ie 112,

But your function is expecting a /pointer/ to an int. You should
hopefully get a warning here. If not, turn up warning levels. You have
set j[0] to point to the 112th byte of your computer's memory, which
is probably protected ROM space or something...
I presume you want return an array from a function. You can't do that
(easily). Pass the array as an argument.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Jun 26 '07 #7
pete wrote:
Default User wrote:

pete wrote:
void func(int a[], int size);
int *func2(void);
>
int main(void)
{
int a[] = {3,2,4};
int *(*j)(void) = func2;
>
func(a, sizeof a / sizeof *a);
printf("\nTraversing second list\n");
func(j(), NINE);
return 0;
}
Hmmm, points for working part of the subject into the solution,
but the OP said RETURNING function pointer.

I think I did better than that.

OP said "cannot retrive the values from the pointer j"
I made j into a function pointer.
I used the return value from a function call made through j
to access nine values.
And that's how I got OP's original line of code:
printf("\nTraversing second list\n");
to say what it means.
Ok, that's not bad.

Brian
Jun 26 '07 #8
Pradyut Bhattacharya wrote:
Hi,
in this program i cannot retrive the values from the pointer j....
We won't bother discussing absurdities of
void main()
If you don't know better by now, since you have done as any civilized
person would and followed the newsgroup and checked the FAQ before
posting, you never will.

The answer to your stated problem is simple. There is no "the pointer j".
You have declared j with
int*j[8];
So j is an array[8] of pointers to int.
Jun 26 '07 #9
On 6 27 , 4 47 , "Pradyut Bhattacharya" <prady...@gmail.comwrote:
Hi,
in this program i cannot retrive the values from the pointer j....
The code :-

---------------------------------------------------------------------------*-----------------------------------

#include <stdio.h>

void func(int [], int);
int*func2();

void main()
{
int a[] = {3,2,4};

int*j[8];
int i;

func(a, 2);

*j = func2();
printf("\nTraversing second list\n");

printf("\n%d", *j);

}

void func(int a[], int size)
{
int i=0;
for (i=0; i<=size; i++)
printf("%d\n", a[i]);

}

int*func2()
{

int b[] = {112,3,4,5,6,7,9,4,10};

return *b;

}

---------------------------------------------------------------------------*----------------------------------

Any help

Thanks
Pradyuthttp://pradyut.tk
India
It not wise to return a pointer which points to the static.
But if you still want to do so, try to remove the line:
printf("\nTraversing second list\n");

Jun 27 '07 #10
On Jun 26, 9:36 pm, !truth <noddy_zh...@asustek.com.cnwrote:
On 6 27 , 4 47 , "Pradyut Bhattacharya" in this program i cannot retrive the values from the pointer j....
The code :-
---------------------------------------------------------------------------**-----------------------------------
#include <stdio.h>
void func(int [], int);
int*func2();
void main()
{
int a[] = {3,2,4};
int*j[8];
int i;
func(a, 2);
*j = func2();
printf("\nTraversing second list\n");
printf("\n%d", *j);
}
void func(int a[], int size)
{
int i=0;
for (i=0; i<=size; i++)
printf("%d\n", a[i]);
}
int*func2()
{
int b[] = {112,3,4,5,6,7,9,4,10};
return *b;
}
---------------------------------------------------------------------------**----------------------------------
Any help

It not wise to return a pointer which points to the static.
Whyever not? What does this have to do with the question anyway?
But if you still want to do so, try to remove the line:
printf("\nTraversing second list\n");
Why? How is that relevant to anything?

Jun 28 '07 #11

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

Similar topics

5
by: Gent | last post by:
I have two questions which are very similar: Is it possible to return an object in C++. Below is part of my code for reference however I am more concerned about the concept. It seems like the...
11
by: Justin Naidl | last post by:
class Foo { protected: char foo_stuff; public: char* get_foo_stuff(); } Given the above example. What I want to know is the "proper/standard" way
41
by: Materialised | last post by:
I am writing a simple function to initialise 3 variables to pesudo random numbers. I have a function which is as follows int randomise( int x, int y, intz) { srand((unsigned)time(NULL)); x...
10
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences...
3
by: Carramba | last post by:
hi! the code is cinpiling with gcc -ansi -pedantic. so Iam back to my question Iam trying to make program were I enter string and serach char. and funktion prints out witch position char is...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
11
by: Antoninus Twink | last post by:
What's the correct syntax to define a function that returns a pointer to a function? Specifically, I'd like a function that takes an int, and returns a pointer to a function that takes an int and...
23
by: pauldepstein | last post by:
Below is posted from a link for Stanford students in computer science. QUOTE BEGINS HERE Because of the risk of misuse, some experts recommend never returning a reference from a function or...
8
by: darren | last post by:
Hi everybody, have a quick look at this code: ===== ===== int main(void) { string msg; makeString(msg); cout << "back in main, result = " << msg << endl;
5
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.