Thread: Trouble with pointers

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    3

    Trouble with pointers

    Hello everyone
    I am new to C and I am having a few problems with pointers. It wouuld be nice if someone could help me out
    This is what I am trying to do
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	int n,i,j;
    	int a[3][4],**Trial=NULL;
    	Trial = (int **)malloc(3*sizeof(int*));
    	for(i=0;i<3;i++)
    	{
    		*(Trial+i) = (int*)malloc(4*sizeof(int));
    	}
    	for(i=0;i<3;i++)
    	{
    		for(j=0;j<3;j++)
    		{
    			printf("Enter the %d,%d coefficient\n",i,j);
    			scanf("%d",&Trial[i][j]);
    		}
    		printf("Enter the result of the equation\n");
    		scanf("%d",&Trial[i][3]);
    	}
    	for(i=0;i<3;i++)
    	{
    		for(j=0;j<4;j++)
    		{
    			printf("The value at position (%d,%d) is %d\n",i,j,Trial[i][j]);
    		}
    	}
    	return 0;
    }
    This program works fine as long as I do not use pointer arithmetic
    but when I use pointer arithmetic I keep getting a segemntation fault
    I replace the
    Code:
     scanf("%d",&Trial[i][j]);
    with
    Code:
     scanf("%d",*(Trial+(i+j)));
    and I get a segmentation fault after the first outer loop.
    Can someone please tell me what I am doing wrong
    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    12
    In scanf function,Each argument must be a pointer to a variable of a type that corresponds to a type specifier in format.

    *(Trial+(i+j)) dereferences and fetch the value at that address.

    Code:
    scanf("%d",*(Trial+(i+j)));
    look into this
    http://computer.howstuffworks.com/c7.htm
    Last edited by prasath; 09-30-2005 at 12:41 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Trial = (int **)malloc(3*sizeof(int*));
    read the FAQ

    >
    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.

  4. #4
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by eternities_end
    Code:
     scanf("%d",&Trial[i][j]);
    with
    Code:
     scanf("%d",*(Trial+(i+j)));
    and I get a segmentation fault after the first outer loop.
    Can someone please tell me what I am doing wrong
    Thanks
    Those are not the same. Trial[i][j] is equivalent to *(*(Trial+i)+j).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with pointers..
    By elfjuice in forum C Programming
    Replies: 7
    Last Post: 11-25-2007, 01:19 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  4. trouble with pointers HELP MEEEE???
    By drdodirty2002 in forum C++ Programming
    Replies: 1
    Last Post: 03-10-2004, 12:39 AM
  5. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM