array to a function causes strange errors

hi,
I have an array int p[10];
then I pass the value of this array to a function i defined, func(int *point)
in the function, I don't have any sentence to change the value of my array p[10]
but the next time i use p[10], the value is changed


so my question is what did i do wrong?
is that a better way to pass the value of an array to a function?

Thanks a lot
You are being too vague, but I'll try to answer.

The function receives a pointer to an integer (int *). C++ variable arrays are a pointer to the first integer, and the rest follow consecutively in memory. Therefore, the declaration:
 
int p[10];


basically states that p itself is a pointer to the value stored in the first position in the array, or put in code:
 
assert(p == &p[0]);


The above should assert to true, demonstrating that both point to the same memory address.

Having said all this, how are you accessing the array in your function? Does your function know the size of the array? Is it fixed at 10?

The function cannot change the address of the argument p, but can change the values it points to. The following will change the values of the array:
1
2
3
4
5
6
func(int * point)
{
...
    point[0] = 1;
...
}


Finally, a common newbie mistake is trying to compare a value using just one "=" sign instead of two:
1
2
3
4
5
6
func(int * point)
{
...
    if (point[0] = 1)
...
}


The above is not a comparison. It will always be true because point[0] is being assigned a value of 1, and if(1) is always true. There should be two "=" signs in the comparison, not 1.
NO, all i have done in the function is:

x=point[i];
kind, which means that I only pass value from array out. There is no sentence
like point[i]=x;
Last edited on
Can we see the called function?
In other words, it is impossible to know what you are talking about without seeing a complete code example that demonstrates the problem.
Thanks for your attention. I found the problem
It is other parts of the program that cause the problem.


I am a beginner. Have never coded longer than 500 lines. For me it is hard to make every parts of my code co-exist.

sorry for bothering you guys.
Last edited on
Topic archived. No new replies allowed.