[VIEWED 21008
TIMES]
|
SAVE! for ease of future access.
|
|
|
|
deepak_bhatta
Please log in to subscribe to deepak_bhatta's postings.
Posted on 08-02-05 12:08
AM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
Wanna discuss in the field of programming
|
|
|
|
Pisces
Please log in to subscribe to Pisces's postings.
Posted on 08-02-05 12:17
AM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
Discuss? You have problems or have solutions?
|
|
|
rajkb34
Please log in to subscribe to rajkb34's postings.
Posted on 08-02-05 1:12
AM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
|
|
|
Dada_Giri
Please log in to subscribe to Dada_Giri's postings.
Posted on 08-02-05 1:20
AM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
ल ल सुरु गरुम् न सुरु गरुम्।
|
|
|
sojodude
Please log in to subscribe to sojodude's postings.
Posted on 08-02-05 1:37
AM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
char* p; char* q; p = "pqrst\0"; q = "abcde\0"; while(*p==*q) { p++; q++; } should be the same as: while(*p++=*q++); But this Damn visual C++ is giving a link error..Why??
|
|
|
sajhabusaima
Please log in to subscribe to sajhabusaima's postings.
Posted on 08-02-05 4:50
AM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
bro run on turbo,where are you giving command to print value.
|
|
|
sojodude
Please log in to subscribe to sojodude's postings.
Posted on 08-02-05 11:05
AM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
I'm trying to copy q into p. Logically it should work as the length of both strings are the same and at the end '\0' is passed into p which is ascii 0 and the loop should stop. But it isn't working on VCPP. I don't know if that's a microsoft problem or a bad programming practice. So just wondering if anybody else gets the desired results using a different compiler.
|
|
|
testdirector
Please log in to subscribe to testdirector's postings.
Posted on 08-02-05 1:06
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
Pprobably you mean a run-time error. One typo you have! Correction while(*p++ == *q++);
|
|
|
arch119
Please log in to subscribe to arch119's postings.
Posted on 08-02-05 3:40
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
>>testdirector provided that he wants to copy q into p ,I don't think that's a typo. But with the same supposition, his first code snippet should be while(*p=*q){ p++; q++; } >>sojodude I haven't tried it in Microsoft's compiler but it should not give you a *link* error. It should rather end up in a segmentation fault (runtime error) [ I don't think Microsoft's compiler is so smart as to detect a runtime error at the linking stage ]. Reason: p points to "pqrst\0" but the latter being a string constant is placed in that part of the memory which can only be read but can't be written. An attempt to write on it will surely cause a runtime error. Btw, when defining a string constant you do not explicitly need to type the trailing "\0". The compiler will place a \0 at the end of the characters in double quotes. And here is a code snippet that copies q into p, you should point p to a region in the memory which can be both read and written. An array or dynamic allocation with malloc will do. char *p=(char *)malloc(6); char *q="abcde"; char *r = p; while(*p=*q){ p++; q++; } printf("%s",r); //will print "abcde"
|
|
|
testdirector
Please log in to subscribe to testdirector's postings.
Posted on 08-02-05 4:17
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
well, I failed to understand what he wants; assuming he wants a copy which he expressed in his second posting, there is a discrepancy between the original two examples. It will be manifested by different behaviours (run-time error) in the two cases during the run. Now you want to call it a typo in the first example or the second, you are free to judge (I did not read his second posting). But here is what I have to say : when you say, p points to a constant area to which a write fails(takes an exception) is not a valid "C" statement (I do that all the time). Although the content of the string is constant, the "abcde\0" is actually a free pointer equivalent to "abcde", a constant string, i.e. a constant pointer to a string of constant letters. Can the letters be changed, yeah sure, any time. Can the address of the string be changed during the run, no, never, but can the pointer p be pointed to another address, yes, sure, again any time (that's what you are doing). Your second example is redundant; the first is good enough.
|
|
|
arch119
Please log in to subscribe to arch119's postings.
Posted on 08-02-05 9:03
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
>>test director >when you say, p points to a constant area to which a write fails(takes an exception) is not a valid "C" statement (I do that all the time). Where did I say that ? >Can the letters be changed, yeah sure, any time. Can you ? Will appreciate if you write a sample program, you OS (with version) and your compiler (With version). >Your second example is redundant; the first is good enough. Ya I will accept that it was indeed redundant if you can write a program that changes the letters in a string constant.
|
|
|
sojodude
Please log in to subscribe to sojodude's postings.
Posted on 08-02-05 9:19
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
|
|
|
sojodude
Please log in to subscribe to sojodude's postings.
Posted on 08-02-05 9:34
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
Arch, I still get the runtime error. Sorry by link I meant runtime. This is what I had. char* p; char* q; p = (char*)malloc(6); q = (char*)malloc(6); q = "abcde"; while(*p=*q) { p++; q++; } char* r; r = p; cout << "\nNew p is: " << r << "\n";
|
|
|
arch119
Please log in to subscribe to arch119's postings.
Posted on 08-02-05 10:07
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
>>sojodude char* p; char* q; p = (char*)malloc(6); q = (char*)malloc(6); // u don't need this 'coz u have pointed q to a different address in the next line. The memory u just allocated becomes completely useless. q = "abcde"; while(*p=*q) { p++; q++; } char* r; // r = p; //move these lines before the while loop because at this point, pointer r points to a memory region that points to the end of "abcde".
|
|
|
testdirector
Please log in to subscribe to testdirector's postings.
Posted on 08-03-05 8:04
AM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
Well, you've already done so by correcting sojodude's first example. If you insist, I have to ask you to compile and run (on any OS , any good compiler) it, it is guaranteed to work. Here it is: #include int main() { char *p, *q, *st; st = p = "abcde"; q = "pqrst"; while(*p++=*q++); printf("%s", st); return 0; }
|
|
|
testdirector
Please log in to subscribe to testdirector's postings.
Posted on 08-03-05 8:05
AM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
#include "stdio.h" int main() { char *p, *q, *st; st = p = "abcde"; q = "pqrst"; while(*p++=*q++); printf("%s", st); return 0; }
|
|
|
gidilat
Please log in to subscribe to gidilat's postings.
Posted on 08-03-05 10:41
AM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
The malloc version from arch119 would work. While Testdirector version with: --- st = p = "abcde"; --- q = "pqrst"; --- while(*p++=*q++); should not work. It should compile fine though. You are trying to modify a string constant. Thats dangerous. A good OS should detect that danger. Thats how some hackers crack system. GN
|
|
|
testdirector
Please log in to subscribe to testdirector's postings.
Posted on 08-03-05 11:28
AM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
Well Gidilat, I ask you to prove your point. Hackers? Define hackers, otherwise hackers have nothing to do with constant strings, believe me.
|
|
|
testdirector
Please log in to subscribe to testdirector's postings.
Posted on 08-03-05 12:19
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
Let us change the matter a little and use arrays. #include "stdio.h" int main() { char p1[] = "abcde"; char q1[] = "pqrst"; char *p, *q,*st; st = p = p1; q = q1; while(*p++ = *q++); printf("%s", st); return 0; } Will it work?
|
|
|
testdirector
Please log in to subscribe to testdirector's postings.
Posted on 08-03-05 2:13
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
what about using const operator. #include const int array[] = {1,2,3,4,5}; /* tell compiler to generate a protected area for it*/ int main() { int *intp; int i; intp = (int *) array; for (i = 0; i < 5; i++) intp[i] ++; /* Access violation, the compiler does not detect it, * the system (processor +OS) may detect it */ return 0; }
|
|