i have an array, let's say "float rain[5][12]", and i have a pointer "float *p", when I make the statement like this"p=rain", then I got a error saying imcompatible pointer type. could anyone explain why? thanks.
i have an array, let's say "float rain[5][12]", and i have a pointer "float *p", when I make the statement like this"p=rain", then I got a error saying imcompatible pointer type. could anyone explain why? thanks.
When you say p=rain, you're also saying p = &rain[0]
But rain[0] isn't a float, it is an array of 12 floats.
You can point to arrays (as a whole) using the following notation
Code:#include <stdio.h> #include <stdlib.h> int main ( ) { float rain[5][12]; float *p = &rain[0][0]; float (*q)[12] = rain; q = &rain[0]; /* same again */ *p = 123; printf("%f %f\n", rain[0][0], q[0][0] ); return 0; }
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
This could also help: http://c-faq.com/aryptr/pass2dary.html
thanks, guys, i understand better!