English 中文(简体)
无效的转换char 到 char* - 复制字符串数组中的字符到另一个字符串数组
原标题:Invalid conversion char to char* - Copying char in string array to another string array
  • 时间:2010-02-09 20:11:50
  •  标签:
  • c++

我是C ++编程语言的初学者。我想编写一个程序,将字符串数组名为str中的字母复制到一个名为str_alpha的新数组中。

同样适用于数字,程序将其从str数组复制到str_digit数组中。

这是我的谦虚代码,可能有很多错误和问题。但这是我用我的很少的经验所能做的。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cctype>

using namespace std;

char str[100], str_alpha[100], str_digit[100];

int main()
{
    gets(str);

    for (int i=0 ; str[i] ; i++)
    {
        if (isalpha(str[i]))
        {
            strcpy (str_alpha[i] , str[i]);
        }
        else if (isdigit(str[i]))
        {
            strcpy (str_digit[i] , str[i]);
        }
    }

    cout << "Alpha is " << str_alpha << endl ;
    cout << "Number is : " << str_digit << endl ;

    return 0;
}

它给了我那些错误:

F:C++Progsstringmain.cpp||In function `int main() :|
F:C++Progsstringmain.cpp|18|error: invalid conversion from `char  to `char* |
F:C++Progsstringmain.cpp|18|error:   initializing argument 1 of `char* strcpy(char*,        const char*) |
F:C++Progsstringmain.cpp|18|error: invalid conversion from `char  to `const char* |
F:C++Progsstringmain.cpp|18|error:   initializing argument 2 of `char* strcpy(char*,   const char*) |
F:C++Progsstringmain.cpp|22|error: invalid conversion from `char  to `char* |
F:C++Progsstringmain.cpp|22|error:   initializing argument 1 of `char* strcpy(char*,    const char*) |
F:C++Progsstringmain.cpp|22|error: invalid conversion from `char  to `const char* |
F:C++Progsstringmain.cpp|22|error:   initializing argument 2 of `char* strcpy(char*, const char*) |
||=== Build finished: 8 errors, 0 warnings ===|

Help me please. Thanks in advance.

最佳回答

首先,strcpy复制的是C字符串(字符数组),而不是char。另外,strcpy(str_digit[i], str[i])strcpy(str_alpha[i], str[i])这两行代码即使不是这种情况,也仍然可能是错误的。由于您没有初始化str_digit和str_alpha数组,因此在打印它们时会得到许多垃圾值,如果其中任何一个垃圾值恰巧是0x00,则cout语句将无法打印整个字符串。正如先前提到的,您应该使用std::string而不是char[]char*。话虽如此,以下是您用于char[]std::string的正确版本的代码。

使用gets是不好的实践,你可能考虑使用std :: cin来代替。你可能想使用迭代器而不是简单的for循环。

//using char[]
#include <iostream>

using namespace std;

int main()
{
    char str[100] , str_alpha[100] , str_digit[100] ;
    int alpha_counter=0, digit_counter=0;

    cin.get(str, 99);

    for (int i=0 ; str[i] ; i++)
    {
        if(isalpha(str[i]))
        {
            str_alpha[alpha_counter] = str[i];
            alpha_counter++;
        }
        else if (isdigit(str[i]))
        {
            str_digit[digit_counter] = str[i];
            digit_counter++;
        }
    } 
    str_alpha[alpha_counter] = 0;
    str_digit[digit_counter] = 0;

    cout << "Alpha is " << str_alpha << endl ;
    cout << "Number is : " << str_digit << endl ;

    return 0;
}

将此翻译为中文:使用std::string的版本: 使用std::string的版本:

//using std::string
#include <iostream>

using namespace std;

int main()
{
    string str, str_alpha , str_digit;

    cin >> str ;

    for (string::iterator it = str.begin();it<str.end();it++)
    {
        if(isalpha(*it))
        {
            str_alpha += *it;
        }
        else if (isdigit(*it))
        {
            str_digit += *it;
        }
    } 

    cout << "Alpha is " << str_alpha << endl ;
    cout << "Number is : " << str_digit << endl ;

    return 0;
}

两个版本在 g++ 4.2.1 带有 -Wall-pedantic 的情况下都可以编译而无警告。

问题回答

我是C++编程语言的初学者。

不要使用char*等指针类型,而要使用C++预定义的类型,例如string

string str;
cin >> str;

string alpha;
string digit;

for (unsigned int i = 0; i < str.length(); ++i) {
    char chr = str[i];
    if (isalpha(chr))
        alpha.push_back(chr);
    else if (isdigit(chr))
        digit.push_back(chr);
}

此外,strcpy 如其名称所示,用于复制字符串,而不是单个char。您可以通过赋值来直接复制它们。无需调用函数。

问题在于 strcpy 是旨在复制整个字符串。在这种情况下,您正在复制单个字符,而不是整个字符串。对于单个字符,您可以使用简单的赋值。

有一些其他事情需要处理。首先,你有:

char str[100]; 字符数组 str[100];

在任何函数之外定义一个数组,数组的大小为100个字符,并将它们全部初始化为0。然后你会循环遍历这个数组,寻找被归类为字母或数字的字符——但这永远不会发生,因为零字节(而不是0)从未是其中的任何一个。

实际上,据我所知,您根本不需要str。您(显然)想要的大致是:

for (i in 0 to 127)
   if (alpha(i))
       add i to str_alpha
   else if (digit(i))
       add i to str_digit

正如Konrad Rudolph所指出的那样,将str_digit和str_alpha转换为std :: string比char数组更好。数组可以完成工作,但要得到真正正确的结果会更加费力。

strcpy 复制整个字符串。用普通赋值 (= 操作符) 复制字符。

你正在复制字符,虽然 strcpy 是设计用来复制字符串(字符数组)的。因此,编译器会抱怨因为你提供的是 char 而不是 char *。 如果你只是在复制字符,请使用:

str_alpha[i] = str[i];




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签