0
I need to know how many elements are allocated in my pointer pointer. For example with vector, sizeof(v)/sizeof(v[0])
this way I get the number of elements that this vector has. I would like to do the same thing, but with pointers.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[]){
int *p = malloc(5*sizeof(int)), n;
n = sizeof(p)/sizeof(int);
printf("%d\n", n);
return 0;
}
In this code I already know how many elements have, just print the variable n
, but I will use this in a function where I don’t know the value of n
.
Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).
– Maniero