-
Change value in function
I have two code, one is true, and other is wrong. (the purpose of this work is test, how to change an array in function.
First is:
Code:
#include <stdio.h>
int array(int a,int b,int *x);
int main(){
int *a;
array(5,6,a);
printf("%d %d\n",a[1],a[2]);
}
int array(int a, int b,int *x){
x[1]=a;
x[2]=b;
}
and second is:
Code:
#include <stdio.h>
int array(int a,int b,int *x);
int main(){
int a;
array(5,6,&a);
printf("%d %d\n",a[1],a[2]);
}
int array(int a, int b,int *x){
x[1]=a;
x[2]=b;
}
First code is true (it will print 5 6), but second code is wrong. It's wrong at line
Code:
Printf("%d %d",a[1],a[2]);
// It has an error name:invalid type int[int] for array subscript
I have google but I don't know so much about this problem.
So, who can help me, why It's wrong, please.
thanks so much :)
-
a is not an array of integers, and lying to your other function about it won't make it so.
Your first function is even worse.
-
> I have two code, one is true, and other is wrong.
Well the first only works because of pure dumb luck, since your pointer is uninitialised.
If you had
int *a = NULL;
it would probably blow up.
Further, you seem to be implying that arrays start at [1], when in fact they start at 0.
The minimal you need to do to make this work is either
int a[3];
or
int *a = malloc( 3 * sizeof(*a) );
-
@Salem: thanks a lot, I know where my mistake, your advice is very helpful (It can run smooth now). But I have one point that I don't know: why in my old first code, why I have "pure dumb LUCK" and why when I had: int *a=NULL, my program will be blow up.
thanks again :)
-
On most machines, dereferencing NULL is a fatal error.
The pure dumb luck is like this
Code:
int age;
printf("You are %d years old\n", age );
It's easy to see that this is wrong.
But with your uninitialised pointer, all that you've really got to go on is whether it crashes or not. Any random address is good enough (for you), so long as it doesn't crash.
-
@Salem: Oh,now I know why a=NULL will blow up. But, if I don't have line
Code:
int *a=malloc(sizeof(*a));
the most problem is we don't know exactly the address of a(a[1],a[2],etc). So (if this thinking of mine is true), I don't think this is a dumb because we doesn't need it in most case. Am I wrong? please tell me more, please.
Sorry, if this is a very silly question.
thanks for tons:)
-
You need it in every case (the initialization of a, that is). 0.001% of the time, you are going to make a point at something already existing, so malloc isn't necessary (but you still need to do initialization). The other 99.999% of the time, you will need to use malloc to obtain a valid pointer.
-
You don't actually need to know --and should not care-- what the actual address assigned to a variable is.
What is important is that you do not play with uninitialized pointers. You need to use malloc() or or assign it to the address of another variable before using it. Playing with unintialized anything is probably the most common cause of run time errors.
-
Oh, I luckily read a paper that say about Pointer. After that, I see some above questions of mine is really silly :( A "bad pointer" is reference to random address. It hard to see problem when debug small problem, but it will be really an awful event if run this module in big project. (Am I thinking right :redface: :"> )
-
I think you need to go through whatever C book or reference you have, page by page, problem by problem. You have some serious level of knowledge problems.