简体   繁体   English

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

[英]How do I allocate memory to my char pointer?

My assignment is to allow the user to enter any input and print the occurrences of letters and words, we also have to print out how many one letter, two, three, etc.. letter words are in the string. 我的任务是允许用户输入任何输入并打印字母和单词的出现,我们还必须打印出字符串中有多少个字母,两个,三个等。 I have gotten the letter part of my code to work and have revised my word function several times, but still can't get the word finding function to even begin to work. 我已经使代码的字母部分开始工作,并且多次修改了我的单词功能,但是仍然无法找到单词查找功能,甚至无法开始工作。 The compiler says the char pointer word is undeclared when it clearly is. 编译器说,显然没有声明char指针词。 Do I have to allocate memory to it and the array of characters? 我是否必须为其分配内存和字符数组?

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

This is because you are trying to multiply the pointer position+1 by the size of pointer. 这是因为您试图将指针的位置+1乘以指针的大小。 Change line 100 to: 将第100行更改为:

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

This will solve your compilation problem, but you still have other problems. 这将解决您的编译问题,但是您仍然有其他问题。

Your calculation of the pointer position+1 is wrong. 您对指针position + 1的计算是错误的。 If you want the compilation problem will go away change line 100 to: 如果您希望编译问题消失,则将第100行更改为:

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

You've got a couple of problems in function findWords : 您在函数findWords中遇到了两个问题:

  1. Here, 这里,

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

    sizeof(point) is the same as sizeof(char*) as point in a char* in the function fincdWords . sizeof(point)是一样sizeof(char*)作为pointchar*在功能fincdWords This is not what you want. 这不是您想要的。 Use 采用

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

    instead. 代替。 But this might be slow as strlen will be called in every iteration. 但这可能很慢,因为每次迭代都会调用strlen So I suggest 所以我建议

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

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

    It doesn't makes sense what you are trying with (words+1) . 您尝试使用(words+1)做什么都没有意义。 I think you want 我想你要

     word[k] = malloc( strlen(words) + 1 ); //+1 for the NUL-terminator 
  3. You got the arguments all mixed up: 您将所有争论混在一起:

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

    You actually wanted 你真的想要

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

    which copies the contents of words to word[j++] . 该拷贝的内容words ,以word[j++]

  4. Here: 这里:

     free(words); 

    words was never allocated memory. words从未分配过内存。 Since you free a pointer that has not been returned by malloc / calloc / realloc , the code exhibits Undefined Behavior. 由于您释放了malloc / calloc / realloc未返回的指针,因此代码表现出未定义的行为。 So, remove that. 因此,将其删除。 You allocated memory for each element of word . 您为word每个元素分配了内存。 So free it using 因此,请使用

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

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

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