Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > it is not printing the right values

Reply
Thread Tools

it is not printing the right values

 
 
prashant.khade1623@gmail.com
Guest
Posts: n/a
 
      04-10-2008
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

 
Reply With Quote
 
 
 
 
Chris McDonald
Guest
Posts: n/a
 
      04-10-2008
"(E-Mail Removed)" <(E-Mail Removed)> writes:

>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.
 
Reply With Quote
 
 
 
 
WANG Cong
Guest
Posts: n/a
 
      04-10-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
prashant.khade1623@gmail.com
Guest
Posts: n/a
 
      04-10-2008
On Apr 10, 2:07*pm, WANG Cong <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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

 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      04-10-2008
"(E-Mail Removed)" <(E-Mail Removed)> writes:

> On Apr 10, 2:07*pm, WANG Cong <(E-Mail Removed)> wrote:
>> (E-Mail Removed) 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;



 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      04-10-2008
"(E-Mail Removed)" <(E-Mail Removed)> writes:

> 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.
 
Reply With Quote
 
prashant.khade1623@gmail.com
Guest
Posts: n/a
 
      04-10-2008
On Apr 10, 3:36*pm, Ben Bacarisse <(E-Mail Removed)> wrote:
> "(E-Mail Removed)" <(E-Mail Removed)> writes:
> > 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
 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      04-10-2008
(E-Mail Removed) 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. ------


 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      04-10-2008
Lew Pitcher wrote:

> (E-Mail Removed) 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. ------


 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      04-10-2008
(E-Mail 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[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;
}



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:52 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:50 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:28 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-18-2007 10:11 AM
Right-click context menu that can pass values when right-clicking a link?? Leon Javascript 3 11-26-2004 07:37 AM



Advertisments