简体   繁体   English

在C中使用malloc创建字符串数组

[英]Creating an array of strings using malloc in C

I am completely novice in C, and just learned about the dynamic memory allocation using malloc, realloc, calloc and free. 我完全是C语言的新手,刚刚了解了使用malloc,realloc,calloc和free进行动态内存分配的知识。

I want to make a small programm which takes an int number as the number of the strings that will be given and then "scanf" them all. 我想做一个小程序,将一个int数作为要给出的字符串数,然后全部“扫描”它们。 Next play with these strings. 接下来用这些琴弦演奏。 For example find the most frequent and print it. 例如,找到最频繁的并打印。
For example when i run the programm and type : 例如,当我运行程序并键入:
5
car house dog tree tree 汽车之家狗树
It should print : 它应该打印:
tree 2 树2

I want scanf-printf because this is the input/output methods i am most familiar with at this point. 我需要scanf-printf,因为这是我目前最熟悉的输入/输出方法。
My code : 我的代码:

int main (){

int N,i,j ;

char *array;

int *freq;


 scanf("%d",&N);

 array = (char*)calloc(N,sizeof(char*));
 for (i=0;i<=N;i++){    
  scanf( ??? );  
 }

 free(array);  
 return 0;  
}

What should i enter in the scanf fuction in order to properly fill the array with strings ? 我应该在scanf函数中输入什么才能正确地用字符串填充数组? After i fill it will i use something like strcmp and a for loop in order to scan the array and find the most frequent word ? 填充后,我将使用诸如strcmp和for循环之类的东西来扫描数组并找到最频繁的单词吗? (I can store the frequencies in the *freq) (我可以将频率存储在* freq中)

You want to allocate an array of strings, in other words an array of pointers to characters, and that's exactly what you do allocate. 您要分配一个字符串数组,换句话说就是一个指向字符的指针数组,这正是您要分配的内容。 The problem is that you assign the pointer returned by calloc to an array of characters . 问题是您将calloc返回的指针分配给字符数组。

You have two choices here actually: Either change your declaration of array to be an "array" pointers to character, eg char **array , and then dynamically allocate the individual strings as well. 实际上,您有两个选择:将array的声明更改为指向字符的“ array”指针,例如char **array ,然后也动态分配各个字符串。 Something like this 像这样

// Allocate an array of pointers
char **array = calloc(N, sizeof(*array));

// Allocate and read all strings
for (size_t i = 0; i < N; ++i)
{
    // Allocate 50 characters
    array[i] = malloc(50);  // No need for `sizeof(char)`, it's always 1

    // Read up to 49 characters (to leave space for the string terminator)
    scanf("%49s", array[i]);
}

Or you can change the type of array , to be a pointer to fixed-sized "strings", like this 或者,您可以将array的类型更改为指向固定大小的“字符串”的指针,如下所示

// Define `my_string_type` as an array of 50 characters
typedef char my_string_type[50];

// Declare a pointer to strings, and allocate it
my_string_type *array = calloc(N, sizeof(*array));

// Read all strings from the user
for (size_t i = 0; i < N; ++i)
{
    // Read up to 49 characters (to leave space for the string terminator)
    scanf("%49s", array[i]);
}

Note that I don't cast the result of calloc or malloc . 注意,我 callocmalloc的结果 You should never cast void * in C. 您不应该在C中使用void *

In the scanf function, you need to select a format and the array you want the data to go. 在scanf函数中,您需要选择一种格式和想要数据存储的数组。 For example: 例如:

scanf("%[^\n]", array); 

您需要确保输入不超过您应用的大小,请尝试scanf("%s",array);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM