printing values of "arrays of pointers"

A

arnuld

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

arnuld

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

done

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



never heard of it
 
B

Bartc

arnuld said:
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
 
B

Ben Bacarisse

[BTW, there seems to be somthing odd in the way you newsreader add
attribution lines. I've fixed it, I think.]

arnuld said:
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.
 
S

santosh

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 <[email protected]>

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: <[email protected]>
Newsgroups: comp.lang.c
References: <[email protected]>
<[email protected]>
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: (e-mail address removed)
Xref: news.motzarella.org comp.lang.c:30654203
 
B

Bartc

santosh said:
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 <[email protected]>
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: (e-mail address removed)"

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

-- Bartc
 
R

Richard Tobin

santosh said:
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
 
S

santosh

Richard said:
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.
 
F

Flash Gordon

santosh wrote, On 03/05/08 11:51:
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.
 
A

arnuld

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:
(e-mail address removed), as default address.
 
A

arnuld

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

arnuld

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, (e-mail address removed) 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 (e-mail address removed) 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)
 
K

Keith Thompson

arnuld said:
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>
 
A

arnuld

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 :(
 
N

Nick Keighley

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.

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

Nick Keighley

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

Nick Keighley

Nick Keighley said:
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).
 
K

Keith Thompson

Nick Keighley said:
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>
 
D

David Thompson

arnuld wrote:
how about [ <[email protected]> ]? 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
 

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

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,900
Latest member
Nell636132

Latest Threads

Top