1

The goal is to get 'n' number of names as input and arrange them in alphabetical order using dynamic memory allocation. If i input 4 names the code is working fine. But if i input more than 5, the code cuts off after i enter the fifth name. It is not accepting the 6th name even if i give n as 6. Can anyone tell me the reason why? And the solution to it? Code:

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

void swap(char[], char[]);

int main()
{
    char** name;
    int i, n, j, y;
    printf("Enter the number of names:");
    scanf("%d", &n);
    name = (char**)malloc(n * sizeof(char));
    for (i = 0; i < n; i++)
    {
        name[i] = (char*)malloc(100 * sizeof(char));
    }
    printf("Enter the names:\n");
    for (i = 0; i < n; i++)
    {
        scanf("%s", *(name + i));
    }
    for (i = 0; i < n; i++)
    {
        for (j = i + 1; j < n; j++)
        {
            y = strcmp(name[i], name[j]);
            if (y >= 0)
            {
                swap(name[i], name[j]);
            }
        }
    }
    for (i = 0; i < n; i++)
    {
        printf("%s\n", name[i]);
    }
    return 0;
}

void swap(char a[], char b[])
{
    char temp[20];
    strcpy(temp, a);
    strcpy(a, b);
    strcpy(b, temp);
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 4
    `name=(char**)malloc(n*sizeof(char));` is wrong. First of all [don't cast the result of `malloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc); Secondly you're not allocating enough memory. I suggest `name = malloc(n * sizeof *name)` instead. – Some programmer dude Apr 08 '22 at 07:18
  • 2
    Your `name=(char**)malloc(n*sizeof(char));` line is missing a `*` - should be `name=malloc(n*sizeof(char*));`, so you allocate `n` times the size of a pointer. – Adrian Mole Apr 08 '22 at 07:18
  • 2
    I also recommend you take some time to fix the indentation of your code. Properly and (more importantly) consistently indented code makes it much easier to read and understand. And consistency is important elsewhere as well, for example why in one case do you use `*(name + i)` instead of `name[i]` which you use everywhere else? And please limit the possible input size for `scanf`, to not risk overflowing your allocated memory. And always check what `scanf` [*returns*](https://en.cppreference.com/w/cpp/io/c/fscanf#Return_value). – Some programmer dude Apr 08 '22 at 07:22
  • Oh and considering that you allocate space for 100 characters for your strings, why does the `swap` function limit its strings to only 20? (And it's not a true limitation, as a longer string can be used leading to buffer overflows and undefined behavior.) – Some programmer dude Apr 08 '22 at 07:23
  • Your string size management is poor too: you allocate 100 bytes per string, but `swap` function only handles 20 bytes strings. More over, you don't check if user inputed a word that fits in 100 bytes – Mathieu Apr 08 '22 at 07:23

1 Answers1

0

In this statement the size of the allocated memory is specified incorrectly

name = (char**)malloc(n * sizeof(char));
                          ^^^^^^^^^^^^

You have to write

name = (char**)malloc(n * sizeof(char *));   
                          ^^^^^^^^^^^^^

The swap function in general is incorrect. For starters it is unclear why there is used the magic number 20

char temp[20];

while the allocated character arrays have sizes equal to 100.

name[i] = (char*)malloc(100 * sizeof(char));

What you need is just to swap pointers pointing strings. For example the function swap can be declared and defined the following way

void swap( char **a, char **b )
{
    char *temp = *a;
    *a = *b;
    *b = temp;
}

and called like

swap( name + i, name + j );

Also to make the input safer you should write

scanf( "%99s", *( name + i ) );

Pay attention to that you should free all the allocated memory when the arrays will not required any more.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335