473,573 Members | 3,311 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What's Happening Here - Program Prints Empty Array?

3 New Member
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 1485
3,652 Recognized Expert Specialist
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
3 New Member
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

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 and have been struggling over this for days, not wanting to show all my ignorance in final desparate plea for help which this is ... Here is the...
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 // number of survey responses
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 how global variables and function returns work... For example, I have a global array "char datestring;" which is defined in the function speakdate....
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 paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990....
by: chandanlinster | last post by:
consider the program, /*********************************************************/ int main() { int a; printf("a = %u\n", a); printf("*a = %u\n", *a); return 0; }
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 occupied one. - Initially, the buffer is empty. Thus, all the slots are initialized with 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 reccomendations woulde be greatly appreciated... 1. Accountant. Write a program that accepts a sequence of integers (some of which may repeat) as input...
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 transformations on it, then display the result. this is the specification of my program Design and implement a C++ program which: 1. Allows the user...
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) {
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.