如何为char指针分配内存?

时间:2015-05-16 03:14:10

标签: c pointers memory malloc words

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

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

3 个答案:

答案 0 :(得分:2)

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

pthread_rwlock_unlock()

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

答案 1 :(得分:1)

指针位置+ 1 的计算错误。如果您想要编译问题,请将第100行更改为:

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

答案 2 :(得分:1)

您在功能findWords中遇到了一些问题:

  1. 下面,

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

    sizeof(point)sizeof(char*)中的point相同,位于函数char*的{​​{1}}中。这不是你想要的。使用

    fincdWords

    代替。但这可能会很慢,因为在每次迭代中都会调用for (i = 0; i < strlen(point); i++) 。所以我建议

    strlen
  2. 同样的问题也在这里:

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

    使用word[k] = malloc((words+1) * sizeof(*words)); 尝试的内容并不合理。我想你想要

    (words+1)
  3. 你们的论点全都搞砸了:

    word[k] = malloc( strlen(words) + 1 ); //+1 for the NUL-terminator
    

    你真的想要

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

    strcpy(word[j++], words); 的内容复制到words

  4. 下面:

    word[j++]

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

    释放它
    word
相关问题