Thread: pointer to an array

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    1

    Question pointer to an array

    Why Doesnt This code work in Linux??

    Code:
    main()
    {
    	int a[3][4] = {1,2,3,4,
              	       5,6,7,8,
    			0,9,1,6
    				  };
    	show(a,3,4);
    }
    
    show(int (*q)[4],int row, int col)
    {
    	int i,j,*p;
    	for(i=0;i<row;i++)
    	{
    	p = q+i;
    	for(j=0;j<col;j++)
    	printf("%d",*(p+j));
    	printf("\n");
    	}
    	printf("\n");
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well that depends on how you define "doesn't work".

    Perhaps it's the lack of a prototype?
    Perhaps it's the broken array notation in the function?
    Perhaps it's Penry, the mild mannered janitor.
    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
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you enable warnings (with -Wall) you will get a heap of warnings for your code.

    I managed to make it work with (on Windows, but using gcc which is the same compiler as you'd use on Linux, so the code generated should match) a few minor changes.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54
    Following version of your program is working fine with gcc( if we ignore a few warnings that it through):
    Code:
    #include<unistd.h>
    #include<stdio.h>
    #include<stdlib.h>
    
    int main()
    {
            int a[3][4] = {1,2,3,4,
                           5,6,7,8,
                                            0,9,1,6
                                      };
            show(a,3,4);
            return(0);
    }
    
    show(int (*q)[4],int row, int col)
    {
            int i,j,*p;
            for(i=0;i<row;i++)
            {
            p = q+i;
            for(j=0;j<col;j++)
            printf("&#37;d",*(p+j));
            printf("\n");
            }
            printf("\n");
    }
    Folloing is the output that i get:
    Code:
    1234
    5678
    0916
    What do you get when you try to compile it?
    -------------------------------------------------------------------
    There are 10 kinds of people in this world....Those who understand binary and those who don't

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by jas_atwal View Post
    Following version of your program is working fine with gcc( if we ignore a few warnings that it through):
    Code:
    #include<unistd.h>
    #include<stdio.h>
    #include<stdlib.h>
    
    void show(int (*q)[4],int row, int col);
    
    int main()
    {
    	int a[3][4] = {1,2,3,4,
    				5,6,7,8,
    				0,9,1,6
    				};
    	show(a,3,4);
    	return(0);
    }
    
    void show(int (*q)[4],int row, int col)
    {
    	int i,j;
    	int (*p)[4];
    	for(i=0;i<row;i++)
    	{
    		p = &q[i];
    		for(j=0;j<col;j++)
    			printf("&#37;d",p[j]);
    		printf("\n");
    	}
    	printf("\n");
    }
    Folloing is the output that i get:
    Code:
    1234
    5678
    0916
    What do you get when you try to compile it?
    You realize it's much easier to read when indented properly, like above?
    And x+y and *(x+y) is harder to read than &x[y] and x[y].

    And jas_atwal, you just had to go pad with spaces when the OP used tabs, didn't you? Sheesh.
    Other errors are show should have a return value, and if you return nothing, it should be void. Main should be int as pointed out.
    p should be declared as int (*)[4]. Because int* is not the type of your pointer q!
    I also modified the arrays to use [] instead of x + y and *(x + y). It's easier to read.

    ...And don't mix tabs and spaces! Use one, stick with one.

    And I did not test the code, of course.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 05-29-2009, 05:48 AM
  2. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  3. pointer to array of structs
    By Luken8r in forum C Programming
    Replies: 2
    Last Post: 01-08-2008, 02:05 PM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM