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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T23:33:30+00:00 2026-05-11T23:33:30+00:00

How do I allocate memory for a char variable (not a char pointer) inside

  • 0

How do I allocate memory for a char variable (not a char pointer) inside a struct?
(Variable names are in portuguese, sorry if it’s kinda confusing)

I have this struct:

typedef struct node{
    char rotulo[10], instrucao[1][2][10], flag;
    int simplificado;

    struct node *referencias[2];
    struct node **antecessores;
    int nrAntecessores;

    struct node *ant;
    struct node *prox;
} Estado;

And this is function insere() that sets values read from an input file in a new node:

void Insere(char *rotulo, char instrucao[][2][10], int qtdInstrucao, char flag){
    int i,j;
    Estado *NovoEstado;
    NovoEstado = (Estado*)malloc(sizeof(Estado));
    NovoEstado->prox = NULL;
    NovoEstado->ant = P->ult;
    strcpy(NovoEstado->rotulo, rotulo);
    NovoEstado->flag = flag;
    NovoEstado->antecessores = NULL;
    NovoEstado->nrAntecessores = 0;
    NovoEstado->simplificado = 0;

    for(i=0;i<qtdInstrucao;i++){
        realloc(NovoEstado->instrucao, i+1*sizeof(char[2][10]));
        strcpy(NovoEstado->instrucao[i][0], instrucao[i][0]);
        strcpy(NovoEstado->instrucao[i][1], instrucao[i][1]);
    }
}

This NovoEstado->flag = flag; isn’t working…
Right after I set it, if I print NovoEstado->flag i get the right value, but if I put it after that for by the end of the function, when I print NovoEstado->flag I get the first char of NovoEstado->rotulo…
The same happens when I try to print flag in main()…

I figure that’s because I’m not properly allocating memory space to flag in Insere(), is that right? And how do I fix it?

I’m pretty sure it’s an awful easy question, and that I possibily knew this once, but I forgot and can’t find it anywhere… So any help would be very appreciated

EDIT

Following ocdecio’s tip I created a pointer to an two-dimensional array, in order to have a dinamic 3 dimensional array.
My goal is to have a “table” like this:

  10 chars | 10 chars  
|__________|__________|
|__________|__________|
|__________|__________|

Where the number of lines is dinamic, but it’s always an array of 2 strings of 10 chars.

So now this is what I’m doing in main:

    char estado[127], rotulo[10], strInstrucoes[117], *conjunto = calloc(21, sizeof(char)), flag;
    char (*instrucao)[2][10];

    FILE * entrada;
    Automato *Aut = (Automato*)malloc(sizeof(Automato));

    if((entrada = fopen(argv[1], "r")) != NULL){
        CriaAutomato(Aut);
        while(fgets(estado, 127, entrada)){
            flag = 0;
            sscanf(estado,"%[^:]: %[^;]; %c", rotulo, strInstrucoes, &flag);
            instrucao = calloc(1, sizeof(char[2][10]));
            conjunto = strtok(strInstrucoes,"() ");
            for(i = 0; conjunto != NULL; i++){
                realloc(instrucao, i+1*sizeof(char[2][10]));
                sscanf(conjunto,"%[^,],%s", instrucao[i][0], instrucao[i][1]);
                printf("%s || %d\n", instrucao[i][1], i);
                conjunto = strtok(NULL, "() ");
            }
            Insere(Aut, rotulo, instrucao, i, flag);
            free(instrucao);
        }
        fclose(entrada);

But this isn’t working…
This is the input read from file

adsasdfg2: (abc,123) (def,456) (ghi,789);

but even before I call Insere I’m not assigning the right values to instrucao the way I want, as this is the output of that printf

123
454
789

instead of what I’m aiming for

123
456
789

What’s wrong?

(before someone asks, this is part of a homework, but not the homework. My task is to make a Deterministic Finite Automata minimizer, this is just a bug I’m getting related to data input)

Thanks a whole lot

  • 1 1 Answer
  • 1 View
  • 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-11T23:33:31+00:00Added an answer on May 11, 2026 at 11:33 pm

    Your problem is likely to be in this line:

    realloc(NovoEstado->instrucao, i+1*sizeof(char[2][10]));
    

    There is no need to allocate anything inside your structure because the field instrucao is statically defined by the statement instrucao[1][2][10], it is not a dynamically allocated structure.

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

Sidebar

Ask A Question

Stats

  • Questions 143k
  • Answers 143k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There is a jpg in the same folder as the… May 12, 2026 at 8:33 am
  • Editorial Team
    Editorial Team added an answer Create a new empy ASP.NET Website and then add existing… May 12, 2026 at 8:33 am
  • Editorial Team
    Editorial Team added an answer Can you put a formula in column d of worksheet… May 12, 2026 at 8:33 am

Related Questions

I'm trying to understand what kind of memory hit I'll incur by creating a
Okay here's the program I have typed up(stdio.h is included also): /* function main
I know how to download an html/txt page. For example : //Variables DWORD dwSize
I have c++ code that attempts to dynamically allocate a 2d array of bytes

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.