0

There is a pointer array in the following code (char * newName) which is giving me some trouble. I believe the issue is somewhere between the backslashes i used to create barriers below. I have attached a picture below. All of the output is working correctly, however, there are 3 outputs of strings right under the word "santiago" that all begin with a strange unicode character and I am stumped as to why that is happening. The letters that come after the strange character (tbuq, bwi, etc) are all correct as well, so I just need to get rid of that character. I would greatly appreciate some guidance here Picture of the strange characters

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

static int compare(const void* a, const void* b)
{
   return strcmp(*(const char**)a, *(const char**)b);
}

void sort(char* names[], int n)
{
   qsort(names, n, sizeof(char*), compare);
}

int main()
{
   int i, j, k, n;
   char alphabet[26];

   for(i = 0; i < 26; i++)
   {
      alphabet[i] = i + 97;
   }

   char newAlphabet[26];

   printf("input the new alphabet\n");
   for(i = 0; i < 26; i++)
   {
      scanf(" %c", &newAlphabet[i]);
   }

   printf("How many names will you input?");
   scanf(" %d", &n);

   char * names[n];

   printf("Enter the names: \n");   //erase before submitting

   //the user will now enter the names one by one
   for (i = 0; i < n; i++)
   {
      names[i] = (char *) malloc(100 * sizeof(char));  //allocating memory of 100 for each name pointer
      if (names[i] == 0)
      {
         printf("Error.");
         exit(1);
      }

      scanf("%s", names[i]);
   }

   for(i = 0; i < n; i++)
   {
      printf("%s\n", names[i]);
   }

   int nameConvert[n][100];
//convert the strings of names to their corresponding indexes based on the a = 0, b = 1, etc
   for(k = 0; k < n; k++)
   {
      for(i = 0; i < (strlen(names[k])); i++)
      {
         for(j = 0; j < 26; j++)
         {

            if((*(names[k] + i)) == alphabet[j])
            {
               nameConvert[k][i] = j;
               break;
            }
         }
      }
   }
////////////////////////////////////////////////////////////////////////////

   char * newName[n];

   for(i = 0; i < n; i++)
   {
      newName[i] = '\0';
   }

   for (k = 0; k < n; k++)
   {
      newName[k] = (char* ) malloc(100 * sizeof(char));

      for(i = 0; i < (strlen(names[k])); i++)
      {
         char tempstr[2];
         tempstr[0] = newAlphabet[nameConvert[k][i]];
         tempstr[1] = '\0';
         strcat(newName[k], tempstr);
      }
      printf("%s\n", newName[k]);
   }
/////////////////////////////////////////////////////////////////////////////

   for(i=0; i < n; i++)
   {
      for(j = 0; j < strlen(names[i]); j++)
      {
         printf("%d ", nameConvert[i][j]);
      }
      printf("\n");
   }

   sort(newName, n);

   for(i = 0; i < n; i++)
   {
      printf("%s\n", names[i]);
   }



   return 0;
}
gsamaras
  • 71,951
  • 46
  • 188
  • 305
Santy G
  • 19
  • 2
  • `(*(names[k] + i)`: probably better to rewrite that as `names[k][i]`. – Kaz Oct 31 '19 at 18:52
  • @Kaz thank you for the tip! any idea why i may be getting such strange output? – Santy G Oct 31 '19 at 18:54
  • 2
    Try changing malloc to calloc to make sure whatever memory you allocate is zeroed since you are using strcat: newName[k] = (char* ) malloc(100 * sizeof(char)); -> newName[k] = (char* ) calloc(100 , sizeof(char)); – Linus Gudmundsson Oct 31 '19 at 19:05
  • @LinusGudmundsson That did the trick, thank you so much! – Santy G Oct 31 '19 at 19:08

1 Answers1

2

Since you are using strcat(), you have to ensure that the memory is zero (NULL) initialized, since we are talking about strings, of course. You can do it like this:

newName[k] = malloc(100 * sizeof(char));
newName[k][0] = '\0';

That way, you basically NULL-terminate the string in the first character, which means that you have an empty string. Then, you append your actual data to that string.

Without that step, you had a C string that was not NULL-terminated, something that the C string standard library needs in order to operate and know where the string ends.


Or follow @LinusGudmundsson suggestion (more expensive, since it will iterate over the 100 elements in order to initialize them all), and replace that malloc call with this:

newName[k] = calloc(100 , sizeof(char));

where the call to that method zero initializes the memory for you too. Read more in Difference between malloc and calloc?

The output you should get now (with either approach):

input the new alphabet
b j k v q z c d e t u h w x n o p f g l y m r s i a
How many names will you input?3
Enter the names:
jake amy santiago
jake
amy
santiago
tbuq
bwi
gbxlebcn
9 0 10 4 
0 12 24 
18 0 13 19 8 0 6 14 
jake
amy
santiago

PS: Not the cause of the problem, but Do I cast the result of malloc? No.

gsamaras
  • 71,951
  • 46
  • 188
  • 305