problem with structure pointers

This is a discussion on problem with structure pointers within the C Programming forums, part of the General Programming Boards category; Hi, I am getting Segmentation fault error for code below. Please tell me how to remove it. Code: #include<stdio.h> #include<stdlib.h> ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    16

    problem with structure pointers

    Hi,

    I am getting Segmentation fault error for code below. Please tell me how to remove it.


    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    struct MATRIX {
    	int rows;
    	int cols;
    	double **t;
    };
    
    struct MATRIX new_matrix(int rows, int cols)
    {
    	struct MATRIX t;
    	int i;
    	t.rows=rows;
    	t.cols=cols;
    	t.t=(double **)malloc(t.rows * sizeof(double *));
    	for(i = 0; i < t.rows; i++)
    		t.t[i] = (double *) malloc(cols * sizeof(double));
    	return t;
    }
    
    struct MATRIX *transpose(struct MATRIX *a)
    {
    	struct MATRIX *b;
    	int i, j;
    	
    	*b = new_matrix(a->cols, a->rows);
    
    	for(i=0; i<b->rows; i++)
    		for(j=0; j<b->cols; j++)
    			 *(*(b->t+i)+j)=*(*(a->t+j)+i);
    
    	return b;
    }
    
    int main(){
    	struct MATRIX *x;
    	struct MATRIX *t;
    	int i, j;
    	
    	*x = new_matrix(3, 4);
    	for(i = 0; i < x->rows; i++)
    		for(j = 0; j < x->cols; j++)
    			*(*(x->t + i) + j) = 1.2*(i+j);
    	
    	t = transpose(x);
    	
    	for(i=0; i < x->rows; i++) {
    		printf("| ");
    		for(j=0; j < x->cols; j++)
    			printf("%.2f ",*(*(x->t+i)+j));
    		printf("|\n");
    	}
    	printf("\n");
    	
    	for(i=0; i < t->rows; i++) {
    		printf("| ");
    		for(j=0; j < t->cols; j++)
    			printf("%.2f ",*(*(t->t+i)+j));
    		printf("|\n");
    	}
    	printf("\n");
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,020
    You never allocated any memory for x or t, so it's natural you get an access violation when dereferencing them.
    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.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can't do *x = new_matrix(3,4), since you don't own the memory x points at. You want your new_matrix to return a MATRIX*, and do x = new_matrix(3,4). Edit: and new_matrix will have to allocate the memory for the matrix, since t is gone at the end of the function.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,020
    And don't forget to free and malloc'd memory.
    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.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Perhaps start with these signatures:
    Code:
    struct MATRIX new_matrix(int rows, int cols)
    struct MATRIX transpose(struct MATRIX *a)
    And make associated . and -> operator changes.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem about Creating structure
    By albert3721 in forum C Programming
    Replies: 3
    Last Post: 06-05-2007, 08:33 PM
  2. Replies: 9
    Last Post: 05-21-2007, 01:10 AM
  3. Problem with custom structure
    By stickman in forum C++ Programming
    Replies: 9
    Last Post: 10-14-2006, 03:57 PM
  4. very weird problem (pointers I think)
    By hannibar in forum C Programming
    Replies: 2
    Last Post: 10-11-2005, 07:45 AM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM

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