Home > Software engineering >  I'm trying to store ID of three employees using Dynamic Memory Allocation
I'm trying to store ID of three employees using Dynamic Memory Allocation

Time:02-11

Code -

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char *e;
    int len;

    for (int i = 1; i < 4; i  )
    {
        printf("Enter the size of the ID of the Employee %d\n", i);
        scanf("%d", &len);

        printf("Enter Employee ID of Employee %d\n", i);
        scanf("%s", &e);
    }

    e = (char *)malloc((len   1) * sizeof(char));

    for (int i = 1; i < 4; i  )
    {
        printf("ID of Employee %d is %s\n", i, e);
    }

    return 0;
}

Terminal -

Enter the size of the ID of the Employee 1
2
Enter Employee ID of Employee 1
A2
Enter the size of the ID of the Employee 2
2
Enter Employee ID of Employee 2
A7
Enter the size of the ID of the Employee 3
3
Enter Employee ID of Employee 3
AA1

Expected Output -

ID of Employee 1 is A2
ID of Employee 2 is A7
ID of Employee 3 is AA1

Output I am getting -

»D of Employee 1 is Φ
»D of Employee 2 is Φ
»D of Employee 3 is Φ

Here, »D and Φ refers to Garbage Value that changes every time when I run it in another terminal

I'm just learning the basics of Dynamic Memory Allocation but getting problems even in that
Pls Help

CodePudding user response:

In addition to the comments above, you have a glaring mistake in that you are attempting to use the memory before you've allocated it. Try this:

#include <string.h>

#define MAX_EMP 3

int main()
{
    char *e[MAX_EMP];
    int len[3];

    for (int i = 0; i < MAX_EMP; i  )
    {
        printf("Enter the size of the ID of the Employee %d\n", i   1);
        scanf(" %d ", &len[i]);

        e[i] = (char*)malloc((len   1) * sizeof(char));
        printf("Enter Employee ID of Employee %d\n", i   1);
        scanf(" %s ", e[i]);
    }

    for (int i = 0; i < MAX_EMP; i  )
        printf("ID of Employee %d is %s\n", i   1, e[i]);

    return 0;
}

Also, note that memory resources are rarely at such a premium that dynamic memory allocation makes sense for such small strings. It's more practical to just do this:

#include <string.h>

#define MAX_EMP 3
#define MAX_LEN 10

int main()
{
    char e[MAX_EMP][MAX_LEN   1];

    for (int i = 0; i < MAX_EMP; i  )
    {
        printf("Enter Employee ID of Employee #%d\n", i   1);
        fgets(e[i],MAX_LEN   1,stdin);
    }

    for (int i = 0; i < MAX_EMP; i  )
        printf("ID of Employee #%d is %s\n", i   1, e[i]);

    return 0;
}

CodePudding user response:

  • you need to store the data for every employee (probably in an array)
  • in scanf("%s", something) something must be of type char *. In your code it is char **.
  • don't use scanf %s because it assumes the buffer is large enough for the input. Use fgets or scanf %ms
  • in new versions there is also scanf("%ms", something) where something is char **. It allocates the string and stores the pointer to it in *something
  • having i start at 0 is easier when working with arrays
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char *ids[3];   // store the id for every employee, no need to store the length

    for (int i = 0; i < 3; i  )
    {
        printf("Enter the size of the ID of the Employee %d\n", i   1);
        int len;
        scanf("%d", &len);
        ids[i] = malloc(len   1);   // no need to cast the pointer, sizeof(char) is always 1

        printf("Enter Employee ID of Employee %d\n", i   1);
        scanf("%s", ids[i]);   // use fgets here
    }

    for (int i = 0; i < 3; i  )
    {
        printf("ID of Employee %d is %s\n", i   1, ids[i]);
        free(ids[i]);   // free it when you are done with it
    }

    return 0;
}```
  • Related