【问题标题】:How do I allocate memory to my char pointer?如何为我的 char 指针分配内存?
【发布时间】:2015-05-16 03:14:10
【问题描述】:

我的任务是允许用户输入任何输入并打印出现的字母和单词,我们还必须打印出字符串中有多少个字母、两个、三个等。字母词。我已经让我的代码的字母部分工作并修改了我的单词功能几次,但仍然无法让单词查找功能开始工作。编译器说 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 '\0'

    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] != '\0') // loop until prt[i] is '\0'
    {
        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);
}

【问题讨论】:

  • 您认为可以将您的内存分配问题归结为一个更小的程序吗?无需所有字母计数的东西,每个人都可以更轻松地进行调试(当您尝试简化它时,您甚至可能会发现问题所在!)

标签: c pointers memory malloc words


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

    1. 这里,

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

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

      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]);
      }
      

    【讨论】:

    • 我对我的代码进行了一些修改,并且之前的所有错误都消失了,但现在我在 strcpy(word[k],words) 写入位置遇到了访问冲突。
    • 请不要更改整个代码。它使答案和 cmets 不完整。发布一个新问题,提供必要的详细信息。
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 2019-01-10
      • 1970-01-01
      • 2021-11-22
      • 1970-01-01
      • 1970-01-01
      • 2015-08-05
      • 2022-01-15
      • 2021-12-12
      • 2011-02-23
      相关资源
      最近更新 更多