it is not printing the right values

Discussion in 'C Programming' started by prashant.khade1623@gmail.com, Apr 10, 2008.

  1. Guest

    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
    , Apr 10, 2008
    #1
    1. Advertising

  2. "" <> 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);
    >}



    >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.
    Chris McDonald, Apr 10, 2008
    #2
    1. Advertising

  3. WANG Cong Guest

    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.
    WANG Cong, Apr 10, 2008
    #3
  4. Guest

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

    }
    ~
    ~
    2
    0
    2
    135153
    2
    0
    2
    0
    2
    0
    , Apr 10, 2008
    #4
  5. Richard Guest

    "" <> writes:

    > On Apr 10, 2:07 pm, WANG Cong <> wrote:
    >> 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 = 2;
    Richard, Apr 10, 2008
    #5
  6. "" <> 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);


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

    > }


    --
    Ben.
    Ben Bacarisse, Apr 10, 2008
    #6
  7. Guest

    On Apr 10, 3:36 pm, Ben Bacarisse <> wrote:
    > "" <> 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);

    >
    > 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, 2008
    #7
  8. Lew Pitcher Guest

    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 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. ------
    Lew Pitcher, Apr 10, 2008
    #8
  9. Lew Pitcher Guest

    Lew Pitcher wrote:

    > 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 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. ------
    Lew Pitcher, Apr 10, 2008
    #9
  10. 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;
    }
    Martin Ambuhl, Apr 10, 2008
    #10
  11. "" 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);
    >
    > }

    [...]

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
    Kenneth Brody, Apr 10, 2008
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tom
    Replies:
    0
    Views:
    412
  2. =?Utf-8?B?QmlzaG95?=
    Replies:
    0
    Views:
    963
    =?Utf-8?B?QmlzaG95?=
    Dec 28, 2006
  3. Bogdan
    Replies:
    1
    Views:
    769
    Bogdan
    Jun 16, 2009
  4. Leon
    Replies:
    3
    Views:
    161
    TaeHo Yoo
    Nov 26, 2004
  5. Oran
    Replies:
    2
    Views:
    512
Loading...

Share This Page