473,789 Members | 2,799 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

printing values of "arrays of pointers"

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[i] = mp;
}
arr_p[i] = np;

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

return 0;
}

--
http://lispmachine.wordpress.com/
my email ID is @ the above address

Jun 27 '08 #1
24 2195
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[i] = 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[i] = 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. 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);
}
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.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #2
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/200...urn-for-vbnet/
or RCX.
never heard of it
--
http://lispmachine.wordpress.com/
my email ID is @ the above address

Jun 27 '08 #3

"arnuld" <No****@microso ft.comwrote in message
news:pa******** *************** *****@microsoft .com...
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[i] = malloc( sizeof( int ) );
*arr_p[i] = 1000+i; /* initial the ints to some recognisable
(and non-0!) value */
}
arr_p[i] = np; /* sentinel (*np contains 0) */

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

return 0;
}

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

arnuld <No****@microso ft.comwrites:
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.
Jun 27 '08 #5
arnuld wrote:
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[i] = mp;
}
arr_p[i] = np;

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

return 0;
}
/* BEGIN new.c */
/*
** 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>

#define MAXSIZE 100

void free_ptrs(int **p, size_t n);

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

puts("/* BEGIN new.c output */\n");
for (i = 0; i < MAXSIZE - 1; ++i ) {
arr_p[i] = malloc(sizeof *arr_p[i]);
if (arr_p[i] == NULL) {
printf("arr_p[%d] == NULL\n", i);
break;
}
*arr_p[i] = i;
}
arr_p[i] = NULL;
for (pp = arr_p; *pp != NULL; ++pp) {
printf("**pp = %d\n", **pp);
}
free_ptrs(arr_p , i);
puts("\n/* END new.c output */");
return 0;
}

void free_ptrs(int **p, size_t n)
{
while (n-- != 0) {
free(*p);
*p++ = NULL;
}
}

/* END new.c */

--
pete
Jun 27 '08 #6

"santosh" <sa*********@gm ail.comwrote in message
news:fv******** **@registered.m otzarella.org.. .
arnuld wrote:

Path: news.motzarella .org!motzarella .org!newsfeed.s traub-nv.de!
goblin1!goblin2 !goblin.stu.nev a.ru!news.net.u ni-c.dk!dotsrc.org !
filter.dotsrc.o rg!news.dotsrc. org!not-for-mail
From: arnuld <No****@microso ft.com>
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: so*****@microso ft.com"

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

-- Bartc

Jun 27 '08 #7
In article <fv**********@r egistered.motza rella.org>,
santosh <sa*********@gm ail.comwrote:
>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
Jun 27 '08 #8
Richard Tobin wrote:
In article <fv**********@r egistered.motza rella.org>,
santosh <sa*********@gm ail.comwrote:
>>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
misrepresentati on. 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.

Jun 27 '08 #9
santosh wrote, On 03/05/08 11:51:
Richard Tobin wrote:
>In article <fv**********@r egistered.motza rella.org>,
santosh <sa*********@gm ail.comwrote:
>>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
misrepresentati on. 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
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
11212
by: Abdullah Kauchali | last post by:
Hi folks, Can one rely on the order of keys inserted into an associative Javascript array? For example: var o = new Object(); o = "Adam"; o = "Eve";
388
21935
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's worth the $25USD. I'm just looking for a book on Pointers, because from what I've read it's one of the toughest topics to understand. thanks in advanced.
46
2271
by: TTroy | last post by:
Hi, I'm just wondering why people/books/experts say "the function returns a pointer to.." or "we have to send scanf a pointer to.." instead of "the function returns the address of.." or "we have to send scanf the address of.." Isn't the lvalue called a POINTER TO and the (r)value called the ADDRESS OF?
2
3227
by: Alfonso Morra | last post by:
Hi, I want to create a container structure (array/list etc), that is expandable. Currently, I have static arrays like this SomeStruct *ptrArray ; What I want to do is to be able to use ptrs to ptrs like so:
14
2838
by: Alf P. Steinbach | last post by:
Not yet perfect, but: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01_examples.zip To access the table of contents, use the "Bookmarks" tab in Adobe Acrobat. Comments, corrections, praise, nits, etc., are still welcome!
0
10199
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6768
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4092
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3697
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.