473,387 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Different copy function showing drastically different results

ilikesuresh
I have written a program for string copying
when i run this program its give the result fine

Expand|Select|Wrap|Line Numbers
  1. main()
  2. {
  3.   char *d="MyString";
  4.   char *s="\0";
  5.   char *p="\0";
  6.    s=(char *)malloc(20);
  7.    P=(char *)malloc(20);
  8.    p=s;
  9.    while(*s++ = *d++);
  10.   printf("%s",p);
  11. }
But when i run

Expand|Select|Wrap|Line Numbers
  1. main()
  2. {
  3.   char *d="MyString";
  4.   char *s="\0";
  5.    s=(char *)malloc(20);
  6.    while(*s++ = *d++);
  7.   printf("%s",s);
  8. }
-This way its run fine but it does not display any thing.
Please clarify me what's wrong with the second program?
Aug 30 '07 #1
1 1087
Ganon11
3,652 Expert 2GB
You are incrementing the pointer s, so that by the end of the program, it points past the string you just copied. Thus, it points to no valid text, and no text is displayed. You need a way to get the pointer back to its original position - like in the first example, where you made a second pointer to hold that memory location,
Aug 30 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

30
by: franky.backeljauw | last post by:
Hello, I am wondering which of these two methods is the fastest: std::copy, which is included in the standard library, or a manually written pointer copy? Do any of you have any experience with...
2
by: Akinja | last post by:
I have a strange question and hope someone can shed some light on the problem. First let me give you my scenario. We are using doubletake to replicate our database server files between the...
5
by: CreepieDeCrapper | last post by:
i have a simple JS window.open function that i'm calling and it works great here: http://demo.creationsite.com/GLBC/www/ (click on "virtual tour" in the yellow text link) - no status bar -...
3
by: Tao.Young | last post by:
Hi, I met with a very strange problem: the same function will generate different results, but when i uncomment Line1, it'll get the same results. anyone can tell me why? the code is...
26
by: _R | last post by:
Given that VS2005 has made an effort to clean up the syntax of VC++ (in C++/CLI), is there any future plans to do away with function protos, ala C#/VB? What are they needed for these days?
15
by: Dilip | last post by:
I am aware that the C++ standard in its present form does not say anything about threads, however I do have a relevant question. I am working on Windows XP/VC++ 8.0. Is there a problem new'ing...
15
by: Frederick Gotham | last post by:
What's the canonical way to copy an array in C++? If we're copying a POD, we can use memcpy (but there could be a more efficient alternative if we know that the blocks are suitably aligned). ...
1
by: ceestand | last post by:
I am having a weird (to me at least) problem retrieving a copy of an item in the Cache. I do something like this: DataTable ToBeInserted = FunctionThatReturnsDataTable();...
1
by: Chris | last post by:
Ok, I am new to php and kinda self teaching myself but I haven't found anyone with this problem. I am creating a site to manage my movies basicly the site will add movies to a database and view, as...
4
by: supriyamk | last post by:
Hi, I am trying to search a directory for subdirectories, recreate the subdirectories elsewhere and copy only certain files from current subdirectory to new subdirectory. In other words i am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.