Connecting Tech Pros Worldwide Forums | Help | Site Map

problem with return

Sheldon
Guest
 
Posts: n/a
#1: Oct 20 '06
Hi all,

I am trying to understand why this function fails to return a string.
Can anyone help?

*************
#include <stdio.h>
#include <stdarg.h>

char set(char *item, int num, ...);

int main(void) {
char *Item;
char Ret;
printf("%s\n", set(item,4, "apple", "pear", "banana", "grape"));
}
char set(char *Item, int num, ...) {
va_list ap;
int i;
char all[50];
va_start(ap, num);
for (i=0; i<num;i++) {
if (i==0)
strcpy(all,item=va_arg(ap,char *));
else
strcat(all,item=va_arg(ap,char *));
}
va_end(ap);
return all;
}
****************

Thanks in advance.

Sheldon


Bill Pursell
Guest
 
Posts: n/a
#2: Oct 20 '06

re: problem with return


Sheldon wrote:
Quote:
Hi all,
>
I am trying to understand why this function fails to return a string.
Can anyone help?
>
*************
#include <stdio.h>
#include <stdarg.h>
>
char set(char *item, int num, ...);
>
int main(void) {
char *Item;
char Ret;
printf("%s\n", set(item,4, "apple", "pear", "banana", "grape"));
}
char set(char *Item, int num, ...) {
va_list ap;
int i;
char all[50];
Make that:
"static char all[50]"
Your function returns a pointer which is invalid
as soon as the function returns. Making it static
will resolve that. It is not the most elegant solution,
however, and you might consider passing in a buffer.
(Which is what the first argument feels like it should be,
by the way. It's very odd to pass in that argument and
then use it as a local variable (which is ignored!)
the way you're doing.)
Quote:
va_start(ap, num);
for (i=0; i<num;i++) {
if (i==0)
strcpy(all,item=va_arg(ap,char *));
else
strcat(all,item=va_arg(ap,char *));
And realize that this will be a very likely cause
of future bugs as you overflow the buffer. Keep
track of the length and use strncat, or do something
else to avoid the overflow.

--
Bill Pursell

Christopher Benson-Manica
Guest
 
Posts: n/a
#3: Oct 20 '06

re: problem with return


Sheldon <shejo284@gmail.comwrote:
Quote:
I am trying to understand why this function fails to return a string.
Who knows? You clearly didn't post your actual code; the code you
posted is broken.
Quote:
Can anyone help?
Compare your code:
Quote:
#include <stdio.h>
#include <stdarg.h>
Quote:
char set(char *item, int num, ...);
Quote:
int main(void) {
char *Item;
char Ret;
printf("%s\n", set(item,4, "apple", "pear", "banana", "grape"));
}
char set(char *Item, int num, ...) {
va_list ap;
int i;
char all[50];
va_start(ap, num);
for (i=0; i<num;i++) {
if (i==0)
strcpy(all,item=va_arg(ap,char *));
else
strcat(all,item=va_arg(ap,char *));
}
va_end(ap);
return all;
}
with this, which your code may or may not resemble:

#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>

char *set(char **item, int num, ...);

int main(void) {
char *item;
printf("%s\n", set(&item,4, "apple", "pear", "banana", "grape"));
return 0;
}

char *set(char **item, int num, ...) {
va_list ap;
int i;
char *all=malloc(50); /* assume success */
*all=0;
va_start(ap, num);
for (i=0; i<num;i++) {
strcat(all,*item=va_arg(ap,char *));
}
va_end(ap);
return all;
}

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Martin Ambuhl
Guest
 
Posts: n/a
#4: Oct 20 '06

re: problem with return


Sheldon wrote:
Quote:
Hi all,
>
I am trying to understand why this function fails to return a string.
Can anyone help?
>
*************
#include <stdio.h>
#include <stdarg.h>
>
char set(char *item, int num, ...);
set() returns a char. A string is an array of chars, the last being 0.
Fred Kleinschmidt
Guest
 
Posts: n/a
#5: Oct 20 '06

re: problem with return



"Sheldon" <shejo284@gmail.comwrote in message
news:1161363728.269037.62570@h48g2000cwc.googlegro ups.com...
Quote:
Hi all,
>
I am trying to understand why this function fails to return a string.
Can anyone help?
>
*************
#include <stdio.h>
#include <stdarg.h>
>
char set(char *item, int num, ...);
>
int main(void) {
char *Item;
char Ret;
printf("%s\n", set(item,4, "apple", "pear", "banana", "grape"));
}
char set(char *Item, int num, ...) {
You specify here that it returns a 'char', not a 'char *'
Quote:
va_list ap;
int i;
char all[50];
va_start(ap, num);
for (i=0; i<num;i++) {
if (i==0)
strcpy(all,item=va_arg(ap,char *));
else
strcat(all,item=va_arg(ap,char *));
}
va_end(ap);
return all;
Here you are trying to return a 'char array'.
This conflicts with your statement above that it returns a 'char'
Quote:
}
****************
>
Thanks in advance.
>
Sheldon
>
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project


W H G
Guest
 
Posts: n/a
#6: Oct 21 '06

re: problem with return



"Sheldon" <shejo284@gmail.comwrote in message
news:1161363728.269037.62570@h48g2000cwc.googlegro ups.com...
Quote:
Hi all,
>
I am trying to understand why this function fails to return a string.
Can anyone help?
>
*************
#include <stdio.h>
#include <stdarg.h>
>
char set(char *item, int num, ...);
>
int main(void) {
char *Item;
char Ret;
printf("%s\n", set(item,4, "apple", "pear", "banana", "grape"));
}
----------------We all know this doesn't compile:
you use variable item which is not Item declared above.
----------------W H G


----snip


Sheldon
Guest
 
Posts: n/a
#7: Oct 21 '06

re: problem with return



Christopher Benson-Manica skrev:
Quote:
Sheldon <shejo284@gmail.comwrote:
>
Quote:
I am trying to understand why this function fails to return a string.
>
Who knows? You clearly didn't post your actual code; the code you
posted is broken.
>
Quote:
Can anyone help?
>
Compare your code:
>
Quote:
#include <stdio.h>
#include <stdarg.h>
>
Quote:
char set(char *item, int num, ...);
>
Quote:
int main(void) {
char *Item;
char Ret;
printf("%s\n", set(item,4, "apple", "pear", "banana", "grape"));
}
char set(char *Item, int num, ...) {
va_list ap;
int i;
char all[50];
va_start(ap, num);
for (i=0; i<num;i++) {
if (i==0)
strcpy(all,item=va_arg(ap,char *));
else
strcat(all,item=va_arg(ap,char *));
}
va_end(ap);
return all;
}
>
with this, which your code may or may not resemble:
>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
>
char *set(char **item, int num, ...);
>
int main(void) {
char *item;
printf("%s\n", set(&item,4, "apple", "pear", "banana", "grape"));
return 0;
}
>
char *set(char **item, int num, ...) {
va_list ap;
int i;
char *all=malloc(50); /* assume success */
*all=0;
va_start(ap, num);
for (i=0; i<num;i++) {
strcat(all,*item=va_arg(ap,char *));
}
va_end(ap);
return all;
}
>
--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.

I am very new in the world of C, only a few weeks. I am trying to learn
fast and along the way mistakes occur. Your code is certainly better
than mine but I was after an explanation as to why. Of course the code
is broken; that is why I asked for help. Apparently, it has to do with
me returning an array instead of a char.
Thanks for taking the time to point that out.

/Sheldon

Sheldon
Guest
 
Posts: n/a
#8: Oct 21 '06

re: problem with return



Bill Pursell skrev:
Quote:
Sheldon wrote:
Quote:
Hi all,

I am trying to understand why this function fails to return a string.
Can anyone help?

*************
#include <stdio.h>
#include <stdarg.h>

char set(char *item, int num, ...);

int main(void) {
char *Item;
char Ret;
printf("%s\n", set(item,4, "apple", "pear", "banana", "grape"));
}
char set(char *Item, int num, ...) {
va_list ap;
int i;
char all[50];
>
Make that:
"static char all[50]"
Your function returns a pointer which is invalid
as soon as the function returns. Making it static
will resolve that. It is not the most elegant solution,
however, and you might consider passing in a buffer.
(Which is what the first argument feels like it should be,
by the way. It's very odd to pass in that argument and
then use it as a local variable (which is ignored!)
the way you're doing.)
>
Quote:
va_start(ap, num);
for (i=0; i<num;i++) {
if (i==0)
strcpy(all,item=va_arg(ap,char *));
else
strcat(all,item=va_arg(ap,char *));
>
And realize that this will be a very likely cause
of future bugs as you overflow the buffer. Keep
track of the length and use strncat, or do something
else to avoid the overflow.
>
--
Bill Pursell
Thanks Bill,

I understand the reason, but I need learn more about C in order to
fully get it. Nevertheless, I appreciate the pointer, pun intended.

/Sheldon

Christopher Benson-Manica
Guest
 
Posts: n/a
#9: Oct 22 '06

re: problem with return


Sheldon <shejo284@gmail.comwrote:
Quote:
I am very new in the world of C, only a few weeks. I am trying to learn
fast and along the way mistakes occur. Your code is certainly better
than mine but I was after an explanation as to why.
I apologize for being more unfriendly than was necessary, but I
assumed that your real code compiled, which the code you posted did
not. Enough people fail to post their real code that I jumped to that
conclusion in your case. In any case there were other errors with
your code - among them being that you failed to include <string.h>, as
well as other errors pointed out by other posters.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Sheldon
Guest
 
Posts: n/a
#10: Oct 23 '06

re: problem with return



Christopher Benson-Manica wrote:
Quote:
Sheldon <shejo284@gmail.comwrote:
>
Quote:
I am very new in the world of C, only a few weeks. I am trying to learn
fast and along the way mistakes occur. Your code is certainly better
than mine but I was after an explanation as to why.
>
I apologize for being more unfriendly than was necessary, but I
assumed that your real code compiled, which the code you posted did
not. Enough people fail to post their real code that I jumped to that
conclusion in your case. In any case there were other errors with
your code - among them being that you failed to include <string.h>, as
well as other errors pointed out by other posters.
>
--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Thanks,

I really learned from your pointers. I understand that you guys get all
kinds of strange request and I will try to point out in the future
whether my code compiled or not. I have corrected my code, using your
example and it works fine. I am learning C on the fly and when such
problems occurr, it takes sometime for me to research it.

/Sheldon

Closed Thread