I'm just starting out with pointers in C, and it's taking a bit longer than expected. I would like to print a pointer that points to a memory area of integers.
In the main program I have the following
int main(int argc, char* argv[]){
int tamano = 10, i = 0;
int *x = (int*) calloc(tamano, sizeof(int));
for(i = 0; i < tamano; i++){
x[i] = i+1;
}
imprimirPuntero(x);
return 0;
}
And in the procedure I have the following
void imprimirPuntero(int *ptr){
int i = 0;
printf("Valores: ");
for(i = 0; i < 10; i++){
printf("%d ", ptr[i]);
}
}
So my question really is... How do I know how many positions the pointer has, without passing that size to it in the procedure?
In these cases, you only have 3 real options , plus a 4th bonus :
As you indicate, you cannot/want to use this option:
The normal thing is to pass a pointer to the element immediately after the last one. However, nothing prevents you from passing a pointer to, say, the penultimate element (except for making the code a bit more complicated). Your function, passing the position immediately after last, would look like this:
It's what the compiler does automatically with strings, using a
0
to mark their end. Well, the same, but adapted to the type of data you are using. And, of course, the compiler won't do it for you , you'll have to put that value by hand in the last position.Bonuses
When we do a
malloc( )
,calloc( )
orrealloc( )
, the implementation has its way of managing memory. Normally, just before the address that is returned to us, some control data is placed , depending on the implementation ; in thelibc
, that control structure can be a simple onesize_t
(depends on certain configuration options of thelibc
). Your function, assuming your implementation uses asize_t
as the control type:This last option is not 100% reliable; For performance reasons, the library usually allocates more bytes than necessary. But it's good to know that this possibility exists :-)
You don't know, your question is as impossible to answer as How do I know a person's age if I don't know their date of birth? .
If you want to know the number of elements to which a pointer points, you either know them in advance (explicit) or you deduce them on the spot (implicit).
To know the size explicitly you have the problem you already know: you must provide the size together with the pointer.
To know the size implicitly you must mark the end of the elements with some special code (as strings do with the null character
\0
); Suppose in your example code your pointer never points to negative values, we could use (for example)-1
as end mark:You have already been given a fairly complete and well-argued answer.
The reality is that natively C does not establish an end of vector as in other languages, only in character strings as you have been told before.
For example in C# when declaring a vector, with the Length property we know what the size of said vector is.
In C when performing:
In either of the two options, at the memory level (example) we would see something like this:
Yes, later the traversal of any of the two vectors will be carried out in a function.
Yes, no rule is established as has been said previously, for example choosing a value from the spectrum of integers to designate it as the indicator of the end of the vector. The traversal of the vector would stop until it encounters a Segmentation Fault or some similar error, because we would start by traversing the reserved memory positions in this case until the position that contains 567891 and then continue with non-reserved memory positions, which in the best of cases cases we could manipulate but another process could also manipulate it.
At the beginning, specify natively, because there may be some library or standard for vector manipulation that provides an abstraction layer and allows what is requested to be done.
Greetings.
In itself, it is not possible to calculate the size of the array during the execution of a function, however, what we could do is wrap the pointer (the one that points to the array ) inside a structure, such as:
The member
x
gives me access to the content of the array and inlength
we will store the size of the array. We will do this assignment when we create the dynamic array and we could do that through a function:The first parameter points to a type structure
ArrayInt
and the second parameter receives the size of the vector.And that's it, every time we want to do an operation through a function, it simply must have a parameter that receives the base address of the structure
ArrayInt
and that way we will have access to the length of the array .For example, an operation could be to print the elements of the array :
We don't need a second parameter to store the length of the array, we just have to access the member
length
of the structureArrayInt
and that's it.Full example :