-1

I am having the segmentation fault issue for about 3 hours ago and I don't really figure it out why. I am trying to assign memory dinamically to terminos(struct) but I can't. I hope you can help me

    #include<stdio.h>
    #include<stdlib.h>
    typedef struct termino
    {
      int exponente;
      float cociente;
    } termino;

    typedef struct polinomio
    {

  termino* polinomio;
  int size;
} polinomio;

main()
{


int size_;
  termino* terminos;
  polinomio *polinomio_;
  polinomio_ = malloc(sizeof(polinomio));
  printf("%d",(sizeof(polinomio_)*2));

  printf("Bienvenido al cálculo de operaciones usando 1 polinomio.\n");
  printf("Ingrese la cantidad de términos que tendrá el polinomio.");  
  scanf("%d",size_);
  //assigning memory for terminos.
  terminos =(termino*) malloc(sizeof(termino) * size_);
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
Sebas
  • 324
  • 2
  • 10
  • `main()`--> `int main(void)` – Natasha Dutta May 22 '15 at 20:46
  • 1
    C and C++ are two different languages. Also, now sounds like a good time to get familiar with a debugger. "I have a segfault and can't figure out why" with no additional information is not a valid question. If you are using `gcc`, please change your command line to `gcc -Wall -Werror` before continuing. If you are using Visual Studio **pay attention** to any compiler warnings being emitted. They're there for a reason. – Jonathon Reinhart May 22 '15 at 20:48

1 Answers1

1

The main issue here is

 scanf("%d",size_);

change to

 scanf("%d",&size_);

because , scanf() expects a pointer-to-data type argument.

Related, from C11 standard, chapter §7.21.6.2, (emphasis mine)

d

Matches an optionally signed decimal integer, whose format is the same as expected for the subject sequence of the strtol() function with the value 10 for the base argument. The corresponding argument shall be a pointer to signed integer.

Apart from that,

  1. The recommended signature for main() is int main(void).
  2. Correct format specifier for sizeof is %zu.
  3. Please do not cast the result of malloc()
Community
  • 1
  • 1
Natasha Dutta
  • 3,242
  • 21
  • 23