Need help with pointers & memory address

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi guys,

 

What data type is used to hold the memory address?

I want to store the memory address held in the pointer variable

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How big is your memory?

Choose a type that can hold the maximum value

 

your question is vague!    memory address held in the pointer variable

WHAT pointer   ...WHAT VARIABLE?

 

Be more descriptive in your next post.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
/*----------------------------*
Concept of STACK Memory-CUSTOM*
------------------------------*/

#include <iostream>

using namespace std;

int main ()
{
    long int ad[5] ;   //Array to hold address.
    long int x[5] = {0};     //Array of 5 int variables. Consider it a STACK.
    long int *p;             //Declaration of pointer to an int value.
    p = x;              //Initialized the pointer to the first element of
                        //array or STACK.
    ad[0] = p;         //Initialized the first element to the first address
                        //of x array i.e. x[0].
    int val = 0;

/*-----------------Loop to fill the STACK----------------*/

    for (int i = 0; i < 5; i++){
    cout << "Enter Value: ";
    cin >> *p;          //Overwrite the contents of the address pointed by 'p'.
    cout << i <<". You Entered: " << *p <<"  " << p << "\n";    //With the input value also display the
                                                                //the memory address of it.
    ad[i] = p;         //Address will be stored in add array.
    p++;                //Post increment the memory address.
    }
/*-------------Loop for Resetting the pointer to first array or STACK element-----------*/
        for (int i = 0; i < 5; i++)
       p--;             //Decrement to the previous address.

/*----------------------Data Fetch---------------------*/
    cout << "Enter Ser. No. to Fetch: ";
    cin >> val;
    p = ad[val];
    cout << *p;

    return 0;
}

 

Last Edited: Sat. Aug 3, 2019 - 04:10 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have posted the program.

 

Secondly, the memory address of the variable, containing the entered value, consist of 6 hex digits, i.e. 24 bits.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:

your question is vague!    memory address held in the pointer variable

WHAT pointer   ...WHAT VARIABLE?

What toolchain?  What hardware?

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

On AVR sizeof(ptr) is 2 so you don't need "long int" if you want to store them. It only needs to be uint16_t but why would you store them in an array of "Int" anyway? Why not an array of pointers and the compiler would pick the appropriate storage size anyway.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You generally want to just use pointers as clawson pointed out. If for some reason you do need to convert a pointer to an integer type you should use uintptr_t/intptr_t. The only use case I've come across for doing this is storing additional data in the unused bits of a pointer.