c++ - 指向 C 字符串的指针?

标签 c++ function pointers c-strings

<分区>

为了介绍 CS 分配,我在 Visual Studio 2010 中编写了一个 C++ 程序,它返回一个整数并接受一个指向 C 字符串的指针作为参数。我知道除了 int main 之外我还需要创建一个函数才能成功,但我不确定如何初始化指向预定义 char 数组的 char 指针数组(如果可能)。

该程序的目的是在预定义的限制范围内从用户那里获取评论,然后告知该用户该评论有多少个字符(包括空格)。

错误是:“char”类型的值不能赋值给“char *”类型的实体;

这是我无法编译的程序:

#include "stdafx.h"
 #include <iostream>
 #include <string>
 #include <conio.h>
 using namespace std;

 //function protoype
 void evalStr(char arr[]);
//variable
int length;


//main function
int main()
{
    const int SIZE = 201;
    char arr[SIZE];
    char *str[SIZE];
    cout << "Please enter a comment under " << (SIZE - 1) << " characters to calculate it's length: ";
    *str = arr[SIZE];

    cin.getline(arr, SIZE);


    length = strlen(*str);
    evalStr(arr);

    system("PAUSE");
    return 0;
}
//function defintion
/*  get the string
    count the number of characters in the string
    return that number

*/
void evalStr(char arr[])
{
    printf("The length of the entered comment is %d characters\n", length);
}

如果有使用 char 指针数组或指向字符串的指针的通用方法,则可以重做此代码以返回字符串的值,而不是使用 printf 语句。我做错了什么?

编辑:这是该程序的更新版本,它可以编译、运行并在达到或超过字符限制时通知用户。

// Accept a pointer to a C-string as an argument 
// Utilize the length of C-string in a function. 
// Return the value of the length
// Display that value in a cout statement.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;


//variables
const int SIZE = 201;
int length;

char arr[SIZE];
char *str;


//main function
int main()
{
    str = &arr[0];

    // Size - 1 to leave room for the NULL character
    cout << "Please enter a comment under " << (SIZE - 1) << " characters to calculate it's length: ";


    cin.getline(arr, SIZE);


    length = strlen(str);
    if (length == (SIZE - 1))
    {
        cout << "Your statement has reached or exceeded the maximum value of " 
             << length << " characters long.\n";
    }
    else
    {
    cout << "Your statement is ";
    cout << length << " characters long.\n";
    }


    system("PAUSE");
    return 0;
}
//function defintion
/*  get the string
    count the number of characters in the string
    return that number

*/
int countChars(int)
{
    length = strlen(str);

    return length;
}

最佳答案

让我们谈谈 main 中发生的事情:

int main()
{

好的,您将拥有 201 个字符的字符串。似乎有道理。

    const int SIZE = 201;

并且您声明了一个包含 201 个字符的数组:

    char arr[SIZE];

现在您声明了一个包含 201 个字符指针的数组。我不确定你为什么要这样做。我怀疑您认为这会做一些与实际不同的事情:

    char *str[SIZE];

这是合理的(除了“it's”的意思是“it is”,但你想要所有格版本,“its”):

    cout << "Please enter a comment under " << (SIZE - 1) << " characters to calculate it's length: ";

这会将第 201 个字符分配给 char 指针数组中的第一个字符指针。这是一个错误,因为:

  • 数组是零索引的,因此第 201 个字符(当您从零开始计数时)超出了数组的末尾。
  • 您还没有将arr 中的内存初始化为任何内容。
  • 您正在将 char 分配给 char *

所以,鉴于以上情况,我不确定你为什么要这样做:

    *str = arr[SIZE];

这看起来很合理:

    cin.getline(arr, SIZE);

这是一个错误,因为此时 str 没有指向包含有效字符串的内存。

    length = strlen(*str);

关于c++ - 指向 C 字符串的指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29997964/

相关文章:

c++ - 将结构数组传递给 pthread_create

c++ - const char[] 和 const char* 的区别

c++ - 如何处理(深度)嵌套函数调用中的默认值?

c++删除排序数组中的重复项-打印新数组

arrays - 什么时候返回数组或散列,什么时候只返回引用?

bash - 可以在 bash 子 shell 中调用一个函数作为后台作业吗?

c - 在调用 free() 时使用符号 (&)

c++ - 为什么重载!运营商需要一个常量返回

c++ - Qt Creator 中依赖文件的自动复制

javascript - 输入中仅允许数字和 "K"字符