Thread: pointer and array

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    21

    pointer and array

    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.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    21
    thanks, guys, i understand better!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM