Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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;
}
share|improve this question
2  
poz in uninitialized. – Eugene Sh. yesterday
1  
*Never ever typedef pointers! You eventually run into trouble sooner or later. In your case it was sooner. – Olaf yesterday
    
Translation to the above: stos is an non-allocated pointer. Yes, the typedefed pointer have confused me too. – Eugene Sh. yesterday
    
@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. – chrislacorunna yesterday
1  
It's not the "same problem" for sure. Right now you are dereferencing some uninitialized and unallocated pointer. – Eugene Sh. yesterday

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
share|improve this answer
    
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 – chrislacorunna yesterday
    
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); – yLaguardia yesterday
    
I meant ive used C++ compiler from Dev and C compiler from VIM (gcc/cc command) – chrislacorunna yesterday
    
But that typecast didn't solve this problem? – yLaguardia yesterday
1  
@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. – chqrlie 18 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.