473,408 Members | 1,788 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Can we save memory address in int variable?

1
If we know that sizeof(int) and sizeof(pointer) is same .How to save that pointer value in int?

for eg.

Expand|Select|Wrap|Line Numbers
  1. class node
  2. {
  3. char a;
  4. node *link;
  5. }
  6.  
  7. int main()
  8. {
  9. int i=10;
  10. node *ptr=NULL;
  11. ptr=new node;
  12. //i want to store value of ptr in i or character array of 4
  13.  
  14. return 0; 
  15. }

please help me as i m stuck in my project
Nov 3 '10 #1
1 7823
Oralloy
988 Expert 512MB
Aesha,

Please tell me this isn't a homework problem. We see variations of this question crop up on Bytes quite freequently.

That said, you can transform the value, but why do you need to violate the abstraction of a pointer? I really am curious, so please do tell me what you want to accomplish by doing this.

There are a host of ways of accomplishing what you want.

union:
Expand|Select|Wrap|Line Numbers
  1. node *p = new node();
  2. int i;
  3. union {
  4.   node *p;
  5.   int i;
  6. } u;
  7. u.p = p;
  8. i = u.i;
clean typecast:
Expand|Select|Wrap|Line Numbers
  1. int i = (int)(void*)(new node());
address games:
Expand|Select|Wrap|Line Numbers
  1. node *p = new node();
  2. int i = *(int*)(void*)&p;
Nov 3 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: David Casey | last post by:
I'm working on a program for my C++ class and have it all written and working except for one part. I need to compare two numeric variables to determine decimal accuracy between them. For example:...
10
by: Dan Elliott | last post by:
I am working on some tricky code and need some help from the experts. I have several large data structures (uBLAS matrices) that must be written to a pre-allocated (by another program) chunk of...
4
by: Someonekicked | last post by:
Is it possible to read a memory address with C++; For example, If I run this code first: ************* #include <iostream> using namespace std; void main() { int *zz = new int;
5
by: Jeong-Gun Lee | last post by:
I'm writing a code of writing a value to a specific memory address. ================================================================= #include <stdio.h> int main() { long air; long...
37
by: kplkumar | last post by:
I want to get the memory address of an object. Can someone tell me how to do that in C#. Also, I tried using the unsafe and the "&" operator and could not acheive it. Can you tell me a good way of...
12
by: FI | last post by:
Hello All, I am relatively new to C programming and I am struck with a problem in dynamic memory allocation. I would like to know if it is ok to pass the 'memory address' returned by...
20
by: sethukr | last post by:
hi, i need to store a value to a particular memory location without having a variable. So, how can i access a 'memory address' without using variables?? Is it possible in C??? Plz, help...
1
by: shaamil | last post by:
How to Pass a memory address(of a string variable) to function B and assign a value(a string) from within the function B. Here's part of the program: #include <stdio.h> char sales; int d;...
15
by: polas | last post by:
Hi everyone - I have a question. I am just playing around with C (I realise there are better ways to do what I want, but I would like to do it this way to increase my understanding of C) and would...
5
by: erfan | last post by:
vc++6.0,winxp. I use this code to see the memory address and the real value`s address below: #include<stdio.h> int main() { int a=22; int *p; int i; p=&a;
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...
0
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...
0
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...

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.