c - 为什么我的指针数组返回一个 unicode 字符?

标签 c string memory-management malloc strcat

下面的代码 (char * newName) 中有一个指针数组,这给我带来了一些麻烦。我相信问题出在我用来在下面创建障碍的反斜杠之间。我在下面附上了一张照片。所有输出都正常工作,但是,在“santiago”一词的正下方有 3 个字符串输出,它们都以一个奇怪的 unicode 字符开头,我很困惑为什么会这样。奇怪字符(tbuq、bwi 等)后面的字母也都是正确的,所以我只需要去掉那个字符。我将非常感谢这里的一些指导 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;
}

最佳答案

由于您使用的是 strcat() ,您必须确保内存已初始化为零 (NULL),因为我们当然在谈论字符串。你可以这样做:

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

这样,您基本上在第一个字符中用 NULL 终止字符串,这意味着您有一个空字符串。然后,您将实际数据附加到该字符串。

如果没有这一步,您将得到一个未以 NULL 结尾的 C 字符串,这是 C 字符串标准库需要的东西,以便操作并知道字符串在哪里结束。


或者遵循@LinusGudmundsson 的建议(更昂贵,因为它将遍历 100 个元素以便将它们全部初始化),并将 malloc 调用替换为:

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

调用该方法零也为您初始化内存。在 Difference between malloc and calloc? 中阅读更多内容

你现在应该得到的输出(无论哪种方法):

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:不是问题原因,而是Do I cast the result of malloc?没有。

关于c - 为什么我的指针数组返回一个 unicode 字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58649609/

相关文章:

c - 即使我为指针结构分配内存,我的 strcpy 也无法工作

javascript - 将双引号替换为反斜杠加上 JSON 中的双引号

C++打印从字符串字符派生的整数

c - C语言typedef的语法和句法

c - 如何在C中的指针(**指针)的末尾添加一个字符串

C - 找到极限之间的所有友好数字

objective-c - -performSelector :onThread:withObject:waitUntilDone: 的内存问题

Python检查字符串是否包含字典键

c# - 关于 .net 内存管理的疑问

windows - 为什么操作系统不能使用整个 64 位进行寻址?为什么只有 48 位?