Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8812967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:43:41+00:00 2026-06-14T03:43:41+00:00

I have three files: something.h typedef struct { int size_t; char *c; } p;

  • 0

I have three files:

something.h

typedef struct {
    int size_t;
    char *c;
} p;

p ** createMatrix(int r, int c);

void printMatrix(p **matrix, const int r, const int c);

something.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "something.h"

p **
createMatrix(int r, int c)
{
    int rowsize=r*2+1;
    int colsize=c*2+1;

    /* memory alloc of rows */
    p **matrix=malloc(rowsize*sizeof(p *));
    int k;
    for(k=0; k<rowsize; k++)
    {
        /* memory alloc of columns */
        matrix[k]=malloc(colsize*sizeof(p));
    } 
    int i, j;
    for(i=0; i<rowsize; i++)
    {
        for(j=0; j<colsize; j++)
        {
            /* columns is between letters */
            if(j%2!=0)
            {   
                matrix[i][j].size_t=7;
                matrix[i][j].c=malloc(8*sizeof(char));
                strcpy(matrix[i][j].c,"       ");
            }
            else if(i%2==0 && j%2==0)
            {
                matrix[i][j].size_t=1;
                matrix[i][j].c=malloc(sizeof(char));
                *(matrix[i][j].c)='a';
            }
            else
            {
                matrix[i][j].size_t=1;
                matrix[i][j].c=malloc(sizeof(char));
                *(matrix[i][j].c)=' ';
            }
        }
    }       
    return matrix;
}

void
printMatrix(p **matrix, const int r, const int c)
{
    int rowsize=r*2+1;
    int colsize=c*2+1;
    printf("\n");
    int i, j;
    for(i=0; i<rowsize; i++)
    {
        printf("\t");
        for(j=0; j<colsize; j++)
        {
            if(matrix[i][j].size_t==1)
                printf("%c", *matrix[i][j].c);
            else
                printf("%s", matrix[i][j].c);

        }
        printf("\n");
    }
    printf("\n");
 }

main.c

#include <stdlib.h>
#include <stdio.h>
#include "something.h"    

void
printMenufunction(p **matrix, int rows, int cols)
{
    int rowsize=rows*2+1;
    int colsize=cols*2+1;
    /* memory alloc of rows */
    matrix=malloc(rowsize*sizeof(p *));
    int i;
    for(i=0; i<rowsize; i++)
    /* memory alloc of columns */
        matrix[i]=malloc(colsize*sizeof(p));
    matrix=createMatrix(rows, cols);
}

int
main(void)
{
    int rows, cols;
    p **matrix;
    char stringtemp[3];
    printf("ask for row and col in form (5x5):\n");
    scanf("%s", stringtemp);
    sscanf(stringtemp, "%dx%d", &rows, &cols);
    printMenufunction(matrix, rows, cols);
    printMatrix(matrix, rows, cols);
    return 0;
}

This code sample that is a simplified version of my prog gives me a segmentation fault. I have debugged it with the gdb so I know its somewhere amongst these lines that the fault lies. I would be grateful for some help, thanks.

UPDATE:
I can’t debug this version. My old prog with alot more code told me it was along this area, but at least I can’t figure out where? So thats why I can’t post what line exactly that makes the segmentation fault. If i knew I think I could have handled the problem my self. I hoped some one could see it more clearly than me..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:43:42+00:00Added an answer on June 14, 2026 at 3:43 am

    First of all, it would help us a lot for you to tell us which line gdb indicated was at fault instead of just saying, “it’s somewhere in these lines.” gdb should be able to tell you exactly which line triggered the error.

    Having said that, here’s a candidate:

    char *stringtemp;
    printf("ask for row and col in form (5x5):\n");
    scanf("%s", stringtemp);
    

    stringtemp hasn’t been assigned to point anywhere meaningful; all you have is a pointer whose initial value is indeterminate. So that’s one potential segfault. Either allocate a buffer for stringtemp to point to, or just allocate it as an array of char:

    char stringtemp[SOME_SIZE]; // where SOME_SIZE is as big as you need.
    

    Why are you allocating the matrix in printMenuFunction?! Why aren’t you using your createMatrix function?!

    EDIT

    I hate getting old; I completely missed that createMatrix is being called from printMenuFunction (which means that the malloc calls in printMenuFunction are redundant and need to be eliminated).

    Rewrite your printMenuFunction as follows:

    void printMenuFunction(p ***matrix, int rows, int cols)
    {
      *matrix = createMatrix(rows, cols);
    }
    

    Note that within that function, if you need to access an element of matrix, you will need to dereference it before applying a subscript, such as

    (*matrix)[i][j].size = ...;
    

    Rewrite your main as follows:

    int
    main(void)
    {
        int rows, cols;
        p **matrix;
        char stringtemp[3]; // 3 is not big enough to hold a string like "5x5";
                            // that requires an array size of *at least* 4.  
                            // "10x10" would require 6 characters.  You need
                            // an extra character for the 0 terminator.
    
        printf("ask for row and col in form (5x5):\n");
        scanf("%s", stringtemp);
        sscanf(stringtemp, "%dx%d", &rows, &cols);
        printMenufunction(&matrix, rows, cols); // note the & in front of matrix
        printMatrix(matrix, rows, cols);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three files, a main .cpp file: #include <stdio.h> #include myClass.h int main()
My have three .cpp files and their headers. //a.cpp #include a.h #include b.h void
I have the following: typedef std::function<void(const EventArgs&)> event_type; class Event : boost::noncopyable { private:
I have something like this, in fact more complex struct than this: typedef struct
I have trouble understanding c header files and source files. I have: something.c #include
I have text file with something like first line line nr 2 line three
I have three files: lib.c lib.h => They should be built as a .so
I have three files: one called sql.php witch has a class db that I
I have three files in a folder 'test' one.php two.php print.html And i have
I have three files: movie.h (header); movie.cpp (implementation file); lab9.cpp (driver file using movie

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.