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 8857753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:40:06+00:00 2026-06-14T14:40:06+00:00

I’ve got code that compiles well enough, but when I try to run it,

  • 0

I’ve got code that compiles well enough, but when I try to run it, I get a segmentation fault and I can’t figure out what’s wrong.

The point of the program is to piece together the text of fragmentet ascii art files of varying dimensions and number of fragments.
The fragments are named part_xx-yy where xx is from 00 to 11 and yy is from 00 to 05.

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


int main(void)
{
    int width;
    int height;
    int xPieces;
    int yPieces;
    int xTens=0;
    int xOnes=0;
    int yTens=0;
    int yOnes=0;
    printf("Fragment width: ");
    scanf("%d", &width);
    printf("Fragment height: ");
    scanf("%d", &height);
    printf("Number of fragments on x axis: ");
    scanf("%d", &xPieces);
    printf("Number of fragments on y axis: ");
    scanf("%d", &yPieces);
    printf("wtf0");
    char *line=malloc(sizeof(width) * sizeof(char));
    printf("wtf1");
    char array[xPieces][yPieces][height][width];
    printf("wtf2");
    char fileName[50];
    printf("wtf3");
    for(int x = 0; x<xPieces;)
    {
        printf("%d", x);
        for(int y = 0; y<yPieces;)
        {
            printf("%d", y);
            if(xOnes>=10) 
            {
                xOnes=0;
                xTens++;
            }
            if(yOnes>=10)
            {
                yOnes=0;
                yTens++;
            }
            snprintf(fileName, sizeof fileName, "part_%i%i-%i%i", xTens, xOnes, yTens, yOnes);
            FILE *file=fopen(fileName, "r");
            char buffer[(width) * (height)];
            fread(buffer, 1, (width) * (height), file);
            for(int i = 0; i<height; i++)
            {
                printf("%d", i);
                for(int j = 0; j<width; j++)
                {
                    printf("%d", j);
                    array[x][y][i][j] = buffer[j + (i * (width))];
                }
            }
            fclose(file);
            y++;
            yOnes++;
        }
        x++;
        xOnes++;
    }

    FILE *newFile=fopen("newFile", "w");

    for(int y = 0; y<yPieces; y++)
    {
        for(int i = 0; i<height; i++)
        {
            for(int x = 0; x<xPieces; x++)
            {
                for(int j = 0; j<width; j++)
                {
                    fwrite(&array[x][y][i][j], 1, 1, newFile);
                }
            }
        }
    }

    fclose(newFile);
    free(line);
}

I figured out how to use the debugger, and that indicated there was something wrong with fread(), which I think was caused by my fileName array, but I changed a few things and now all I get from the debugger is this:

Program received signal SIGSEGV, Segmentation fault.
0x0018d68c in fread () from /lib/tls/i686/cmov/libc.so.6

I thought perhaps fread() tried to read into a too small buffer, so I increased the buffer to 10000 (which SHOULD be dramatic overkill), but alas, to no avail.
I have researched quite a bit now, struggling with this problem for a couple of hours, but still have no idea how to go further from here as what I find of similar problems doesn’t make much sense to me or isn’t similar enough.

I think at this point I need someone else to look at my code, so any help will be greatly appreciated.

.

.

Update: I’ve updated my code with a few changes, and now I get a segmentation fault here instead:

Program received signal SIGSEGV, Segmentation fault.
0x08049058 in main () at innlev3.c:50
50                      *array[x][y][i][j] = buffer[j + (i * (*width))];

I thought this part was pretty good… What have I done wrong, here?

Update 2: Code updated again. I’ve found something I think is very strange… Neither of those printf’s after my scanf’s work… Aand I’m back to the good old fread() segmentation fault. I guess it’s a good thing I didn’t make a new question out of this… 😛

Program received signal SIGSEGV, Segmentation fault.
0x0018d68c in fread () from /lib/tls/i686/cmov/libc.so.6
(gdb) backtrace
#0  0x0018d68c in fread () from /lib/tls/i686/cmov/libc.so.6
#1  0x08048fc6 in main ()
  • 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-14T14:40:07+00:00Added an answer on June 14, 2026 at 2:40 pm

    I’m guessing file is NULL, probably indicating that *fileName doesn’t exist.

    Note that statements like this:

    fileName[5] = "%i",xTens;
    

    don’t do what you probably expected. That statement is equivalent to:

    fileName[5] = xTens;
    

    And that should have given you a compiler warning, since you’re assigning an int to a char*.

    Instead, you probably meant to use snprintf to use printf-style formatting to construct the filename.

    char filename[50];
    snprintf(filename, sizeof filename, "part_%i%i-%i%", xTens, xOnes, yTens, yOnes);
    FILE *file=fopen(fileName, "r");
    

    For your second crash: You have an extra layer of pointers that you don’t need on array. Declare it as char array... and remove the * when you access it. As it stands now, you’ve told the compiler that the elements will be pointers, but you haven’t made them point anywhere, and then you asked the compiler to go look where they’re pointing using *. Boom!

    Your final use of array then needs to make a pointer to each character to pass to fwrite. You’d do that with the & operator, called “address-of”, used like &array....

    The address-of operator is the opposite of *. Once you have the program working, you can use & other places to simplify your code. For example, instead of declaring int *width and allocating it off the heap using malloc, you can remove the * everywhere and pass &width to scanf.


    Since we’re back to the fread segfault: Check the return value of fopen before you use it. If it’s NULL, print an error message.

    if(file == NULL)
    {
        printf("can't open %s\n", fileName);
        exit(1);
    }
    

    That will probably tell you what’s wrong. However, this isn’t debugging code. You should generally check for error returns from the functions you call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a small JavaScript validation script that validates inputs based on Regex. I
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.