Trouble with pointers

This is a discussion on Trouble with pointers within the C Programming forums, part of the General Programming Boards category; Hello everyone I am new to C and I am having a few problems with pointers. It wouuld be nice ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    cwr
    cwr is offline
    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, 12: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, 06:08 PM
  4. trouble with pointers HELP MEEEE???
    By drdodirty2002 in forum C++ Programming
    Replies: 1
    Last Post: 03-09-2004, 11:39 PM
  5. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM

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