|
From: Per W. <pw...@ia...> - 2004-01-31 17:54:17
|
Hmmm. I assume your example was ment to be: int x,y; int *p = &x; /* Don't forget the address operator here... */ p += 1; /Per W On Sat, 31 Jan 2004, Michael C Urban wrote: > On Sat, 31 Jan 2004, Brian Gordon wrote: > > > I'm new to C++ and in my C++ book, I am learning about pointers. I have > > a question: can you store an address to some kind of "omnitype" pointer? > > I am thinking about doing something like this: > > Although in my last message I showed you how to do what you are asking, I > would also stress that directly manipulating pointer addresses is usually > quite risky unless you are doing some kind of very low level programming > (like programming a microcontroller). Pointer address manipulation is only > guranteed to behave well if you are dealing with an array, where you know > the the elements of the array are stored contigiously in memory. The > following code sample is a disaster waiting to happen: > > int x, y; > int *p = x; > p += 1; > > In this case, I have increased the address of *p by one integer offset. In > other words, I have increased the address that *p points to by 32 bits. So > *p no longer points at x. But does it point at y? Probably not, because > there is no gurantee that x and y are stroed contigiously in memory, even > though they were declared one right after the other. So in the above > sample, we cannot predict what the results will be since *p now points to > an address with unpredictable contents. (More then likely, *p now points > to garbage). > > Hope that clears things up, and hope I wasn't just telling you stuff you > already know. But you said you were new to C++, and it looked like what > you were doing was probably unsafe. > > Mike > > > ------------------------------------------------------- > The SF.Net email is sponsored by EclipseCon 2004 > Premiere Conference on Open Tools Development and Integration > See the breadth of Eclipse activity. February 3-5 in Anaheim, CA. > http://www.eclipsecon.org/osdn > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |