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

Scratchy question....

P: n/a
Is it possible to return two character pointers using a single return
statement or for that matter returning two values using a single return
statement. For example, say if we have a function like -

char* abc() /*Is this function well written??? any problems
*/
{
char def[8]= "chump";
return (char*)def;
}

void main()
{
char *g,h,*i;
g =abc();
h =*g;
i = somefunction(g); /*****WILL THERE BE ANY PROBLEMS IN THIS
LINE*******/
}

Now time to scratch heads...
Is it possible to return a second character pointer to use back from
this function.....i.e. can we change the function abc() to return
another pointer of same character type in one function
call....???????????????????????

Also, is the abc() function well written>????any errors that we can get
under some circumstances?????

Dec 15 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a


ch*******@yahoo.com wrote:
Is it possible to return two character pointers using a single return
statement or for that matter returning two values using a single return
statement. For example, say if we have a function like -

char* abc() /*Is this function well written??? any problems
*/
{
char def[8]= "chump";
return (char*)def;
You are trying to return the address of a 'local variable' which might
not exist upon the function return !!
}

void main()
{
char *g,h,*i;
g =abc();
h =*g;
i = somefunction(g); /*****WILL THERE BE ANY PROBLEMS IN THIS
LINE*******/

and here you are trying to use that non-existant address by
passing it to another function !

What do you expect to happen btw ? Try to think about it
- Ravi
}

Now time to scratch heads...
Is it possible to return a second character pointer to use back from
this function.....i.e. can we change the function abc() to return
another pointer of same character type in one function
call....???????????????????????

Also, is the abc() function well written>????any errors that we can get
under some circumstances?????


Dec 15 '05 #2

P: n/a
Try executing the below code you can find that most of the time 22 is
printed. I think it will answer your first question.
int *s()
{
int a=20;
return &a;
}

int sum()
{
int z=22;
return 0;
}

int main()
{
int *ptr = s();
sum();
printf("%d",*ptr);
}
2) To send the second Char ptr, send it as the argument to the func So
chimple.....

Dec 15 '05 #3

P: n/a
Well....I ran the code and it worked well....I mean I got the answer
'c' as in "chump" - def[] array when I use printf("%c", h); The answer
was 'c'...I understand that there might be a case when the local array
is not available outside the function.... I think you are correct....

Dec 15 '05 #4

P: n/a
Well...I needed to return 2 char pointers using a single return
call.....

Dec 15 '05 #5

P: n/a
ch*******@yahoo.com wrote:

Is it possible to return two character pointers using a single return
statement
or for that matter returning two values using a single return
statement. For example, say if we have a function like -

char* abc() /*Is this function well written??? any problems
*/
{
char def[8]= "chump";
return (char*)def;
}

void main()
{
char *g,h,*i;
g =abc();
h =*g;
i = somefunction(g); /*****WILL THERE BE ANY PROBLEMS IN THIS
LINE*******/
}

Now time to scratch heads...
Is it possible to return a second character pointer to use back from
this function.....i.e. can we change the function abc() to return
another pointer of same character type in one function
call....???????????????????????

Also, is the abc() function well written>????
any errors that we can get under some circumstances?????


/* BEGIN new.c */

#include <stdio.h>

char **abc(void);

int main(void)
{
char **i;

i = abc();
puts(i[0]);
puts(i[1]);
return 0;
}

char **abc(void)
{
static char def[8]= "chump";
static char *array[2] = {def + 1, def};

return array;
}

/* END new.c */
--
pete
Dec 15 '05 #6

P: n/a
In article <11*********************@o13g2000cwo.googlegroups. com>,
ch*******@yahoo.com <ch*******@yahoo.com> wrote:
Well...I needed to return 2 char pointers using a single return
call.....


something like:

/* Untested, undebugged, probably not standards compliant - but should get
* the OP started */
char **foo(whatever) {
static char *buff[2];
/* whatever */
return buff;
}

