473,395 Members | 1,968 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,395 software developers and data experts.

What's Happening Here - Program Prints Empty Array?

Hi all,

I can't find any problem in this program, i getting empty string as output, why?
what happening here any one help?
Expand|Select|Wrap|Line Numbers
  1. int main() 
  2.         char *p1=“name”; 
  3.         char *p2; 
  4.         p2=(char*)malloc(20); 
  5.         memset (p2, 0, 20); 
  6.         while(*p2++ = *p1++); 
  7.         printf(“%s\n”,p2); 
  8.       return 0;
  9. }
Feb 28 '08 #1
2 1477
Ganon11
3,652 Expert 2GB
Because you are incrementing the pointer as you go, when you finish copying, your pointer looks like this:

Expand|Select|Wrap|Line Numbers
  1. p2 ----------------------
  2.                         V
  3. | n | a | m | e | \0 | some more memory |
It will point just after the string you've finished copying.
Feb 28 '08 #2
Because you are incrementing the pointer as you go, when you finish copying, your pointer looks like this:

Expand|Select|Wrap|Line Numbers
  1. p2 ----------------------
  2.                         V
  3. | n | a | m | e | \0 | some more memory |
It will point just after the string you've finished copying.

Ya i understood, Thank you very much for your help.
Feb 28 '08 #3

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

Similar topics

4
by: Garry Freemyer | last post by:
I'm trying to convert this macro to a c# function but I have a big problem. It's on the LEFT side of an assignment statement and I am extremely flustered over this one because I'm a little rusty...
2
by: RadiationX | last post by:
How does this program work? // fig06_07.c -- COP2220 Example -- 040209 (sjd) // Student poll program #include <stdio.h> #include <stdlib.h> #define RESPONSE_SIZE 40 //...
18
by: ben.carbery | last post by:
Hi, I have just written a simple program to get me started in C that calculates the number of days since your birthdate. One thing that confuses me about the program (even though it works) is...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
5
by: chandanlinster | last post by:
consider the program, /*********************************************************/ int main() { int a; printf("a = %u\n", a); printf("*a = %u\n", *a); return 0; }
10
by: preethamkumark | last post by:
- The program first creates a shared memory buffer containing an array of 20 integers. - Each slot of the buffer can have either 0 or 1, where 0 represents an empty slot, and 1 represents an...
0
by: joestevens232 | last post by:
I need some serious help...I got the first part of the program done the accountant part but i can't figure out how to do the checker..i have been trying for hours and nothings workin any help or...
0
by: aled | last post by:
I'm new to C++ and need help with a binary program, represented in the program as an array of bits (0's and 1's). It allows the user to input a binary word, then do some calculations or...
10
by: DavidSeck.com | last post by:
Hi, I am working with the Facebook API right now, an I have kind of a problem, but I don't know what I am doing wrong. So I have a few arrays, f.ex.: User albums: array(2) {
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.