two questions...

This is a discussion on two questions... within the C Programming forums, part of the General Programming Boards category; 1) what is this error: arithmetics on pointer to an incomplete type? 2) how do i return a matrix from ...

  1. #1
    kickass
    Guest

    two questions...

    1) what is this error: arithmetics on pointer to an incomplete type?

    2) how do i return a matrix from a function?

    thanks

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > 1) what is this error: arithmetics on pointer to an incomplete type?
    You can have structures declared without any contents, and you can have pointers to these structures.
    But what you can't do is try and dereference these pointers (since to do this, the compiler needs to know the details).

    So,
    struct foo; // an incomplete structure
    struct foo *bar; // ptr to an incomplete type
    You can assign it a value, but that's about all

    To create an array, or dereference it, you need this in scope as well.
    struct foo {
      int baz;
    };


    > 2) how do i return a matrix from a function?
    depends how you've declared matrix
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    kickass
    Guest

    re...

    i declering my matrix like...
    int matrix[][];

    but what if i would declear it like int **matrix;

    can you show me the both ways to return it from a function?!

    thanks.

  4. #4
    Witch_king's puppet
    Join Date
    Aug 2001
    Posts
    20
    It's been a long time since I passed a 2-d array to a function but I remember it being very similar to passing an ordinary array.

    void function(char array[][20]);

    char array[20][20];

    function(array);

    You have to specify the size of the colum. When you think about a 2d array think of it as rows and columns. The array is automatically updated in main because it is passed as a pointer. The other syntax to accomplish the same thing in the prototype is confusing and I'd have to look it up.

    The problem with this:
    char **array;
    Is that it is just a pointer. It does not hold any memory and needs to be allocated memory before it is used. In other words you will have to use malloc. This could be wrong because I didn't look it up but the syntax for allocating a 2d array is something like this:

    char *array = (char *) malloc (sizeof(char) * ROWS);
    for(int i=0;i<ROWS;i++)
    char **array = (char **) malloc(sizeof(char *) * COLUMNS);

    And I'm sure this is slightly wrong but someone else can correct it. This is the general idea though. It's probably a lot easier to use:
    char array[20][20];
    If you know the size that you need.
    I'm A Farmer

  5. #5
    kickass
    Guest

    NO!!!

    I want to know how to return a matrix from a fucntion, not how to send one...

  6. #6
    Witch_king's puppet
    Join Date
    Aug 2001
    Posts
    20
    It is returned because when you send the array you are sending a pointer to the array. The caller is automatically updated.
    I'm A Farmer

  7. #7
    Witch_king's puppet
    Join Date
    Aug 2001
    Posts
    20
    If you want to define the array in the caller than you have to allocated it on the free store with malloc and return a char *. Hmm maybe it would be a char **. Never tried to return a 2d array. Try it out.
    I'm A Farmer

  8. #8
    kickass
    Guest
    people, let me reframe: how do i return a:

    int matrix[][];

    from a function. I tried:

    int *function(int X[][])

    but it gave me the "arithmetics on pointer to an incomplete type", error on every line in the function i used to read/write to the passed(to the function) matrix.

  9. #9
    Witch_king's puppet
    Join Date
    Aug 2001
    Posts
    20
    You simply can not return that from a function unless you put the word static infront of it. A better thing to do would be to dynamically allocate the memory on the heap. You have to understand that the array will go out of scope if you do not do that.


    int *function(int X[][])????????????????????????????????????????????????? ?????????????????????????????????????

    Where the **** are you defining the array? If it is defined in the caller than it doesn't need to be returned for **** sakes.

    void function(int x[][size]);
    I'm A Farmer

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Pass and return a matrix

    Code:
    #include <stdio.h>
    
    typedef int matrix_row_type[20];
    
    matrix_row_type * foo ( matrix_row_type *matrix ) {
        return matrix;
    }
    
    int main ( ) {
        matrix_row_type arr[20];
        matrix_row_type * mat_ptr = foo( arr );
        int r,c;
    
        for ( r = 0 ; r < 20 ; r++ ) {
            for ( c = 0 ; c < 20 ; c++ ) {
                mat_ptr[r][c] = 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 08:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21