0

I'm trying to do something very simple, at least I thought, with no success. I would like to assign the address pointed to by an integer pointer to the address pointed to by a char pointer. Example

//C++

int *pointerint;
char *pointerchar;
pointerchar = pointerint;

//

I've tried to do this severals different ways without success.

Example:

pointerchar = (char *) &pointerint;

The compiler excepts it but the address assigned to the pointer is zero. That can not be correct. Can anyone tell me how to do this correctly and what I am doing wrong. Thanks in advance for all your help.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 3
    This is indeed very simple to do as you can see from the answers ... but why do you want to do it? It's a generally dangerous operation, especially in the hands a rookie. – Jim Balter Sep 26 '12 at 19:10

2 Answers2

3

Try (if C):

pointerchar = (char *) pointerint;
ouah
  • 142,963
  • 15
  • 272
  • 331
  • 1
    This is correct, but to extend, what you did there is typecasted the address of the pointer. Since `pointerint` is already a pointer, you don't need to use the `&` operator again. – slugonamission Sep 26 '12 at 19:09
  • Here I convert the value of the pointer and not the address of the pointer (what the OP did). – ouah Sep 26 '12 at 19:10
  • @LuchianGrigore I assume he initializes the `pointerint` pointer. – ouah Sep 26 '12 at 19:11
  • Even if pointerint isn't initialised, it will work. You'd just be copying the number across. All you're changing is the semantic meaning of the data that is pointed to. – slugonamission Sep 26 '12 at 19:12
  • @slugonamission a non-initialized pointer is an invalid pointer and accessing (even just reading it) an invalid pointer is undefined behavior. – ouah Sep 26 '12 at 19:13
  • @slugonamission nope. What number? There's no number to speak of because it's not initialized. Reading from an uninitialized variable is UB. – Luchian Grigore Sep 26 '12 at 19:13
  • The value of the pointer is undefined, but reading it will still yield a, albeit undefined, value. – slugonamission Sep 26 '12 at 19:14
  • @slugonamission no, reading from it will yield undefined behavior. – Luchian Grigore Sep 26 '12 at 19:14
  • @LuchianGrigore to the extent that the value returned from it will be undefined, and the side effects potentially undefined. Since it will consume stack space using most compilers, there is still some data there. You just don't know what it is. – slugonamission Sep 26 '12 at 19:16
  • @slugonamission do you know what undefined behavior means? – Luchian Grigore Sep 26 '12 at 19:16
  • @slugonamission Not only did you post a comment in the wrong place (it should have been posted to the OP's question, where the address was typecast), your other claims here are incorrect. You are debating with people with a far better understanding of the language standard. – Jim Balter Sep 26 '12 at 19:19
  • Ok, I'm wrong in this case, sorry. I never realised in C that indeterminate can refer to a trap value. – slugonamission Sep 26 '12 at 19:21
  • @slugonamission Here's the thing: in practice, in nearly all implementations, a pointer is represented by "just a number", and an assignment from an uninitialized pointer just copies that representation. But that's not what the C standard says, and it's a good mental habit to keep in mind the formalism ... type safety is essential to robust software. – Jim Balter Sep 26 '12 at 19:26
  • There's more discussion about this at http://stackoverflow.com/questions/1597405/what-happens-to-a-declared-uninitialized-variable-in-c-does-it-have-a-value – Jim Balter Sep 26 '12 at 19:34
  • OK, here it is. Actually the first answer was correct, all I really need to do is assign the pointerint to the pointerchar, with out the ampersand. So you were right there, however also the pointerint needs to be initialized first, so as to not allow the compiler to assign a zero value to the address. Soooo...what a should have done was.... //C++ int *pointerint, dummy; char *pointerchar; Dummy = 32; pointerint = &dummy; pointerchar = pointerint; pointerchar-=3;// – user1701238 Sep 26 '12 at 21:43
3

Now that you have a C answer, here's a C++ one:

int *pointerint;  
char *pointerchar;
//initialize pointerint
pointerchar = reinterpret_cast<char*>(pointerint);

Note that if you don't initialize the pointerint, you'll run into undefined behavior.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625