it is not printing the right values

P

prashant.khade1623

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);
}


the output is something like this

0
4096
132617
-1075507061
4
12643904
3
100
0
2
 
W

WANG Cong

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.
 
P

prashant.khade1623

<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.


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);

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

Richard

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 = 2;
 
B

Ben Bacarisse


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);


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

prashant.khade1623


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);


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




Thanks...

I got you....

thanks for the help
 
L

Lew Pitcher

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 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);


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. ------
 
L

Lew Pitcher

Lew said:
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 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);


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. ------
 
M

Martin Ambuhl

(e-mail address removed) 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);
}


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 = 2; /* mha: the replacement */

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

Kenneth Brody

:
[...]
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);

}

[...]

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top