如何为我的 char 指针分配内存?

How do I allocate memory to my char pointer?

我的任务是允许用户输入任何内容并打印出现的字母和单词,我们还必须打印出字符串中有多少个字母、两个、三个等字母单词。我已经让我的代码的字母部分开始工作,并且多次修改了我的单词功能,但仍然无法让单词查找功能开始工作。编译器说 char 指针字在明明是未声明的情况下未声明。我必须为它和字符数组分配内存吗?

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


void findLetters(char *ptr);
void findWords(char *point);


int main()
{
    char textStream[100]; //up to 98 characters and '\n\ and '[=10=]'

    printf("enter some text\n");
    if (fgets(textStream, sizeof (textStream), stdin)) //input up to 99 characters
    {
        findLetters(textStream);
        findWords(textStream);
    }
    else
    {
        printf("fgets failed\n");
    }

    return 0;
}

void findLetters(char *ptr) //find occurences of all letters
{
    int upLetters[26];
    int loLetters[26];
    int i;
    int index;

    for (i = 0; i < 26; i++) // set array to all zero
    {
        upLetters[i] = 0;
        loLetters[i] = 0;
    }
    i = 0;
    while (ptr[i] != '[=10=]') // loop until prt[i] is '[=10=]'
    {
        if (ptr[i] >= 'A' && ptr[i] <= 'Z') //stores occurrences of uppercase letters
        {
            index = ptr[i] - 'A';// subtract 'A' to get index 0-25
            upLetters[index]++;//add one
        }

        if (ptr[i] >= 'a' && ptr[i] <= 'z') //stores occurrences of lowercase letters
        {
            index = ptr[i] - 'a';//subtract 'a' to get index 0-25
            loLetters[index]++;//add one
        }
        i++;//next character in ptr
    }
    printf("Number of Occurrences of Uppercase letters\n\n");
    for (i = 0; i < 26; i++)//loop through 0 to 25
    {
        if (upLetters[i] > 0)
        {
            printf("%c : \t%d\n", (char)(i + 'A'), upLetters[i]);
            // add 'A' to go from an index back to a character
        }
    }
    printf("\n");
    printf("Number of Occurrences of Lowercase letters\n\n");
    for (i = 0; i < 26; i++)
    {
        if (loLetters[i] > 0)
        {
            printf("%c : \t%d\n", (char)(i + 'a'), loLetters[i]);
            // add 'a' to go back from an index to a character
        }
    }
    printf("\n");
}

void findWords(char *point)
{
    int i = 0;
    int k = 0;
    int count = 0;
    int j = 0;
    int space = 0;
    int c = 0;
    char *word[50];
    char word1[50][100];
    char* delim = "{ } . , ( ) ";

    for (i = 0; i< sizeof(point); i++) //counts # of spaces between words
    {
        if ((point[i] == ' ') || (point[i] == ',') || (point[i] == '.'))
        {
            space++;
        }
    }
    char *words = strtok(point, delim);
    for(;k <= space; k++)
    {
        word[k] = malloc((words+1) * sizeof(*words));
    }

        while (words != NULL)
        {
            printf("%s\n",words);
            strcpy(words, word[j++]);
            words = strtok(NULL, delim);
        }

    free(words);
}

这是因为您试图将指针位置+1 乘以指针大小。将第 100 行更改为:

    word[k] = malloc(strlen(words)+1);

这将解决你的编译问题,但你还有其他问题。

你计算的指针position+1是错误的。如果您希望编译问题消失,请将第 100 行更改为:

word[k] = malloc( 1 + strlen(words));

你在函数中遇到了一些问题 findWords:

  1. 这里,

    for (i = 0; i< sizeof(point); i++)
    

    sizeof(point) 与函数 fincdWords 中的 char* 中的 sizeof(char*) 等同于 point。这不是你想要的。使用

    for (i = 0; i < strlen(point); i++)
    

    相反。但这可能会很慢,因为 strlen 将在每次迭代中调用。所以我建议

    int len = strlen(point);
    for (i = 0; i < len; i++)
    
  2. 同样的问题也在这里:

    word[k] = malloc((words+1) * sizeof(*words));
    

    您尝试使用 (words+1) 没有任何意义。我想你想要

    word[k] = malloc( strlen(words) + 1 ); //+1 for the NUL-terminator
    
  3. 你把论点搞混了:

    strcpy(words, word[j++]);
    

    你真的想要

    strcpy(word[j++], words);
    

    words的内容复制到word[j++]

  4. 此处:

    free(words);
    

    words 从未分配内存。由于您释放了 malloc/calloc/realloc 未返回的指针,代码显示未定义行为。所以,删除它。 您为 word 的每个元素分配了内存。所以使用

    释放它
    for(k = 0; k <= space; k++)
    {
        free(word[k]);
    }