简体   繁体   中英

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);
    }

Dereference the pointer, like so:

printf("%d", *p);

Operator * on pointers is the opposite of operator & .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM