-2

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:

  1. char newName[50]; and
  2. char *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;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Tanish Sharma
  • 47
  • 1
  • 6
  • 5
    The malloc line is redundant anyway and could be removed, since you immediately reassign `newName` in the next line – M.M Mar 27 '20 at 07:59
  • 2
    There are better tools than the `if` condition in the function — such as `isalpha()` from ``. Using `'A'`, `'Z'`, `'a'` and `'z'` would be better than using numbers, too. The better term is "Only letters are allowed". – Jonathan Leffler Mar 27 '20 at 08:15
  • 1
    Hint: `sizeof(char)` is _always_ 1, as `sizeof` measures size in multiples of `char` size. So `sizeof(char)` is like asking "how many apples are one apple?", so you can just use `1`, i.e. `malloc(50)`. – Erlkoenig Mar 27 '20 at 08:17
  • Does this answer your question? [Is an array name a pointer?](https://stackoverflow.com/questions/1641957/is-an-array-name-a-pointer) – anatolyg Mar 27 '20 at 08:25
  • I'm particularly confused about character arrays and their pointers. And @M.M said that the malloc line is redundant, why is that so, I don't get it. – Tanish Sharma Mar 27 '20 at 12:00

1 Answers1

0

No, they are not the same.

char name[50];

creates a char array of 50 bytes on the stack (at least in your case).

char * name;
name = malloc(50); // no need to cast the malloc() result

creates a char pointer and points it to a memory area of 50 bytes allocated on the heap.

Both ways to do it are wrong if you do

newName = checkName(name);

on the next step, because

  • in version 1 that won't work (you cannot assign a char * as returned by checkName() to a char[]) and
  • in version 2 that will make you lose access to your allocated block (that's called a memory leak).

If you want checkName() to act on a memory area you pass to it, you should, well, pass one to it.

glglgl
  • 89,107
  • 13
  • 149
  • 217
  • Can you please explain a bit more about why version 2 won't work, and what's memory leak? – Tanish Sharma Mar 27 '20 at 12:01
  • @TanishSharma I think there are resources on the internet which can explain a memory leak in a more concise way than I can. But in short, if you `malloc()` a memory area and lose its pointer, you cannot `free()` it again and the memory remains blocked forever (i. e. until you terminate your program). – glglgl Mar 27 '20 at 14:19