[英]Why is my pointer array returning a character in unicode?
There is a pointer array in the following code (char * newName) which is giving me some trouble.以下代码(char * newName)中有一个指针数组,这给我带来了一些麻烦。 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.所有的 output 都正常工作,但是,在“santiago”这个词的正下方有 3 个字符串输出,它们都以一个奇怪的 unicode 字符开头,我很难理解为什么会这样。 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.奇怪字符(tbuq、bwi 等)后面的字母也都是正确的,所以我只需要去掉那个字符。 I would greatly appreciate some guidance here我将非常感谢这里的一些指导
#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;
}
Since you are using strcat()
, you have to ensure that the memory is zero (NULL) initialized, since we are talking about strings, of course.由于您使用的是strcat()
,因此您必须确保 memory 已初始化为零(NULL),因为我们当然在谈论字符串。 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.这样,您基本上在第一个字符中以 NULL 终止字符串,这意味着您有一个空字符串。 Then, you append your actual data to that string.然后,你 append 你的实际数据到那个字符串。
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.如果没有该步骤,您将拥有一个未以 NULL 结尾的 C 字符串,这是 C 字符串标准库需要的,以便操作并知道字符串的结束位置。
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:或遵循@LinusGudmundsson 的建议(更昂贵,因为它会遍历 100 个元素以将它们全部初始化),并将 malloc 调用替换为:
newName[k] = calloc(100 , sizeof(char));
where the call to that method zero initializes the memory for you too.其中对该方法的调用为零也会为您初始化 memory。 Read more in Difference between malloc and calloc? 在 malloc 和 calloc 之间的区别中阅读更多内容?
The output you should get now (with either approach):您现在应该得到的 output (使用任何一种方法):
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? PS:不是问题的原因,而是我投出malloc的结果吗? No.不。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.