0
When I send a vector to the function, in the arguments.. what’s the difference between putting
void funcao(int vet[])
and
void funcao(int *vet)
0
When I send a vector to the function, in the arguments.. what’s the difference between putting
void funcao(int vet[])
and
void funcao(int *vet)
3
What’s the difference between
void funcao(int vet[]) { /* ... */ }
andvoid funcao(int *vet) { /* ... */ }
?
Absolutely none.
The two shapes are 100% identical.
Note that in C, it is impossible to pass arrays as parameters to a function (and that the compiler automatically converts to a pointer to the first element). With that in mind, the second option seems to correspond better to reality.
-1
Your question is whether there’s a difference between vet[]
and *vet
. Well, the answer is no.
Vectors
Vectors are indexed structures used to store data of the same
type: int, char, float or double. For example, the statement
int v[80]; /∗ declara um vetor de inteiros de nome v com 80 casas ∗/
Each home of the vector v
(that is to say, v[0], v[1], ..., v[79]
) is a whole. In addition, each house has an address
associated (ie, &v[0], &v[1], ..., &v[79]
).
One question we could ask is how a vector gets stored in memory. The organization of variables
in memory depends on how the operating system does memory management. In general, to be more
efficient, the operating system tends to place the oats Vari successively. Thus, the vector allocation in the
memory is made successively, that is, as illustrated in the figure above: v[0]
before v[1]
, that
in turn before v[2]
and so on. Thus, the variables declared as
int v [80]; /∗ declara um vetor de inteiros de nome v com 80 casas ∗/
int n , m;
could be allocated successively as
In the C language there is no check of indexes outside the vector. Who should control the correct use of indexes is the programmer. In addition, access using a wrong index may cause access to another variable
in memory. In the above example, v[80]
would access variable n. If access to memory is improper you receive the message "Segmentation fault".
Vectors and Pointers
The implementation of vectors in C is very interconnected with the pointers aiming to facilitate the manipulation vector. Consider the following variable statement:
int v[80]; /∗ declara um vetor de inteiros de nome v com 80 casas ∗/
int *p;
that allocates to Mem:
We can use normal syntax to make a pointer point to a vector home:
p = &v [2]; /∗ p aponta para a casa de índice 2 de v ∗/
But we can use the special syntax for pointers and vectors, together with operations for pointers:
We can make a pointer point to the beginning of the v vector by doing
p = v;
It is the only situation where the vector name makes sense without the brackets. The above command is equivalent to
do p = &v[0];
We can use vector syntax (nome_do_vetor[índice]
) with the pointer. So, if we do
p = v;
we can access the element that is in the house i of v doing p[i], that is, both p[i] and v[i] access the
box i of vector v. Example:
i = 3;
p = v ; /∗ p aponta para v[0]. Equivale a fazer p = &v[0] ∗/
p [ i ] = 4; /∗ equivale a fazer v[i] = 4 ∗/
But if we do
p = &v [3];
then, p[0]
is the element v[3]
, p[1]
is the element v[4]
, p[2]
is the element v[5]
, and so on.
int ∗p , ∗q , n , v[50];
float ∗x , y[20];
When we add 1 to a pointer to int (for example, p) it points to the memory address just after the memory reserved for this integer. Example, if p = &v[4]
, then p+1
is the address of v[5]
, p+2
is the address of v[6]
, p+i
is the address of v[4+i]
.
Thus, ∗p
(goes where the p is pointing) is the v[4]
. Therefore, v[4] = 3
is the same thing as
do ∗p = 3
. Like p+1
is the address of v[5]
, then ∗(p+1)
is v[5]
. Thus, v[5] = 10
is the same thing as doing ∗(p+1) = 10
.
If we add 1 to a float pointer (for example x) it advances to the address after this float.
For example, if x=&y[3]
, then x+1
is the address of y[4]
and x+i
is the address of y[3+i]
.
Adding or subtracting an integer from a pointer:
p = &v [22]; q = &v [30];
p = p − 4 ; q++;
∗( p+2) = 3 ; ∗q = 4;
Which index of v gets 3? Which index of v gets 4?1
Subtract two pointers:
p = &v [20]; q = &v [31];
n = q − p ; /∗ número inteiro : a diferença entre os índices, neste caso, 11. ∗
Source: IME.USP
And what is your conclusion about the differences of the statements?
Browser other questions tagged c array
You are not signed in. Login or sign up in order to post.
I’m pretty sure it’s duplicate, then I’ll take a closer look. This answer here I think is a starting point to understand the subject https://answall.com/a/193517/70
– Bacco