-2

I am trying to print the void* memory address inside void**, like this:

#include <stdio.h>
#include <stdlib.h>

int main(){

    void** MyArray = malloc(500 * sizeof(void*));
    printf("Last pointer: %p\n", *MyArray[499]);
    free(MyArray);
    return 0;

}

But when i try to compile I get a warning and an error:

  1. Line: 6 Col: 31 in D:\C\test.c [Warning] dereferencing 'void *' pointer
  2. Line: 6 Col: 2 in D:\C\test.c [Error] invalid use of void expression

What I'm doing wrong? Thanks

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Magic code
  • 45
  • 7

1 Answers1

1

You allocate memory to hold an array to hold a bunch of void pointers. This array doesn't contain any pointers yet. If you want the address of the last position in the array, do this:

int main(){
    void** MyArray = malloc(500 * sizeof(void*));
    printf("Last pointer is located at: %p\n", (void *)&(MyArray[499]));
    free(MyArray);
    return 0;

}
trent
  • 25,033
  • 7
  • 51
  • 90
Steve Kolokowsky
  • 423
  • 2
  • 12