Username
Username

Reputation: 41

How to printout a value a pointer is pointing at from an array

As in the title. I'm trying to print out a value that a pointer is pointing at but have no luck with it. The code below shows that Im assigning a pointer to a random position in an array .

//Assign a pointer to a random location in array 
int val = rand() % 6;
int* p = &a[val];

So far this is the only thing that works but it just prints out the location in memory, when I need the value printf("%p", &p); So again how can I print the value my pointer is pointing at ?

Edit: My array is declared like this int a[6]; and im filling it like this:

for (int i = 5; i >= 0; i--)
{
    a[i] = pal_num % 10;
    pal_num /= 10;
}

Where pal_num is a 6 digit number

Edit2: Calling the function print_status

while (!is_pal(a))
{
    if (!is_pal(a))
    {
        print_status(a, num_mov, p);
        ask_for_command(p, num_mov);
    }

Upvotes: 0

Views: 49

Answers (1)

user3703887
user3703887

Reputation:

Dereference the pointer, like so:

printf("%d", *p);

Operator * on pointers is the opposite of operator &.

Upvotes: 2

Related Questions