Thread: Matrices and pointers

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    2

    Matrices and pointers

    HI all,

    I'm trying to compile the following code:

    Code:
    void fibFillMat(int mat[][COL], int n)
    {
    	int position=1;
    	int temp=1;
    	int i;
    	**mat=0; //First number is zero
    	for(i=1;i<=n;i++)
    	{
    		*(*(mat+i))=(position*i);
    		position+=temp; //finds next relevant position
    		temp=*(*(mat+i));
    		*(mat+i)+=(position-temp); //moves the pointer to next relevant position
    	}
    }
    The function is supposed to fill a matrix with the Fibonacci sequence.

    The error i get is:
    "invalid operands to binary + (have ‘int[3]’ and ‘int’)"
    (I use Eclipse for Linux).

    I'm obviously new to this pointers thing, but i'm pretty sure *(mat+i) points to another pointer wich i can increment, right?

    so i can't understand why it's not compiling.

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Code:
    *(mat + i) += position - temp; //moves the pointer to next relevant position
    When you dereference mat+i you get an array. I'm a little bit confsued, because I don't know what is supposed to be your 'pointer'. Do you want to modify 'mat' pointer?
    The array should be 1-dimentional:
    Code:
    void fibFillMat(int mat[], int n);
    Then you can use:
    Code:
    ++mat;
    To get the next position.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    2
    When you dereference mat+i you get an array. I'm a little bit confsued, because I don't know what is supposed to be your 'pointer'. Do you want to modify 'mat' pointer?
    Basically, i'm trying to move inside the 'i' row in the matrix mat.
    In my understanding, *(mat+i) gives me the address of the beginning of row 'i' in the matrix.

    Am i wrong?

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    mat is pointer to an array of COL int.
    so *(mat+i) = mat[i] is an array of COL int.
    And you cannot modify array. like int array[10];
    array++; array += 1; all will produce compiler error.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by Compulsive D View Post
    Basically, i'm trying to move inside the 'i' row in the matrix mat.
    In my understanding, *(mat+i) gives me the address of the beginning of row 'i' in the matrix.

    Am i wrong?
    I still don't get why you need NxM array instead of N array. Yes, it indeed gives this address, but this is just address of array.

    EDIT:
    <mat points here>[0,0][0,1][0,2][0,3][0,4]<mat + 1 points here>[1,0][1,1][1,2][1,3][1,4]<mat + 2 points here>[2,0][2,1][2,2][2,3][2,4]

    You get address mat+1, but this is not a pointer which you can modify, it's just an array.
    Last edited by kmdv; 12-16-2010 at 10:14 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matrices and pointers
    By lombardom in forum C Programming
    Replies: 1
    Last Post: 05-14-2010, 06:59 AM
  2. multiplying two matrices
    By Vin in forum C Programming
    Replies: 1
    Last Post: 03-01-2010, 04:54 AM
  3. Pointers or Arrays?
    By magda3227 in forum C Programming
    Replies: 3
    Last Post: 07-09-2008, 09:52 AM
  4. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  5. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM