标签: c-strings

调试断言失败 c >= -1 && c <= 255

我很难理解我的代码的运行时错误。这是我的班级作业,我认为很容易,但发生了一些奇怪的事情。我在下面发布了我的代码。

这个作业的提示是创建一个程序,要求用户输入一些单词,然后它应该输出有多少个单词。我有一个朋友运行该程序并且它有效,但是,每当我尝试运行它时,我都会收到此运行时错误。[ https://i.stack.imgur.com/Vhqbe.jpg ]

请记住,我是 C++ 和一般编程的初学者,因此我无法理解非常技术性的答复。预先感谢您的帮助。

#include <string>
#include <iostream>
#include <cstring>

using namespace std;

int wordCounter(char *);

int main()
{
    char *stringArray = nullptr;
    stringArray = new char[120];

    cout << "Please enter a saying or a phrase that has more than one word: " << endl;
    cin.getline(stringArray, 120);

    int words = wordCounter(stringArray);
    cout << "Your statement contains " << words << " words." << endl;

    system("pause");
    return 0;
}

int wordCounter(char *stringTest)
{
    char characterToTest;
    int numberOfWords = …
Run Code Online (Sandbox Code Playgroud)

c++ c-strings visual-studio

2
推荐指数
1
解决办法
7250
查看次数

与MFC的CString连接最合适的方式是什么

我对 C++ 有点陌生,我的背景是 Java。我正在研究 hdc 打印方法。我想知道将字符串和整数的组合连接到一个 CString 中的最佳实践。我使用的是MFC的CString。

int i = //the current page
int maxPage = //the calculated number of pages to print


CString pages = ("Page ") + _T(i) + (" of ") + _T(maxPage);
Run Code Online (Sandbox Code Playgroud)

我希望它看起来像“第 1 页,共 2 页”。我当前的代码不起作用。我收到错误:

表达式必须具有整型或枚举类型

我发现了更困难的方法来完成我需要的事情,但我想知道是否有一种与我正在尝试的类似的简单方法。谢谢!

c++ mfc concatenation c-strings

2
推荐指数
1
解决办法
2614
查看次数

如何将一个变量的字符串分配给另一变量?

这是我在这个网站上的第一个问题。

如何将一个变量的字符串分配给另一变量。我在这里做错了什么?

#include<stdio.h>
#include<string.h>
main(){

char a[30],b[30];

scanf("%s",a);
b[30]=a[30];
printf("%s",b);

}

Run Code Online (Sandbox Code Playgroud)

c arrays c-strings strcpy

2
推荐指数
1
解决办法
1068
查看次数

尝试计算字符串中逗号的数量并保存到 int 计数器

我不断收到一条错误消息“警告:指针和整数之间的比较”。我尝试使用 char* 但仍然遇到相同的错误。我想计算字符串中出现的逗号数量,并将出现的次数放入计数器中。

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


int main(int argc, char *argv[]) {

    /*FILE *fp;
    fp = fopen("csvTest.csv","r");
    if(!fp){
        printf("File did not open");
    }*/


    //char buff[BUFFER_SIZE];
    char buff[100] = "1000,cap_sys_admin,cap_net_raw,cap_setpcap";    

    /*fgets(buff, 100, fp);
    printf("The string length is: %lu\n", strlen(buff));
    int sl = strlen(buff);*/

    int count = 0;
    int i;
    for(i=0;buff[i] != 0; i++){
        count += (buff[i] == ",");
    }
    printf("The number of commas: %d\n", count);




    char *tokptr = strtok(buff,",");
    char *csvArray[sl];

    i = 0;
    while(tokptr != NULL){
          csvArray[i++] = tokptr;
          tokptr …
Run Code Online (Sandbox Code Playgroud)

c counter loops c-strings char

2
推荐指数
1
解决办法
946
查看次数

Print the characters by iterating in c

#include <stdio.h>

void fun(char *p) {
    if (p) {
        printf("%c", *p);
        p++;    
    }
}

int main() {
    fun("Y32Y567");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Output:Y

Expected Output:Y32Y567

My questions why it is not working the expected way?

c arrays pointers c-strings char

2
推荐指数
1
解决办法
39
查看次数

在程序中获取不兼容的整数到指针转换错误。不确定这是如何/为什么会发生,但正在寻找解释

我正在尝试计算 char p[] 中有多少个破折号“-”。我循环遍历字符串,并使用 strcmp 函数将 p[i] 位置处的内容与“-”进行比较。如果它们相同,则 strcmp 函数返回 0。

int howmanyDash( char p[] ){
    int length = strlen(p);
    int i, count = 0;

    for (i = 0; i < length; i++)
    {
        if (strcmp(p[i], "-") == 0)
        {
            ++count;
        }   
    }

    return count;
    
}
Run Code Online (Sandbox Code Playgroud)
int main(){
    char word[20];
    scanf("%s", word);
    int dashCount = howManyDash(word);
    printf("Dashes: %d\n", dashCount);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我收到的错误如下:警告:不兼容的整数到指针转换将“char”传递给“const char *”类型的参数;取地址 & [-Wint-conversion] if (strcmp(p[i], "-") == 0)

此警告在第 7 行生成: if (strcmp(p[i], "-") == 0)

c c-strings char strchr function-definition

2
推荐指数
1
解决办法
51
查看次数

将 '\0' 放入 char[] 数组最后一个元素之后是否安全?

我出于一些实际原因感兴趣。我知道 C++'\0'在最后一个元素之后添加,但是手动添加它安全吗?我听说过undefined behavior,但是我感兴趣的是NULL字符是否实际上是内存中的下一个符号?

UPD:我明白,如果没有代码片段,我的问题还不够清楚。所以,我问这是否真的是保存并且不会导致未定义的行为?

const char* a = "Hello";
a[5] = '\0'; //this is wrong, will not compile on most compilers

char* b = new char[5];
b[5] = '\0';

char c[5];
c[5] = '\0';
Run Code Online (Sandbox Code Playgroud)

c c++ arrays initialization c-strings

2
推荐指数
2
解决办法
1100
查看次数

是否可以将 strcpy 与单字符变量一起使用?

这是我编写的程序中的主要函数,我需要对字符数组进行排序,使这些字符在开头具有偶数 ascii 代码,并且我想显示数组在每次迭代时的排序方式。

\n
#include<stdio.h>\n#include<stdlib.h>\n#include<string.h>\n\n\n int main ()\n { \n    int n, i,j;\n    char echange;\n    printf("array size :  ");\n    scanf("%d", &n);\n    char t[n];\n    for (i=0; i<n; i++)\n    {\n        printf("enter array elements : ");\n        scanf(" %c", &t[i]);\n    }\n\n  \n\n    for (j=0; j<n; j++)\n        for (i=0; i<n; i++)\n    {\n        if ((t[i] % 2!=0) && (t[i+1] % 2 ==0) && (i != n-1))\n           {\n              strcpy(echange, t[i]);\n              strcpy(t[i], t[i+1]);\n              strcpy(t[i+1], echange);\n              printf (" %c (%d)", t[i], t[i]);\n           }\n           else\n           printf(" %c (%d)", &t[i], t[i]);\n\n …
Run Code Online (Sandbox Code Playgroud)

c sorting c-strings char strcpy

2
推荐指数
1
解决办法
1631
查看次数

C++ std::any 将 std::any 的 C 字符数组转换为字符串的函数

#include <iostream>
#include <any>
#include <string>
#include <vector>
#include <map>
using namespace std;

string AnyPrint(const std::any &value)
{   
    cout << size_t(&value) << ", " << value.type().name() << " ";
    if (auto x = std::any_cast<int>(&value)) {
        return "int(" + std::to_string(*x) + ")";
    }
    if (auto x = std::any_cast<float>(&value)) {
        return "float(" + std::to_string(*x) + ")";
    }
    if (auto x = std::any_cast<double>(&value)) {
        return "double(" + std::to_string(*x) + ")";
    }
    if (auto x = std::any_cast<string>(&value)) {
        return "string(\"" + (*x) + "\")"; …
Run Code Online (Sandbox Code Playgroud)

c++ string c-strings rtti stdany

2
推荐指数
1
解决办法
962
查看次数

用 CString 替换 LPCTSTR 安全吗?

函数的参数需要LPCTSTR类型变量;如果我们传递一个CString类型变量怎么办?在这种情况下安全吗?或者我们应该注意什么?

例如这个函数:

Add(new CAToolbar, _T("Command Toolbar"), xtpBarTop);
Run Code Online (Sandbox Code Playgroud)

这里我想将_T("Command Toolbar")( LPCTSTRtype) 替换为CString str;. 像这样的东西:

Add(new CAToolbar, str , xtpBarTop);
Run Code Online (Sandbox Code Playgroud)

安全吗?

c++ mfc desktop-application c-strings visual-c++

2
推荐指数
1
解决办法
660
查看次数