Thread: Giving an argument to function.

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    76

    Giving an argument to function.

    Hi, i have such situation:

    Code:
    #include <stdio.h>
    
    typedef int x[2][2];
    
    int y(int **i)
    {
            i[0][1]=1;
            return 0;
    }
    
    int main()
    {
            x one;
            y(&one);
            printf("%d\n",one[0][1]);
            return 0;
    }
    but i get a warning:
    $ gcc hhhh.c
    hhhh.c: In function `main':
    hhhh.c:15: warning: passing arg 1 of `y' from incompatible pointer type
    and of course, printed value is bad.
    What's wrong with that?
    Regards.

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Try:
    Code:
            y(one);
    You don't need the address of the double pointer...just the double pointer.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    76
    unfortunatelly, without changes...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Having created the typedef, why not use it?

    Code:
    int y ( x i );
    {
            i[0][1]=1;
            return 0;
    }
    int main()
    {
            x one;
            y(one);
            printf("%d\n",one[0][1]);
            return 0;
    }
    > You don't need the address of the double pointer...just the double pointer.
    There are no double pointers.
    There is an array, and a pointer to an array.
    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.

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    76
    Thank you. Now it works. I though i have to use pointers to change the object outside the function ( int y(someting *i)).

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Arrays are not like non-array variables. If you pass an array to a function, it's really passed via reference, so every change inside the function effects the real array. You can't pass arrays by value.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Just because you've created some typedefs to hide all the 'pointer' bits from sight doesn't mean that C all of a sudden stops passing a pointer to the first element of the array.

    Typedef doesn't add anything new, it just allows you to say more concisely what you could have said another way.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  4. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM