473,803 Members | 2,909 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
24 2196
Nick Keighley said:
On 2 May, 14:44, Richard Heathfield <r...@see.sig.i nvalidwrote:
<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.

--
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 #21
On 6 May, 14:44, Richard Heathfield <r...@see.sig.i nvalidwrote:
Nick Keighley said:
On 2 May, 14:44, Richard Heathfield <r...@see.sig.i nvalidwrote:

<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

Jun 27 '08 #22
Nick Keighley <ni************ ******@hotmail. comwrites:
[...]
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_Keit h) <ks***@mib.or g>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #23
Nick Keighley wrote:
>
.... snip ...
>
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).
If you define it within the for statement you get the Pascal
effect. E.g. try: "for (int i = 0; i < max; i++) ...;". i
disappears on exit.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #24
On Mon, 05 May 2008 02:25:53 -0400, CBFalconer <cb********@yah oo.com>
wrote:
arnuld wrote:
how about [ <su*****@domain .tld]? 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.ne t
Jun 27 '08 #25

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

Similar topics

27
11215
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
21948
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
2274
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
2840
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
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10542
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10289
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
9119
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7600
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
5496
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4274
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
3
2968
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.