将字符串传递给函数并返回结构

时间:2013-04-05 10:30:01

标签: c string structure

我正在尝试实现将字符串传递给函数并返回包含该字符串数据的结构的简单目标。我写过这个,但是当我尝试编译它时,我遇到了运行时错误,我无法理解为什么。谢谢你好好看看。

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

struct stringStats
{
    int length;
    int uppercase;
    int lowercase;
    int digits;
    int nonAlphaNum;
};

struct stringStats stringReader (char anyString[])
{
    int i;
    struct stringStats returned = {0, 0, 0, 0, 0 };

    returned.length = strlen(anyString);

    for (i = 0; anyString[i] != '\0'; ++i)
    {
        if (isupper(anyString[i]))
            ++returned.uppercase;
        if (islower(anyString[i]))
            ++returned.lowercase;
        if (isdigit(anyString[i]))
            ++returned.digits;
        if (isalnum(anyString[i]) == 0)
            ++returned.nonAlphaNum;
    }
    return returned;
}

int main(void)
{
    struct stringStats stored;
    char passedString[] = "Th1s string's g0t it all!";
    stored = stringReader(passedString);
    printf ("%i\n%i\n%i\n%i\n%i\n", stored.length, stored.uppercase, stored.lowercase,
                                    stored.digits, stored.nonAlphaNum);

return 0;
}

3 个答案:

答案 0 :(得分:2)

这是不正确的:

char passedString = "Th1s string's g0t it all!";

不确定如何编译(建议在最高警告级别编译并将警告视为错误):

$ gcc -O2 -Wall -Werror -std=c99 -pedantic main.c -o main -pthread
main.c: In function ‘main’:
main.c:38:25: error: initialization makes integer from pointer without a cast [-Werror]
main.c:39:5: error: passing argument 1 of ‘stringReader’ makes pointer from integer without a cast [-Werror]
main.c:14:20: note: expected ‘char *’ but argument is of type ‘char’
cc1: all warnings being treated as errors

类型应为char[]

char passedString[] = "Th1s string's g0t it all!";

请注意,返回类型是合法的,因为返回的是局部变量的副本(按值),而不是局部变量的地址。只是警告复制struct是危险的,如果struct包含指针成员,因为现在两个struct的成员指向同一个地址(潜在的悬挂指针源)。

本地struct确实需要初始化,因为它目前不是:

struct stringStats returned = {0}; /* All members initialized to zero. */

for循环条件不正确,将导致循环体永远不会被执行(第一次评估为0 != '\0',这将是假的)。改为:

for (i = 0; anyString[i]; ++i)
{
}

答案 1 :(得分:2)

这里有几个问题。

  1. 在main()中你有char passedString = "Th1s string's g0t it all!"; - char是存储一个字符,所以这应该是char *passedString = "Th1s string's g0t it all!";

  2. 在递增之前,您没有初始化returned的成员。

  3. 你的for循环中的终止条件是检查i本身的值,而你实际上想要检查anyString[i]的值以终止空字符。

答案 2 :(得分:0)

您将本地结构的值返回给调用对象。根据我的经验,传递引用更安全,并在内部执行操作。

看看这个::

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

struct stringStats
{
    int length;
    int uppercase;
    int lowercase;
    int digits;
    int nonAlphaNum;
};

int stringReader (char anyString[], struct stringStats *returned)
{
    int i;
    returned->digits=0;
    returned->length=0;
    returned->lowercase=0;
    returned->nonAlphaNum=0;
    returned->uppercase=0;

    returned->length = strlen(anyString);

    for (i = 0; anyString[i] != '\0'; ++i)
    {
        if (isupper(anyString[i]))
            ++returned->uppercase;
        if (islower(anyString[i]))
            ++returned->lowercase;
        if (isdigit(anyString[i]))
            ++returned->digits;
        if (isalnum(anyString[i]) == 0)
            ++returned->nonAlphaNum;
    }
    return 0;
}

int main(void)
{
    struct stringStats stored;
    char passedString[] = "Th1s string's g0t it all!";
    stringReader(passedString,&stored);
    printf ("%i\n%i\n%i\n%i\n%i\n", stored.length, stored.uppercase, stored.lowercase,
                                    stored.digits, stored.nonAlphaNum);

    return 0;
}

这个编译并在我的结尾完美运行。希望这有帮助!