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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:59:38+00:00 2026-05-28T00:59:38+00:00

I have been trying to execute the following code. Though the task is trivial,

  • 0

I have been trying to execute the following code. Though the task is trivial, I am getting a segmentation fault.

The aim of the code snippet betlow is to create a multi dimensional array of maximum row size 4 and column size 33. Then after creation, it should set the contents of all the rows as 0, followed by a '\0' character. Then in the end, it should display the output on the stdout.

Although I am not new to programming, I keep on getting similar errors, so if possible please explain me how can I avoid such mistakes in the future.

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

int main(int argc, char* argv[])
{
        int i,j,k,x,y;
        char** arr;
        arr = (char**) malloc(4 * sizeof(char*));
        for ( i = 0; i < 4; i++) {
                arr[i] = (char*) malloc(9 * sizeof(char));
                memset(arr,0,8);
                arr[i][8] = '\0';
        }
        for ( j = 0; j<4; j++) {
                puts(arr[j]);
        }
        return 0;
}
  • 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-05-28T00:59:39+00:00Added an answer on May 28, 2026 at 12:59 am

    You’re memseting the wrong pointer.

    Instead of:

    memset(arr,0,8);
    

    you want:

    memset(arr[i],0,8);
    

    So you’re off by one level of indirection.


    As pointed out in the comments, here are some optimizations:

    for ( i = 0; i < 4; i++) {
        arr[i] = (char*) malloc(9 * sizeof(char));
        memset(arr[i],0,9);
    }
    

    or

    for ( i = 0; i < 4; i++) {
        arr[i] = (char*) calloc(9, sizeof(char));
    }
    

    Note that the cast to char* isn’t necessary in C.


    If you wanted to the character '0' instead of the null-character, then you should go with this:

    for ( i = 0; i < 4; i++) {
        arr[i] = (char*) malloc(8 * sizeof(char));
        memset(arr[i],'0',8);
        arr[i][8] = '\0';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to get the following code to work(everything is defined in
I have been trying to execute the following UNIX shell script which is not
I have been trying to learn how to add testing to existing code --
I have been trying the following but always fails, roomTypeSQL = SELECT spftype FROM
In my example code below, you can see that I have been trying to
I have been trying to optimize the two following nested loops: def startbars(query_name, commodity_name):
I have been trying to implement Win32's MessageBox using GTK. The app uses SDL/OpenGL,
I have been trying to find a really fast way to parse yyyy-mm-dd [hh:mm:ss]
I have been trying to determine a best case solution for registering a COM
I have been trying to work my way through Project Euler, and have noticed

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.