1

Okay, so the problem concerns adding values through function to structure. Honestly, I couldn't solve the problem (spent a lot of time trying), so I am asking for your help. While executing the program, I get a segmentation fault. It occurs while using the variables from stack stos.

typedef struct e {
    int zaglebienie[100];
    char *nazwa_funkcji[100];
    int poz;
} *stack;

void put_on_fun_stack(int par_level, char *funame, stack stos) {
    int i = stos->poz;
    stos->zaglebienie[i] = par_level;
    char *funkcja = strdup(funame);
    stos->nazwa_funkcji[i] = funkcja;
    stos->poz++;
}

int main() {
    char *p = "makro";
    stack stos;
    stos->zaglebienie[0] = 0;
    put_on_fun_stack(1, p, stos);
    return 0;
}
8
  • 2
    poz in uninitialized. Commented Dec 8, 2016 at 15:38
  • 1
    *Never ever typedef pointers! You eventually run into trouble sooner or later. In your case it was sooner. Commented Dec 8, 2016 at 15:40
  • Translation to the above: stos is an non-allocated pointer. Yes, the typedefed pointer have confused me too. Commented Dec 8, 2016 at 15:42
  • @Olaf what do you mean exactly? Same problem occures while typedefing ...}stack and changing all the "->" to ".". I dont really get the point of your answers. Commented Dec 8, 2016 at 15:44
  • 1
    It's not the "same problem" for sure. Right now you are dereferencing some uninitialized and unallocated pointer. Commented Dec 8, 2016 at 15:47

1 Answer 1

1

You're declaring a pointer to stack but you're not allocating any memory to it.

And as already mentioned in the comments, using typedef with with a pointer will unnecessarily complicate your life.

So I suggest you create the struct stack and then in main declare a pointer to stack and allocate memory for it, somewhat like this:

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

typedef struct e {

    int zaglebienie[100];
    char *nazwa_funkcji[100];
    int poz;

} stack;


void put_on_fun_stack(int par_level, char *funame, stack *stos)
{
    int i = stos->poz;
    stos->zaglebienie[i] = par_level;
    char *funkcja = strdup(funame);
    stos->nazwa_funkcji[i] = funkcja;
    stos->poz++;
}

int main(void)
{
    char *p = "makro";

    // calloc to initialize stos variables to 0
    stack *stos = calloc(sizeof(stack), 1);

    printf("stos->poz before: %d\n", stos->poz);
    put_on_fun_stack(1, p, stos);
    printf("stos->poz after: %d\n", stos->poz);
    printf("stos->nazwa_funkcji[0]: %s\n", stos->nazwa_funkcji[0]);


    free(stos->nazwa_funkcji[0]);
    free(stos);
    return 0;
}

Output:

stos->poz before: 0
stos->poz after: 1
stos->nazwa_funkcji[0]: makro
Sign up to request clarification or add additional context in comments.

7 Comments

Thats pretty the thing i wanted to do, but still ive got error : 3[Error] invalid conversion from 'void*' to 'stack* {aka e*}' [-fpermissive] in Dev-C++ or Initializer element is not constant in VIM
I've answered in C (because of the C tag in your question). In C++ you have to explicitly cast the return value of malloc & co to the type you want. So try stack *stos = (stack*) calloc(sizeof(stack), 1);
I meant ive used C++ compiler from Dev and C compiler from VIM (gcc/cc command)
Still, there is an error "initializer element is not constant stack stos = (stack) calloc(sizeof(stack), 1); ^ Btw. Im using pointer to a structure and cant change it, cause thats the part of project.
@chrislacorunna: you did not make the changes suggested by yLaguardia. You must change your typedef to typedef struct e stack;. It is bad style to hide pointers behind typedefs, it is error prone and leads to confusing code for both the programmer and the reader.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.