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

it is not printing the right values

main()
{

int *ptr;
int *p;
p=ptr;
int i;
ptr = malloc (10);

for (i =0;i<10;i++)
ptr[i++] = 2;

for (i =0;i<10;i++)
printf("%d\n",p[i]);
}
the output is something like this

0
4096
132617
-1075507061
4
12643904
3
100
0
2

Apr 10 '08 #1
10 1217
"pr****************@gmail.com" <pr****************@gmail.comwrites:
>main()
{
>int *ptr;
int *p;
p=ptr;
int i;
ptr = malloc (10);
>for (i =0;i<10;i++)
ptr[i++] = 2;
>for (i =0;i<10;i++)
printf("%d\n",p[i]);
}
>the output is something like this
>0
4096
132617
-1075507061
4
12643904
3
100
0
2
Unsurprising.
Track the values of the variable 'i'.

--
Chris.
Apr 10 '08 #2
pr****************@gmail.com wrote:
main()
{

int *ptr;
int *p;
p=ptr;
int i;
ptr = malloc (10);
<snip>

It's wrong here. 'ptr' gets a new value via calling malloc while 'p' still
uses its old one, i.e. an uninitialized value.

--
Hi, I'm a .signature virus, please copy/paste me to help me spread
all over the world.
Apr 10 '08 #3
On Apr 10, 2:07*pm, WANG Cong <xiyou.wangc...@gmail.comwrote:
prashant.khade1...@gmail.com wrote:
main()
{
int *ptr;
int *p;
p=ptr;
int i;
ptr = malloc (10);

<snip>

It's wrong here. 'ptr' gets a new value via calling malloc while 'p' still
uses its old one, i.e. an uninitialized value.

--
Hi, I'm a .signature virus, please copy/paste me to help me spread
all over the world.

I have changed the place where p = ptr. But still I am getting wrong
answer

#include<stdio.h>
main()
{

int *ptr;
int *p;
int i;
ptr = malloc (10);

p=ptr;
for (i =0;i<10;i++)
ptr[i++] = 2;

for (i =0;i<10;i++)
printf("%d\n",p[i]);

}
~
~
2
0
2
135153
2
0
2
0
2
0

Apr 10 '08 #4
"pr****************@gmail.com" <pr****************@gmail.comwrites:
On Apr 10, 2:07Â*pm, WANG Cong <xiyou.wangc...@gmail.comwrote:
>prashant.khade1...@gmail.com wrote:
main()
{
int *ptr;
int *p;
p=ptr;
int i;
ptr = malloc (10);

<snip>

It's wrong here. 'ptr' gets a new value via calling malloc while 'p' still
uses its old one, i.e. an uninitialized value.

--
Hi, I'm a .signature virus, please copy/paste me to help me spread
all over the world.


I have changed the place where p = ptr. But still I am getting wrong
answer

#include<stdio.h>
main()
{

int *ptr;
int *p;
int i;
ptr = malloc (10);
You probably meant something like this:

ptr = malloc(sizeof(i)*10);

>
p=ptr;
for (i =0;i<10;i++)
ptr[i++] = 2;
did you mean this instead?

for (i =0;i<10;i++)
ptr[i] = 2;

Apr 10 '08 #5
"pr****************@gmail.com" <pr****************@gmail.comwrites:
main()
int main(void) is much better.
{

int *ptr;
int *p;
p=ptr;
int i;
ptr = malloc (10);
No one has yet said that malloc does not know what you are
allocating. You can't just say 10, because that allocates 10 bytes.
You need malloc(10 * sizeof *ptr) which means, rather literally,
"allocate space big enough for 10 things of the type ptr points to".
for (i =0;i<10;i++)
ptr[i++] = 2;

for (i =0;i<10;i++)
printf("%d\n",p[i]);
As already pointed out, p is not properly set. It does not point at
an array whose elements you can print like this.
}
--
Ben.
Apr 10 '08 #6
On Apr 10, 3:36*pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
"prashant.khade1...@gmail.com" <prashant.khade1...@gmail.comwrites:
main()

int main(void) is much better.
{
int *ptr;
int *p;
p=ptr;
int i;
ptr = malloc (10);

No one has yet said that malloc does not know what you are
allocating. *You can't just say 10, because that allocates 10 bytes.
You need malloc(10 * sizeof *ptr) which means, rather literally,
"allocate space big enough for 10 things of the type ptr points to".
for (i =0;i<10;i++)
ptr[i++] = 2;
for (i =0;i<10;i++)
printf("%d\n",p[i]);

As already pointed out, p is not properly set. *It does not point at
an array whose elements you can print like this.
}

--
Ben.


Thanks...

I got you....

thanks for the help
Apr 10 '08 #7
pr****************@gmail.com wrote:
[snip]
I have changed the place where p = ptr. But still I am getting wrong
answer

#include<stdio.h>
main()
{

int *ptr;
int *p;
int i;
ptr = malloc (10);

p=ptr;
for (i =0;i<10;i++)
ptr[i++] = 2;
Please note that you increment i /twice/ each time you loop through this
for() loop

[1] i starts off at 0

[2] i is tested to ensure that it is less than zero (failure exits the loop)

[3] ptr[i] is set to 2 and i is incremented by 1

[4] i is incremented by 1, and the loop is repeated at the exit test ([2])

The effect of all this is that you only set even number index values of
ptr[] to 2, leaving all the odd number index values of ptr[] set to their
original (uninitialized) values
for (i =0;i<10;i++)
printf("%d\n",p[i]);
This loop cycles through /all/ entries in the array, including the ones left
uninitialized by the previous loop
}
~
~
2
0
2
135153
2
0
2
0
2
0
--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
Apr 10 '08 #8
Lew Pitcher wrote:
pr****************@gmail.com wrote:
[snip]
>I have changed the place where p = ptr. But still I am getting wrong
answer

#include<stdio.h>
main()
{

int *ptr;
int *p;
int i;
ptr = malloc (10);

p=ptr;
for (i =0;i<10;i++)
ptr[i++] = 2;

Please note that you increment i /twice/ each time you loop through this
for() loop

[1] i starts off at 0

[2] i is tested to ensure that it is less than zero (failure exits the
[loop)
**Correction**
[2] i is tested to ensure that it is less than /ten/ (failure exits the
[loop)

>
[3] ptr[i] is set to 2 and i is incremented by 1

[4] i is incremented by 1, and the loop is repeated at the exit test ([2])

The effect of all this is that you only set even number index values of
ptr[] to 2, leaving all the odd number index values of ptr[] set to their
original (uninitialized) values
>for (i =0;i<10;i++)
printf("%d\n",p[i]);

This loop cycles through /all/ entries in the array, including the ones
left uninitialized by the previous loop
>}
~
~
2
0
2
135153
2
0
2
0
2
0
--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
Apr 10 '08 #9
pr****************@gmail.com asked about a program with undefined
behavior and was concerned about its (predictably) unpredictable behavior:

I have provided a replacement below. Each of the changes is for a
reason. Please read it and think about why that might be so.
main()
{

int *ptr;
int *p;
p=ptr;
int i;
ptr = malloc (10);

for (i =0;i<10;i++)
ptr[i++] = 2;

for (i =0;i<10;i++)
printf("%d\n",p[i]);
}
the output is something like this
[...]

#include <stdio.h>
#include <stdlib.h>

int main(void)
{

int *ptr;
int *p;
#if 0
p = ptr; /* mha: for pre-C99 compilers (most),
this cannot precede the declaration
of i. Nor does ptr have a defined
value at this point. These errors
are fixed by moving this down a
line. */
#endif
int i;
ptr = malloc(10); /* mha: I have not added the error
check for this line. You should
consider doing so */
p = ptr; /* mha: new home of this line */

for (i = 0; i < 10; i++)
#if 0
ptr[i++] = 2; /* mha: it is possible that you mean
this, but unlikely. You are
incrementing i twice, once in the
line above and in this line.
Consider the replacement below. */
#endif
ptr[i] = 2; /* mha: the replacement */

for (i = 0; i < 10; i++)
printf("%d\n", p[i]);
free(ptr); /* mha: added. Always free() any
memory dynamically allocated. */
return 0;
}

Apr 10 '08 #10
"pr****************@gmail.com" wrote:
[...]
I have changed the place where p = ptr. But still I am getting wrong
answer

#include<stdio.h>
main()
{

int *ptr;
int *p;
int i;
ptr = malloc (10);
Here, you allocate 10 chars, not 10 ints. You want:

ptr = malloc(10 * sizeof(*ptr));
p=ptr;
for (i =0;i<10;i++)
ptr[i++] = 2;
How many times is "i++" executed each time through the loop?
for (i =0;i<10;i++)
printf("%d\n",p[i]);

}
[...]

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Apr 10 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: gb | last post by:
Hi All, Ive created a popup page using 'var openWindow = window.open("new","pop")' And added content using openWindow.document.write(" "); statements. But now I would like to be able to print...
0
by: Programatix | last post by:
Hi, I am working on the PrintDocument, PrintDialog, PageSetupDialog and PrintPreviewControl components of Visual Studio .NET 2003. My developement machine is running Windows XP. There are...
0
by: Programatix | last post by:
Hi, I am working on the PrintDocument, PrintDialog, PageSetupDialog and PrintPreviewControl components of Visual Studio .NET 2003. My developement machine is running Windows XP. There are...
1
by: G.Esmeijer | last post by:
Friends, I'm (a bit) dissapointed about the printing possibilities in c#. It could also be lack of knowedge. I would like to align a text on the right side of the paper when printing. For...
5
by: VMI | last post by:
I have a BMP image (the form is also in PDF) that contains a scanned copy of a paper form that we need to fill out. Is it possible to use this image in my application so that the application can...
1
by: dt | last post by:
Hello, I'm writing a simple application in Visual C++. The application basically takes in a set of values (of type double and int) and outputs a set of values. Both the input and output is...
1
by: Fred Nelson | last post by:
I'm a newby and I'm writing my first VB.NET print program! I need to change the default left and right margins for my printed output "e" is my "System.Drawing.Printing.PrintPageEventArgs By...
7
by: DazedAndConfused | last post by:
I have a 8.5 x 11 landscape document with about 1/4 inch of space on the left and right where there is no print. The document displays perfect in print preview, but when I print it, about 1/2 inch...
6
by: Siv | last post by:
Hi, I am getting into printing with VB.NET 2005 and want to implement the usual capability that a user can select a selection of pages. I have a report that is generated by my application that if...
0
it0ny
by: it0ny | last post by:
Hi guys, thanks I am fairly new to this forum so I hope I chose the right place to post this question. I try to make my program printout a deposit's report. I created a class to store the...
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...
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
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
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?
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...

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.