Why I'm geting the wrong string?
I try to understand what is the problem with this code
1 2 3 4
|
char name[NAME_LEN],id[ID_LEN],act[REQ_MSG];
scanf("%s %s %s",act,id,name);
sprintf(s2.mtext,"%s %s %s",act,id,name);
printf("s2.mtext is: %s\n",s2.mtext);
|
s2.text is a part of a struct,
and lets say: s2.mtext=char [50].
Can you see where is the problem?
I get the wrong string... :-(
Thank you!
Last edited on
What's an s2?
1 2 3 4 5 6
|
struct my_msgbuf{
long mtype;
char mtext[NAME_LEN+ID_LEN+REQ_MSG+1];
};
struct my_msgbuf s2;
|
NAME_LEN+ID_LEN+REQ_MSG+1
is something like 40...
Someone???
Hi cpcp,
I did a quick try with the information you gave and had no problem. Here is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#include <stdio.h>
#include <stdlib.h>
#define NAME_LEN 10
#define ID_LEN 10
#define REQ_MSG 19
struct my_msgbuf{
long mtype;
char mtext[NAME_LEN+ID_LEN+REQ_MSG+1];
};
int main()
{
struct my_msgbuf s2;
char name[NAME_LEN],id[ID_LEN],act[REQ_MSG];
scanf("%s %s %s",act,id,name);
sprintf(s2.mtext,"%s %s %s",act,id,name);
printf("s2.mtext is: %s\n",s2.mtext);
return 0;
}
|
And here is the output (with random inputs):
$ gcc tmp.cc -o tmp
$ ./tmp
Alex
123
EDR
s2.mtext is: Alex 123 EDR
|
So could you give us a bit more detail or the output you get and the input you give ?
And if I'm doing it with spaces?
It will be good to?
Thank you!
I just checked and, yes it works. I think it works for any kind of space: tabulation, simple space, new lines...
Do you still have problems ?
Topic archived. No new replies allowed.