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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:10:49+00:00 2026-05-28T18:10:49+00:00

I am trying to malloc a 2dimensional array in C, where each entry is

  • 0

I am trying to malloc a 2dimensional array in C, where each entry is a string (so, I suppose, a 3dimensional array). I have read a lot, and this is my attempt. However, I am getting a segmentation fault and I’m really not sure what is wrong. I am quite new to programming, so I apologize if my style is not good!

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

int main(int argc, char *argv[])
{
    double temp, int_check;
    int gen, exit_flag=0, valid_input, valid, i, j;

    char ***S;

    printf("argc %d\n", argc);
    if(argc < 2)
    {
        printf("Please enter command line arguments of the form: a R where a is the number of generators and R are relators\n");
    exit_flag = 1;
    }
    else
    {
        valid = sscanf(argv[1], "%lg", &temp);
        int_check = temp - (int)temp;
        valid_input = ((valid != 0) && (valid != EOF)) && (int_check == 0) && (temp > 0);

        if(!valid_input)
        {
            printf("Invalid input, the number of generators must be an integer > 0\n");
            exit_flag = 1;
        }
        gen = (int)temp;

        printf("Number of generators = %d\n", gen);
    }

    if(exit_flag==0)
    {

        S = (char***)malloc(2*sizeof(char**));      /*Defintes the grid to the size required*/
        if(S == NULL) 
        {
            printf("Cannot allocate memory for the S");
        }

        for(i=0; i<2; i++)
        {
            S[i] = (char**)malloc((argc-2)*sizeof(char*));
            if(S[i] == NULL)
            {
                printf("Cannot allocate memory for the S");
            }
        }                                                   /*Grid finished being given the right size*/

        for(i=2; i<argc; i++)                               /*Put the relators in the grid. Make rhs equal 1*/
        {
            strcpy(S[0][i-2], argv[i]);
            strcpy(S[1][i-2], "1");
            printf("relator %s\n", S[0][i-2]);
        }

        printf("The array S is\n");
        for(j=0; j<(argc-2); j++)
        {
            for(i=0; i<2; i++)
            {
                printf(" %s ", S[i][j]);
            }
            printf("\n");
        }
    }

    else    /*If the inputs are invalid, exit the program*/
    {
        exit(EXIT_FAILURE);
    }

    for(i=0; i<2; i++)
    {
        free(S[i]);
    }
    free(S);

    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-28T18:10:50+00:00Added an answer on May 28, 2026 at 6:10 pm

    You never allocate any memory for the actual characters, only for the various pointers pointing to those desired characters. Add the allocation here:

    for(i=2; i<argc; i++)        /* Put the relators in the grid. Make rhs equal 1*/
    {
        S[0][i-2] = malloc(strlen(argv[i]) + 1);  // allocate memory for the data
        strcpy(S[0][i-2], argv[i]);
        strcpy(S[1][i-2], "1");
        printf("relator %s\n", S[0][i-2]);
    }
    

    Don’t forget to free the whole mess in the end.

    I’m not sure the the repeated strcpys do what you want; strcpy adds a null terminator so your string ends there. Perhaps strncpy is a more useful function for your situation.

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

Sidebar

Related Questions

I'm trying to malloc a huge 4d array (192 gig available); but what goes
I am trying to override malloc by doing this. #define malloc(X) my_malloc((X)) void* my_malloc(size_t
Im trying to increase the size of a **array with realloc which I have
When trying to define an array such this: $array = new SPLFixedArray(256); for ($i
I'm trying to create a C array of objective C NSStrings using malloc. I'm
following this previous question Malloc Memory Corruption in C , now i have another
I am trying to create an array of strings in C using malloc .
Possible Duplicate: newbie questions about malloc and sizeof I am trying to read strings
I am trying to malloc and free a small array/table of single letter strings.
I have been trying to free memory allocated via malloc() using free() . Some

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.