Scratchy question....

C

chump1708

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?????
 
R

Ravi Uday

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
 
M

mailursubbu

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.....
 
C

chump1708

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....
 
P

pete

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 */
 
K

Kenny McCormack

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;
}
 
V

Vladimir Oka

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
 
C

chump1708

EXCELLENT GROUP>>>>>>......AMAZING POSSIBLE WAYS.....HAIL the
group.....hail the members...
Thanks all....
especially moderator and creator of this group....
 
C

Christopher Benson-Manica

<[email protected]>
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.
 
N

Nick Keighley

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.

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.
 
R

Randy Howard

(e-mail address removed) wrote
(in article
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.
 
C

Christopher Benson-Manica

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.
 

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
474,172
Messages
2,570,934
Members
47,473
Latest member
ChristelPe

Latest Threads

Top