problem with return

S

Sheldon

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
 
B

Bill Pursell

Sheldon said:
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.)
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.
 
C

Christopher Benson-Manica

Sheldon said:
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.
Can anyone help?

Compare your code:
#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;
}

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

Fred Kleinschmidt

Sheldon said:
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 *'
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'
 
M

Martin Ambuhl

Sheldon said:
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.
 
W

W H G

Sheldon said:
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
 
S

Sheldon

Christopher Benson-Manica skrev:
Sheldon said:
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.
Can anyone help?

Compare your code:
#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;
}

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


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
 
S

Sheldon

Bill Pursell skrev:
Sheldon said:
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.)
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.

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
 
C

Christopher Benson-Manica

Sheldon said:
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.
 
S

Sheldon

Christopher said:
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.

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
 

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
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top