some strange wtf
Good day.
I have this, near equal code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
#include <stdio.h>
#include <string.h>
char *rs1 (const char *str)
{
char res1[6];
strncpy(res1,str,3);
res1[3]='\0';
return res1;
}
char *rs2 (const char *str)
{
char *res2 = new char[6];
strncpy(res2,str,3);
res2[3]='\0';
return res2;
}
int main ()
{
char str[] ="+OK 1 523\r\n";
puts(rs1(str));
puts(rs2(str));
return 0;
}
| |
But console output differs! Can you plz explain why?? Thanks in advance.
1 2 3 4 5 6 7
|
char *rs1 (const char *str)
{
char res1[6];
strncpy(res1,str,3);
res1[3]='\0';
return res1;
}
| |
I guess this is wrong, you're returning local variable. Change this function as
1 2 3 4 5 6 7
|
char *rs1 (const char *str)
{
char *res1 = new char[6];
strncpy(res1,str,3);
res1[3]='\0';
return res1;
}
| |
oh, i haven't noticed that.
BTW, code gives correct result to me , after i've done that change
My output console :
What system and compiler you are using?
Windows OS , Dev c++ ide
Windows not localized? I mean "pure" english version?
Topic archived. No new replies allowed.