|
-
Jun 25th, 2004, 11:08 PM
#1
Thread Starter
Fanatic Member
pointing to the last character
how do i do this? i do
Code:
char *s="brown monkey"
char *p=s;
to point to the first character of s.
-
Jun 26th, 2004, 05:22 AM
#2
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jun 27th, 2004, 08:12 PM
#3
Thread Starter
Fanatic Member
thanks corned. it helps. but my code
Code:
#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);
}
throws
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
am i missing something? i'm trying to do a strrev function of my own. thanks.
-
Jun 28th, 2004, 03:02 AM
#4
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|