The program reads lines from the standard input. Each line is printed on the standard output preceded by its line number. The program has no built in limit on how long a line it can handle.
my answer is:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 30
int main(){
int line_n=0;
char line[MAXSIZE];
char *p;
while(gets(&line))
{
if('\n'){
if(*line=='q') break;
else if(strlen(line)>MAXSIZE){
p=&line;
p=(char *)malloc(sizeof(strlen(line))); //failed to use dynamic memory allocation here
printf("%d).",line_n);
printf("%s\n",line);
}
else{
printf("%d).",line_n);
printf("%s\n",line);
}
}
line_n++;
}
I am pretty new to C programming and I need help with this dynamic memory allocation. I have overflows when my input is bigger than MAXSIZE.