-5

I want want to read a character string, over the stdin, so I have chosen fgets. But I got this warning: initialization makes integer from pointer without a cast.

Here is my code:

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

#define MAX_LINIA 301

int main(int argc,char *argv[]){
    char *buffer;
    printf("Enter the input\n");
    if (fgets(buffer,MAX_LINIA-1,stdin)==NULL) printf("Error")
    else printf("%s", buffer);
    return 0:
}

1 Answers1

0

You are using buffer without initialising it, which causes undefined behaviour.

You don't need to use pointer, and can use char array instead (given the max size already defined):

char buffer[MAX_LINIA];

The fgets then be better written as:

if(fgets(buffer,sizeof(buffer),stdin)==NULL)
artm
  • 17,291
  • 6
  • 38
  • 54