[Show all top banners]

deepak_bhatta
Replies to this thread:

More by deepak_bhatta
What people are reading
Subscribers
:: Subscribe
Back to: Kurakani General Refresh page to view new replies
 Information technology

[Please view other pages to see the rest of the postings. Total posts: 61]
PAGE:   1 2 3 4 NEXT PAGE
[VIEWED 21008 TIMES]
SAVE! for ease of future access.
The postings in this thread span 4 pages, View Last 20 replies.
Posted on 08-02-05 12:08 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Wanna discuss in the field of programming
 
Posted on 08-02-05 12:17 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Discuss? You have problems or have solutions?
 
Posted on 08-02-05 1:12 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 
 
Posted on 08-02-05 1:20 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

ल ल सुरु गरुम् न सुरु गरुम्।
 
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??

 
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.
 
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.
 
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++);
 
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"

 
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.
 
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.
 
Posted on 08-02-05 9:19 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

I actually found this question at

http://www.acetheinterview.com/cgi-bin/answers.cgi?action=answers&number=5&topic=000017.ubb&q_id=17

And everybody there seems so confident that the piece of code is correct. So I was a little surprized when it didn't work.
 
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";
 
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".


 
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;
}

 
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;
}

 
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

 
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.


 
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?
 
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;
}
 



PAGE:   1 2 3 4 NEXT PAGE
Please Log in! to be able to reply! If you don't have a login, please register here.

YOU CAN ALSO



IN ORDER TO POST!




Within last 60 days
Recommended Popular Threads Controvertial Threads
TPS Re-registration case still pending ..
Toilet paper or water?
and it begins - on Day 1 Trump will begin operations to deport millions of undocumented immigrants
Tourist Visa - Seeking Suggestions and Guidance
From Trump “I will revoke TPS, and deport them back to their country.”
I hope all the fake Nepali refugee get deported
advanced parole
ढ्याउ गर्दा दसैँको खसी गनाउच
To Sajha admin
MAGA denaturalization proposal!!
How to Retrieve a Copy of Domestic Violence Complaint???
wanna be ruled by stupid or an Idiot ?
All the Qatar ailines from Nepal canceled to USA
MAGA मार्का कुरा पढेर दिमाग नखपाउनुस !
Travel Document for TPS (approved)
MAGA and all how do you feel about Trumps cabinet pick?
Those who are in TPS, what’s your backup plan?
NOTE: The opinions here represent the opinions of the individual posters, and not of Sajha.com. It is not possible for sajha.com to monitor all the postings, since sajha.com merely seeks to provide a cyber location for discussing ideas and concerns related to Nepal and the Nepalis. Please send an email to admin@sajha.com using a valid email address if you want any posting to be considered for deletion. Your request will be handled on a one to one basis. Sajha.com is a service please don't abuse it. - Thanks.

Sajha.com Privacy Policy

Like us in Facebook!

↑ Back to Top
free counters