I am new to C programming. Right now I am learning string and pointers.
As a beginner I find it difficult to find the mistake. I have written a code for dynamic allocation of a string and print the string using function.the code is given below.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void fun (char *);
int main()
{
int n;
char *s = NULL;
printf("enter the length of the string\n");
scanf("%d",&n);
s = (char*)malloc(n * sizeof(char));
printf("enter the string\n");
fgets(s, 20, stdin);
printf("string before passing is %s\n",s);
fun(s);
return 0;
}
void fun( char *p)
{
char *d;
d = p;
printf("string after passing is%s\n",d);
}
There is no error message while compiling. But the code doesn't take string.
Can anyone please help me in finding the mistake.