Dec 15 '05 #7

P: n/a
ch*******@yahoo.com wrote:

Now time to scratch heads...
Is it possible to return a second character pointer to use back from
this function.....i.e. can we change the function abc() to return
another pointer of same character type in one function
call....???????????????????????


Create a structure containing two pointers, and rewrite your function
to return this structure instead of the single pointer.

Regards

Vladimir

Dec 15 '05 #8

P: n/a
EXCELLENT GROUP>>>>>>......AMAZING POSSIBLE WAYS.....HAIL the
group.....hail the members...
Thanks all....
especially moderator and creator of this group....

Dec 15 '05 #9

P: n/a
<11*********************@g47g2000cwa.googlegroups. com>
ch*******@yahoo.com <ch*******@yahoo.com> wrote:
EXCELLENT GROUP>>>>>>......AMAZING POSSIBLE WAYS.....HAIL the
group.....hail the members...
Thanks all....
especially moderator and creator of this group....


There is no moderator (comp.lang.c.moderated is, however). I don't
believe there is a single creator; Usenet generally doesn't work like
that.

--
Christopher Benson-Manica
ataru(at)cyberspace.org
Dec 15 '05 #10

P: n/a
ch*******@yahoo.com wrote:
I think you are correct....


Correct about what?
Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Dec 15 '05 #11

P: n/a
please leave some context in your code.

chump1708 is returning the address of a local variable and has
been told the local variable "may" not exist once the function
has returned. This is not correct. Once a function returns any
local variables *will* not exist.

ch*******@yahoo.com wrote:
Well....I ran the code and it worked well....I mean I got the answer
'c' as in "chump" - def[] array when I use printf("%c", h); The answer
was 'c'...I understand that there might be a case when the local array
is not available outside the function.... I think you are correct....


the local variable is never available outside the function. What your
program is doing is exhibiting Undefined Behaviour. The program's
behaviour is not defined by the C Standard. The implementor of the
compiler is allowed to do anything he likes. In this case it is
probably
accessing the memory location where the local variable was held on
a stack. This memory is no longer valid but (only in this case) still
holds the old value of the variable.

Moral: just because a program does what you expect doesn't mean it
is correct.
--
Nick Keighley
Testing can show the presense of bugs, but not their absence.
-- Dijkstra

Dec 15 '05 #12

P: n/a
"ch*******@yahoo.com" <ch*******@yahoo.com> writes:
EXCELLENT GROUP>>>>>>......AMAZING POSSIBLE WAYS.....HAIL the
group.....hail the members...
Thanks all....
especially moderator and creator of this group....


Please read <http://cfaj.freeshell.org/google/> and follow its advice.

Also, things like ALL-CAPS and multiple question marks are the
equivalent of shouting; please don't do that.

--
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.
Dec 15 '05 #13

P: n/a
ch*******@yahoo.com wrote
(in article
<11**********************@g47g2000cwa.googlegroups .com>):
Well....I ran the code and it worked well....I mean I got the answer
'c' as in "chump" - def[] array when I use printf("%c", h); The answer
was 'c'...
Getting lucky on a single implementation, for a particular
example program doesn't make it "okay", or "correct". It just
means you appear to be making a very poor assumption about it's
correctness. In effect, this is bad luck for you, because you
will make a very incorrect assumption about what is or is not
valid C as a result of it.
I understand that there might be a case when the local array
is not available outside the function.... I think you are correct....


It is never guaranteed to be available, even if you find a case
where it is available. You can learn it now the easy way, or
you can learn it later the hard way.
--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Dec 15 '05 #14

P: n/a
ma*********@gmail.com wrote:
Try executing the below code you can find that most of the time 22 is
printed. I think it will answer your first question.
Only if the question is "What is an example of something I should
never do?". Returning the address of a local variable is always a
mistake.
int *s()
{
int a=20;
return &a;
}


--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Dec 16 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.