After using malloc
, name
gets printed but after allocating memory and typing in a string
, puts
doesn't print the string at all, neither does printf
...why is this?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *name;
int size;
printf("enter the size if name below\n");
scanf("%d", &size);
name =(char*) malloc(size * sizeof(char));//since my compiler returns pointr of type void, you have specify whether (int*) or (char*)
if (name== NULL)
printf("memory allocation failed,,,\n");
printf("%s\n",name);
printf("enter name below\n");
scanf("%s", name);
printf("name is\n%s", name);
name = (char*)realloc(name, 100*sizeof(char));
if (name == NULL)
printf("failed\n");
gets(name);
getchar();
puts(name);
free(name);
return 0;
}