Here is my code:
I've commented char newName[50];
I tried the same thing using both malloc
and newName[50]
. But the file doesn't compile if I don't use malloc
. Aren't both of them the same thing? I thought that they were same; now I'm confused.
Please clarify the difference between the two declarations:
char newName[50];
andchar *newName;
newName = (char *) malloc(50 * sizeof(char));
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char* checkName(char *s);
int main(void)
{
char name[50];
scanf("%s", name);
char *newName;
newName = (char *) malloc(50 * sizeof(char));
// char newName[50];
newName = checkName(name);
printf("%s", newName);
}
char* checkName(char *s)
{
int i = 0, j = 1;
int l = strlen(s);
char name[50];
strcpy(name, s);
while(j)
{
for(i = 0; i < l; ++i)
{
if(s[i] < 65 || (s[i] > 90 && s[i] < 97) || s[i] > 122)
{
printf("\nOnly alphabets are allowed, please Re-enter your name : ");
scanf("%s", s);
j = 1;
continue;
}
else
j = 0;
}
}
return s;
}