how do i do this? i do
to point to the first character of s.Code:char *s="brown monkey"
char *p=s;
Printable View
how do i do this? i do
to point to the first character of s.Code:char *s="brown monkey"
char *p=s;
First
char *s = "literal";
is not quite valid, it should be
const char *s = "literal";
and thus
const char *p = s;
To get the last character,
p = s + strlen(s) - 1;
strlen comes from <string.h>.
This is pure C, in C++ you'd be better off using std::string.
thanks corned. it helps. but my code
throwsCode:#include<string.h>
main(){
const char *s="brown monkey";
const char *p=s+strlen(s)-1;
char *t;
while(*--p)
*t++=*p;
printf("%s",t);
}
am i missing something? i'm trying to do a strrev function of my own. thanks.Code:Exception: STATUS_ACCESS_VIOLATION at eip=0040109C
eax=00000001 ebx=00000004 ecx=FFFFFF65 edx=0040104E esi=61095368 edi=00000000
ebp=0022FEA4 esp=0022FE8C program=D:\My Documents\My Project\C\a.exe
cs=001B ds=0023 es=0023 fs=003B gs=0000 ss=0023
Stack trace:
Frame Function Args
0022FEA4 0040109C (00000001, 0A011620, 0A010008, 00000000)
0022FF10 6100401E (00000000, 77F517E6, 85362CC8, 00000300)
0022FF40 6100421D (00401054, 77F517E6, 0000124F, 00000003)
0022FF60 6100425C (00000000, 00000000, C038893C, 0000000F)
0022FF90 00401107 (00401054, 8053476F, 00000001, 00000000)
0022FFC0 0040103D (77F517E6, 00000007, 7FFDF000, F18E1CF0)
0022FFF0 77E814C7 (00401000, 00000000, 78746341, 00000020)
End of stack trace
There's no memory attached to t, it points to a random location.
Learn a lot about string handling in C from a book or online tutorial.