Results 1 to 4 of 4

C - function declearation

This is a discussion on C - function declearation within the Programming forums, part of the Web Designing & Development category; Hi Guys, I have written the program for accesssing elements of 2D array using pointer to array of int which ...

  1. #1
    Junior Member Array
    Join Date
    Jan 2010
    Posts
    3

    Default C - function declearation

    Hi Guys,

    I have written the program for accesssing elements of 2D array using pointer to array of int which is as below:

    void display(int (*)[4], int , int);

    int main()
    {
    int a[2][4] = {{1,2,3,4},
    {5,6,7,8}
    };

    display(a, 2, 4); // this line is giving error

    getch();
    }

    void display(int (*ptr)[4], int row, int col)
    {

    int i, j;
    int *p;

    for(i = 0; i < row; i++)
    {
    p = ptr + i;
    for( j= 0; j < col; j++)
    {
    printf("%d ", *(p+j));
    }
    printf("\n");
    }
    }

    But the function declearion line is giving the error and also I am not sure about function prototype written correct or not.

    Please help me in how the function prototype and declearation has to be written for the above fucntion defination.

    Thanks

  2. #2
    Member Array
    Join Date
    Aug 2008
    Posts
    54

    Default Re: C - function declearation

    Hi, I have mentioned below some changes u can do to run the code successfully :

    1. Change the prototype of the function

    from : [COLOR=Red]void display(int (*)[4], int , int);[/COLOR]

    to : [COLOR=Blue]void display(int *, int , int);[/COLOR]

    since u r sending the base address of the integer 2d array which will be catched by the integer pointer then no need to specify size in the function prototype...


    2. If u r not returning any value as it seems the coding u have done for function main() then change it

    from : [COLOR=Red]int main()[/COLOR]

    to : [COLOR=Blue]void main()[/COLOR]

    otherwise it will give a warning message.

    3. In function display() change the code
    from : [COLOR=Red]p = ptr + i;[/COLOR]

    to : [COLOR=Blue]p = *(ptr + i);[/COLOR]

    Make the above changes n u will get the result properly...if u have any further queries pl mention...

  3. #3
    Senior Member Array
    Join Date
    Mar 2011
    Posts
    110

    Default Re: C - function declearation

    Thanks a lot for sharing the coding part,Actually I need the answer.............

  4. #4
    Senior Member Array
    Join Date
    Mar 2011
    Posts
    459

    Default Re: C - function declearation

    dont understand it



Similar Threads

  1. Replies: 3
    Last Post: 04-04-2011, 12:01 PM
  2. why i am not geting the site:www.altavista.edu/protien function?
    By Narasimhaiah N in forum Other Search Engine Discussion
    Replies: 3
    Last Post: 10-11-2010, 07:46 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
SEO Forum | Web Hosting Forum | Websites For Sale |