473,325 Members | 2,480 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,325 software developers and data experts.

why the complete string is not getting printed?

Siddarth777
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int main()
  5. {
  6.     char *ptr;
  7.     int n,i,count=0;
  8.  
  9.     printf("How many characters do you want:");
  10.     scanf("%d",&n);
  11.  
  12.     ptr=(char*)malloc(n*1);
  13.  
  14.     if(ptr==NULL)
  15.     {
  16.         printf("memory allocation error");
  17.         exit(1);
  18.     }
  19.  
  20.     for(i=0;i<n;i++)
  21.     {
  22.         scanf("%c",(ptr+i));
  23.         count++;
  24.     }
  25.  
  26.     printf("The string you entered is:");
  27.     for(i=0;i<n+1;i++)
  28.     printf("%c",*(ptr+i));
  29.     printf("\n%d",count);
  30.  
  31. }
  32.  

As I have written the above program to check the working of the dynamic character arrays, but when i print the array i have entered, am not getting the complete string printed

am unable to understand where i am going wrong
please help me

thank you in advance
Jul 13 '11 #1
2 1628
donbock
2,426 Expert 2GB
What output do you get? How does that differ from what you expect.

I notice that you malloc n bytes; you read in n bytes; but you write out n+1 bytes. Why the discrepancy?
Jul 14 '11 #2
actually in the above program, n means the size of the string, and for just the trial purpose i have used n+1 to test whether the complete string will be printed or not......so where i am going wrong
please help
Nov 13 '11 #3

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

Similar topics

4
by: Bill | last post by:
I'm submitting a string of data, with several words in it. When I try to retreive it from the database, I'm finding the words of the string have been removed. For example, if the string entered is...
1
by: Mohan | last post by:
Hi All, I am using web browser control in a VB 6.0 client server application to print reports. I am writing the report into a HTML file and displays the report to the user using the code,...
1
by: Bill | last post by:
I am passing a string of text between a page and a database. It is a book title, which consists of 2 or three words, and only the first word is passing to the database. The page that passes the...
3
by: Ilja Booij | last post by:
Hi all, I have some trouble with the following: I'm getting a time string, in YYYY-MM-DD HH:mm:ss format, which I need to translate into a string with DD-Mon-YYYY HH:mm:ss +HHMM, where the...
1
by: Poonam | last post by:
Hi I have a page where there is table and below the table there is 'iframe', This page also has 'Print' button. Printing this page is printing the table and iframe contents on 2 different...
3
by: manontheedge | last post by:
I have some code that I'm reusing, so nothing has changed EXCEPT that the length of a string has increased a bit...maybe by a few hundred characters. i have a string assignment statement, for...
6
by: bushwacker | last post by:
Hello all, I'm a chemical engineering student. our teacher has given us a project to do some calculations based on some equations. those equations include constants, which are to be read from a...
5
by: myra | last post by:
Hi, The problem is: I am trying to get the function call tree by using the profiling functions __cyg_profile_func_enter and __cyg_profile_func_exit. But it is printing the address of these...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.