printing values of "arrays of pointers"

Discussion in 'C Programming' started by arnuld, May 2, 2008.

  1. arnuld

    arnuld Guest

    PURPOSE: see the comments.
    WHAT I GOT: infinite loop



    /* This program will simply create an array of pointers to integers
    * and will fill it with some values while using malloc to create
    * pointers to fill the array and then will print the values pointed
    * by those pointers
    *
    */



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

    enum MAXSIZE { ARRSIZE = 100 };


    int main( void )
    {
    int* arr_p[ARRSIZE];
    int** pp;
    int *mp, *np;
    int i;

    int null_int = 0;

    pp = arr_p;
    np = &null_int;


    for( i = 0; i < ARRSIZE - 1; ++i )
    {
    mp = malloc( sizeof( int ) );
    mp = &i;
    arr_p = mp;
    }


    arr_p = np;

    while( **pp )
    {
    printf("**pp = %d\n", **pp);
    }

    return 0;
    }





    --
    http://lispmachine.wordpress.com/
    my email ID is @ the above address
     
    arnuld, May 2, 2008
    #1
    1. Advertising

  2. arnuld

    arnuld Guest

    > On Fri, 02 May 2008 13:44:50 +0000, Richard Heathfield wrote:


    >> for( i = 0; i < ARRSIZE - 1; ++i )
    >> {
    >> mp = malloc( sizeof( int ) );


    > You don't check to see whether this fails to allocate the requested
    > memory...


    done


    >> mp = &i;


    > ...which doesn't really matter, since you throw it away here.



    well, then how can I generate a new variable every-time I enter into the
    loop ?



    > Well done. You just filled arr_p with a bunch of pointers to i. Why bother
    > with the malloc call, then? You could save yourself (ARRSIZE-1) *
    > sizeof(int) bytes of memory leak by removing the malloc call completely.


    I can but I did not because I want to understand the how pointers to
    arrays to pointers behave and learn them.



    > You don't even /try/ to iterate through the array, do you?


    okay, here is a little fixed version. I can't do anything about variable i
    in for loop as I said, I can't find a way to generate new variables at
    every iteration through the loop.


    > You might want to take a long hard look at Visual Basic




    http://lispmachine.wordpress.com/2008/02/20/how-i-gave-away-my-youth-in-return-for-vbnet/



    > or RCX.


    never heard of it


    --
    http://lispmachine.wordpress.com/
    my email ID is @ the above address
     
    arnuld, May 2, 2008
    #2
    1. Advertising

  3. arnuld

    Bartc Guest

    "arnuld" <> wrote in message
    news:p...
    > PURPOSE: see the comments.
    > WHAT I GOT: infinite loop
    >


    I've modified your code so that it behaves better:


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

    enum MAXSIZE { ARRSIZE = 100 };


    int main( void )
    {
    int *arr_p[ARRSIZE];
    int **pp;
    int *np;
    int i;

    int null_int = 0;

    pp = arr_p;
    np = &null_int;


    for( i = 0; i < ARRSIZE - 1; ++i )
    {
    arr_p = malloc( sizeof( int ) );
    *arr_p = 1000+i; /* initial the ints to some recognisable
    (and non-0!) value */
    }


    arr_p = np; /* sentinel (*np contains 0) */

    while( **pp) /* Step through array using pp */
    {
    printf("**pp = %d\n", **pp);
    ++pp;
    }

    return 0;
    }

    -- Bartc
     
    Bartc, May 2, 2008
    #3
  4. [BTW, there seems to be somthing odd in the way you newsreader add
    attribution lines. I've fixed it, I think.]

    arnuld <> writes:
    > On Fri, 02 May 2008 13:44:50 +0000, Richard Heathfield wrote:
    >
    >>> for( i = 0; i < ARRSIZE - 1; ++i )
    >>> {
    >>> mp = malloc( sizeof( int ) );

    >
    >> You don't check to see whether this fails to allocate the requested
    >> memory...

    >
    > done
    >
    >>> mp = &i;

    >
    >> ...which doesn't really matter, since you throw it away here.

    >
    > well, then how can I generate a new variable every-time I enter into the
    > loop ?


    That is what malloc is for. C calls them objects, and you make them
    exactly as you have written it. The error is overwriting the pointer
    you get from malloc with one to i. Pointers to local variables like
    i (automatic storage in C terms) are very rarely used.

    --
    Ben.
     
    Ben Bacarisse, May 2, 2008
    #4
  5. arnuld

    santosh Guest

    arnuld wrote:

    Path: news.motzarella.org!motzarella.org!newsfeed.straub-nv.de!
    goblin1!goblin2!goblin.stu.neva.ru!news.net.uni-c.dk!dotsrc.org!
    filter.dotsrc.org!news.dotsrc.org!not-for-mail
    From: arnuld <>

    Ah! I see that you've taken precisely the inverse of the advice at one
    of the links I gave your earlier.

    Are you aware that this bit of smartness could get you banned by your
    news server? I'm not sure, but I also think that Microsoft *could* sue
    you. Mind you, not that they're going to ever bother with such minute
    plankton, when they have bigger fish to fry.

    You have no legal right to use that email address unless you are an
    employee of Microsoft and have received their permission to do so.

    Subject: Re: printing values of "arrays of pointers"
    Date: Fri, 02 May 2008 19:05:36 +0500
    User-Agent: Pan/0.14.2 (This is not a psychotic episode. It's
    cleansing moment of clarity.)
    Message-Id: <>
    Newsgroups: comp.lang.c
    References: <>
    <>
    MIME-Version: 1.0
    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: 8bit
    Lines: 56
    Organization: SunSITE.dk - Supporting Open source
    NNTP-Posting-Host: 121.246.233.222
    X-Trace: news.sunsite.dk DXC=_V
    [g3R;E0oDTKD0DTdP>YSB=nbEKnk;L?8>K\5E[I;]`AOkTH[3o6lP<B6S
    1\>;C<okYMidCE<;]_OU6jcM6:f60l:OJoZT67UB^cTY:nb4
    X-Complaints-To:
    Xref: news.motzarella.org comp.lang.c:30654203
     
    santosh, May 3, 2008
    #5
  6. arnuld

    Bartc Guest

    "santosh" <> wrote in message
    news:fvh64c$mht$...
    > arnuld wrote:
    >
    > Path: news.motzarella.org!motzarella.org!newsfeed.straub-nv.de!
    > goblin1!goblin2!goblin.stu.neva.ru!news.net.uni-c.dk!dotsrc.org!
    > filter.dotsrc.org!news.dotsrc.org!not-for-mail
    > From: arnuld <>


    > You have no legal right to use that email address unless you are an
    > employee of Microsoft and have received their permission to do so.


    Don't know the OP's newsreader but: if you open a news account with Outlook
    Express, it does ask:

    "Email address: ..... for example: "

    Could someone really be sued for following this advice too literally?

    -- Bartc
     
    Bartc, May 3, 2008
    #6
  7. In article <fvh64c$mht$>,
    santosh <> wrote:

    >You have no legal right to use that email address unless you are an
    >employee of Microsoft and have received their permission to do so.


    Do you have any basis for this claim? I don't recall any laws being
    passed on the subject. Trademark law isn't relevant, since he's not
    engaged in trade. He is clearly not using their name for any
    fraudulent purpose. He's not misrepresenting himself (even if that
    were illegal) since no-one would take it as a real email address.
    RFCs have no legal force.

    Nothing stops me from going around wearing a badge saying "Bill Gates,
    Microsoft", unless I do it for some illegal purpose. Why should a
    header in a news posting be any different?

    At most he might be violating his ISP's terms and conditions.

    -- Richard
    --
    :wq
     
    Richard Tobin, May 3, 2008
    #7
  8. arnuld

    santosh Guest

    Richard Tobin wrote:

    > In article <fvh64c$mht$>,
    > santosh <> wrote:
    >
    >>You have no legal right to use that email address unless you are an
    >>employee of Microsoft and have received their permission to do so.

    >
    > Do you have any basis for this claim? I don't recall any laws being
    > passed on the subject. Trademark law isn't relevant, since he's not
    > engaged in trade. He is clearly not using their name for any
    > fraudulent purpose. He's not misrepresenting himself (even if that
    > were illegal) since no-one would take it as a real email address.
    > RFCs have no legal force.
    >
    > Nothing stops me from going around wearing a badge saying "Bill Gates,
    > Microsoft", unless I do it for some illegal purpose. Why should a
    > header in a news posting be any different?
    >
    > At most he might be violating his ISP's terms and conditions.


    I don't know about other countries, but I believe we have rules in our
    so-called "Cyber Laws" that prohibit willfull impersonation and
    misrepresentation. This would be particularly applicable if arnuld were
    to comment upon Microsoft under this false email address. And why would
    no one take it as a real email address? It's a perfectly valid domain
    and hostname, though the latter may not exist. It's not a proper munged
    address, nor an invalid address.

    More practically Sunsite could terminate his account with them if they
    were to be alerted to his behaviour. Their rules clearly say that users
    are only to use their own email addresses which actually exist and can
    be delivered to, in the Sender field.
     
    santosh, May 3, 2008
    #8
  9. arnuld

    Flash Gordon Guest

    santosh wrote, On 03/05/08 11:51:
    > Richard Tobin wrote:
    >
    >> In article <fvh64c$mht$>,
    >> santosh <> wrote:
    >>
    >>> You have no legal right to use that email address unless you are an
    >>> employee of Microsoft and have received their permission to do so.

    >> Do you have any basis for this claim? I don't recall any laws being
    >> passed on the subject. Trademark law isn't relevant, since he's not


    <snip>

    > I don't know about other countries, but I believe we have rules in our
    > so-called "Cyber Laws" that prohibit willfull impersonation and
    > misrepresentation. This would be particularly applicable if arnuld were
    > to comment upon Microsoft under this false email address. And why would
    > no one take it as a real email address? It's a perfectly valid domain
    > and hostname, though the latter may not exist. It's not a proper munged
    > address, nor an invalid address.


    Also some of us (well, me anyway) deliberately use addresses in our
    domains that look like spamtraps. I've found that spam@ gets spammed
    less than other addresses I've used.

    > More practically Sunsite could terminate his account with them if they
    > were to be alerted to his behaviour. Their rules clearly say that users
    > are only to use their own email addresses which actually exist and can
    > be delivered to, in the Sender field.


    Ah well, I'm sure at some point someone will decide to report him to
    them if he continues.
    --
    Flash Gordon
     
    Flash Gordon, May 3, 2008
    #9
  10. arnuld

    arnuld Guest

    > On Sat, 03 May 2008 16:21:35 +0530, santosh wrote:


    > More practically Sunsite could terminate his account with them if they
    > were to be alerted to his behaviour. Their rules clearly say that users
    > are only to use their own email addresses which actually exist and can
    > be delivered to, in the Sender field.



    If I use my real email ID that means I am inviting 100% of spam. I don't
    want to be spammed and no one wants to be spammed.

    Regarding microsoft.com, I got that idea from my friend. He told me that
    when he is using Outlook Express on WindowsXP, he gets this:
    , as default address.



    --
    http://lispmachine.wordpress.com/
    my email ID is @ the above address
     
    arnuld, May 5, 2008
    #10
  11. arnuld

    arnuld Guest

    > On Sat, 03 May 2008 16:21:35 +0530, santosh wrote:


    > More practically Sunsite could terminate his account with them if they
    > were to be alerted to his behaviour. Their rules clearly say that users
    > are only to use their own email addresses which actually exist and can
    > be delivered to, in the Sender field.



    I think this Server Policy is good:

    http://news.aioe.org/spip.php?article3



    --
    http://lispmachine.wordpress.com/
    my email ID is @ the above address
     
    arnuld, May 5, 2008
    #11
  12. arnuld

    arnuld Guest

    > On Sat, 03 May 2008 16:21:35 +0530, santosh wrote:


    > More practically Sunsite could terminate his account with them if they
    > were to be alerted to his behaviour. Their rules clearly say that users
    > are only to use their own email addresses which actually exist and can
    > be delivered to, in the Sender field.



    I have mailed them and have asked them to change their policy.





    --
    http://lispmachine.wordpress.com/
    my email ID is @ the above address
     
    arnuld, May 5, 2008
    #12
  13. arnuld

    arnuld Guest

    > On Mon, 05 May 2008 04:48:11 +0000, Richard Heathfield wrote:

    > Nobody wants to be spammed. On the other hand, how would you like it if
    > someone used *your* real address in *their* From field?


    :(


    > All you have to do is invent an address that is in the correct form but
    > guaranteed not to exist. For example, lid is a well-formed
    > email address that definitely doesn't exist (because there is no top-level
    > domain called "invalid"). This is not an area where originality is
    > terribly important. If in doubt, use lid for From and
    > Reply-To, and mung your real email address in your sig block.



    how about my present address ? It doe not use any real domain name and is
    different enough from common words like spam, invalid, nospam etc.

    BTW, I don't see Reply-To header for my own posts, not even for the posts
    of BartC and santosh but I do see Reply-To header of yours. ( I have
    already enabled all headers in PAN preferences)



    --
    http://lispmachine.wordpress.com/
    my email ID is @ the above blog.
    just check the "About Myself" page :)
     
    arnuld, May 5, 2008
    #13
  14. arnuld <> writes:
    >> On Mon, 05 May 2008 02:25:53 -0400, CBFalconer wrote:
    >> No. People can create new domains and addresses at any time. The
    >> .invalid domain is guaranteed not to exist, and thus to never cause
    >> problems.

    >
    > so "invalid" is the only one that is usable :(


    <OT>
    No, it's not the only one, but it's the most appropriate for your
    purposes. Point your web browser to <http://example.com/> and follow
    the link for more information.

    But even if it were the only one, why would that be a problem? How
    many invalid TLDs do you need? (Hint: the correct answer is an
    integer between 0 and 2.)

    If you want to discuss this further, please find a newsgroup or other
    forum where it's topical.
    </OT>

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, May 5, 2008
    #14
  15. arnuld

    arnuld Guest

    > On Mon, 05 May 2008 02:25:53 -0400, CBFalconer wrote:

    > No. People can create new domains and addresses at any time. The
    > .invalid domain is guaranteed not to exist, and thus to never cause
    > problems.



    so "invalid" is the only one that is usable :(

    --
    http://lispmachine.wordpress.com/
    my email ID is @ the above blog.
    just check the "About Myself" page :)
     
    arnuld, May 6, 2008
    #15
  16. On 2 May, 14:44, Richard Heathfield <> wrote:
    > arnuld said:


    > > PURPOSE:         see the comments.
    > > WHAT I GOT:      infinite loop

    >
    > > /* This program will simply create an array of pointers to integers
    > >  * and will fill it with some values while using malloc to create
    > >  * pointers to fill the array and then will print the values pointed
    > >  * by those pointers
    > >  *
    > >  */

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

    >
    > > enum MAXSIZE { ARRSIZE = 100 };

    >
    > > int main( void )
    > > {
    > >   int* arr_p[ARRSIZE];
    > >   int** pp;
    > >   int *mp, *np;
    > >   int i;

    >
    > >   int null_int = 0;

    >
    > >   pp = arr_p;
    > >   np = &null_int;

    >
    > >   for( i = 0; i < ARRSIZE - 1; ++i )
    > >     {
    > >       mp = malloc( sizeof( int ) );

    >
    > You don't check to see whether this fails to allocate the requested
    > memory...
    >
    > >       mp = &i;

    >
    > ...which doesn't really matter, since you throw it away here.
    >
    > >       arr_p = mp;
    > >     }

    >
    > Well done. You just filled arr_p with a bunch of pointers to i. Why
    > bother with the malloc call, then? You could save yourself
    > (ARRSIZE-1) * sizeof(int) bytes of memory leak by removing the malloc > call completely.
    >
    > >   arr_p = np;

    >
    > >   while( **pp )

    >
    > pp has the value &arr_p[0], so **pp attempts to find the int value at
    > *arr_p[0]. Since arr_p[0] points to i, we're looking for the value of > i.
    > That value is indeterminate, because the object i never had a value
    > assigned to it.


    not even in the for loop?


    > Thus, the behaviour is undefined. This loop could execute
    > forever (the most likely possibility) or not at all (probably the
    > second most likely) or something completely weird could happen.
    >
    > >     {
    > >       printf("**pp = %d\n", **pp);


    if you want to print a pointer (and **pp *isn't a pointer)
    use %p. Instead of trying to be clever why not use a for loop?

    for (i = 0; i < ARRSIZE; i++)
    printf ("%p ", (void*)arr_p);

    printf ("\n");


    > >     }

    >
    > You don't even /try/ to iterate through the array, do you? You might
    > want
    > to take a long hard look at Visual Basic or RCX.



    --
    Nick Keighley

    My god it's full of stars!
    Dave Bowman, on seeing HAL's source code
     
    Nick Keighley, May 6, 2008
    #16
  17. On 2 May, 15:05, arnuld <> wrote:
    > > On Fri, 02 May 2008 13:44:50 +0000, Richard Heathfield wrote:


    > >>   for( i = 0; i < ARRSIZE - 1; ++i )
    > >>     {
    > >>       mp = malloc( sizeof( int ) );


    > > You don't check to see whether this fails to allocate the requested
    > > memory...

    >
    > done
    >
    > >>       mp = &i;

    > > ...which doesn't really matter, since you throw it away here.

    >
    > well, then how can I generate a new variable every-time I enter into
    > the loop ?


    you can't. Well you can but you don't want to.

    for( i = 0; i < ARRSIZE - 1; ++i )
    {
    int new;
    arr_p = &new;
    }

    The Standard doesn't say if new has the same address every time,
    but on almost any reasonable implementation it does. Hence you
    don't want to do this.

    So why not malloc an object and store its address in the array?
    You probably meant to do that but your code really didn't
    do that.

    for( i = 0; i < ARRSIZE - 1; ++i )
    {
    if ((mp = malloc (sizeof (int))) == NULL)
    {
    fprintf (stderr, "memory error\n");
    abort();
    }

    arr_p = mp;
    }


    <snip>

    --
    Nick Keighley

    -pedantic
    This option is not intended to be useful; it exists only to satisfy
    pedants who would otherwise claim that GNU CC fails to support the
    ANSI standard.
    (Using and Porting GNU CC)
     
    Nick Keighley, May 6, 2008
    #17
  18. On 6 May, 14:44, Richard Heathfield <> wrote:
    > Nick Keighley said:
    >
    > > On 2 May, 14:44, Richard Heathfield <> wrote:

    >
    > <snip>
    >
    > >> Since arr_p[0] points to i, we're looking for the value of >
    > >> i. That value is indeterminate, because the object i never had a value
    > >> assigned to it.

    >
    > > not even in the for loop?

    >
    > Oops - good spot - apologies to OP.


    some languages make it UB to acces the for loop variable
    outside the loop (I think- Pascal, Alogol-60 and Ada)
    but I think it is guaranteed to point one past the end
    of the array after exiting from a "normal" for-loop.
    (I can't be arsed to define the exit value in Standarese).


    --
    Nick Keighley
     
    Nick Keighley, May 6, 2008
    #18
  19. Nick Keighley <> writes:
    [...]
    > some languages make it UB to acces the for loop variable
    > outside the loop (I think- Pascal, Alogol-60 and Ada)
    > but I think it is guaranteed to point one past the end
    > of the array after exiting from a "normal" for-loop.
    > (I can't be arsed to define the exit value in Standarese).


    There's nothing magical about a C90-style for loop. A variable that
    happens to be used in one or more of the for loop header's expressions
    has whatever value it has after the loop terminates. This:

    int i;
    for (i = 0; i < 100; i ++) {
    /* code that doesn't modify i */
    }
    printf("i = %d\n", i);

    will print "i = 100".

    C99 introduces the ability to define a variable in the loop header:

    for (int i = 0; i < 100; i ++) { ... }

    Referring to such a variable outside the loop isn't merely undefined
    bhaevior, it's impossible. (Well, you could save its address in a
    pointer variable; dereferencing the pointer outside the loop would
    invoke undefined behavior.)

    For comparison ...

    <OT>
    In Pascal, a for loop specifies a range. I *think* the value is
    unspecified after the loop terminates, but I don't remember -- and it
    probably varies among various implementations and pseudo-standards. I
    don't know about Algol-60. Ada's for loop is much more restrictive
    than C's:
    for I in 0 .. 99 loop
    ...
    end loop;
    ``I'' is treated as a constant (a non-modifiable object) within the
    body of the loop, and doesn't exist outside the loop. You could take
    its address and try to refer to it from outside the loop, as you can
    in C, but Ada discourages that kind of thing; if you do it anyway, you
    get Ada's equivalent of undefined behavior.
    </OT>

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, May 6, 2008
    #19
  20. On Mon, 05 May 2008 02:25:53 -0400, CBFalconer <>
    wrote:

    > arnuld wrote:


    > > how about [ <> ]? It doe not use any real domain
    > > name and is different enough from common words like spam, invalid,
    > > nospam etc.

    >
    > No. People can create new domains and addresses at any time. The
    > .invalid domain is guaranteed not to exist, and thus to never cause
    > problems.


    With profuse apologies to Kilmer, only ICANN can make a TLD.
    And except for 2char ccTLDs which defer to ISO 3162 MA,
    their process is quite public and slow enough you can get plenty of
    warning of a possible conflict. But .invalid is indeed guaranteed.

    - formerly david.thompson1 || achar(64) || worldnet.att.net
     
    David Thompson, May 19, 2008
    #20
    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. Jon B
    Replies:
    7
    Views:
    7,556
    =?Utf-8?B?SmFzb25DaG9p?=
    Jan 30, 2006
  2. JustSomeGuy
    Replies:
    13
    Views:
    519
    msalters
    Dec 9, 2004
  3. Replies:
    1
    Views:
    663
    -berlin.de
    Mar 28, 2005
  4. iffy agbim
    Replies:
    1
    Views:
    114
    Mark Andrews
    May 24, 2004
  5. iffy agbim
    Replies:
    0
    Views:
    113
    iffy agbim
    May 21, 2004
Loading...

Share This Page