标签: c-strings

使用指向char的指针时访问冲突写入位置

我正在写一个非常简单的程序,从字符串中删除重复的字符.我运行了visual studio并得到了错误:

inteviews.exe中0x00d110d9处的未处理异常:0xC0000005:访问冲突写入位置0x00d27830.

我真的不明白这是什么问题.当前单元格获取下一个单元格的值.

void remove(char *str, char a) {
    while (*str != '\0') {
        if (*(str+1) == a) {
            remove(str + 1, a);
        }

        *str = *(str +1 );//HERE I GET THE ERROR
        ++str;
    }
}


int _tmain(int argc, _TCHAR* argv[])
{
    char *str = "abcad";

    while (*str != '\0') {
        remove(str,*str);
        str++;
    }

    std::cout << str << std::endl;

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

编辑:

我已经尝试将其更改为char str[] = "abcad"但我仍然得到相同的错误.

c++ string c-strings

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

你如何在c中迭代一组字符数组?

您是否必须手动循环遍历数组并获取每个字符数组的strlen计数,求和,使用求和值分配目标,然后再次循环数组?

如何找到包含字符数组的数组的大小,以便迭代它们?

c c-strings

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

是否有一个简洁的方法来strdup()后跟strcat()?

假设我想复制一个字符串,然后将值连接到它.

使用stl std :: string,它是:

string s = "hello" ;
string s2 = s + " there" ; // effectively dup/cat
Run Code Online (Sandbox Code Playgroud)

在C:

char* s = "hello" ;
char* s2 = strdup( s ) ; 
strcat( s2, " there" ) ; // s2 is too short for this operation
Run Code Online (Sandbox Code Playgroud)

我知道在C中执行此操作的唯一方法是:

char* s = "hello" ;
char* s2=(char*)malloc( strlen(s) + strlen( " there" ) + 1 ) ; // allocate enough space
strcpy( s2, s ) ;
strcat( s2, " there" ) …
Run Code Online (Sandbox Code Playgroud)

c c-strings

5
推荐指数
1
解决办法
1592
查看次数

用C++进行电子邮件验证

好的,所以我正在尝试制作一个允许用户输入电子邮件的程序.如果符合两项规定,他们的电子邮件将被视为有效:A.那里必须有某个"@"符号和B."@"之后必须有一段时间.我在大多数情况下都得到了代码,但是在验证具有"@"符号前一段时间的电子邮件时遇到了一些困难.如果他们在"@"符号之前有一段时间,那么他们就被视为有效,但他们不应该这样.例如,输入text.example@randomcom被认为是有效的.

任何人都可以帮我弄清楚我做错了什么?先感谢您!

#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;

int main()
{
    int x = 25; //random size enough to hold contents of array plus one for               null terminator
    char input[x]; //array to hold input
    int sizeOf; //holds length of input array
    char* ptr = nullptr; //pointer
    char* ptr2 = nullptr; //pointer

    cout << "Enter your email address\n";
    cin.getline(input,x);
    sizeOf = strlen(input);

    for(int i = 0; i < sizeOf; i++)
    {
        ptr= strstr(input, "@"); //searches input …
Run Code Online (Sandbox Code Playgroud)

c++ email validation c-strings

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

使char*指向另一个字符串文字泄漏内存吗?

我对这个简单的例子有一些误解:

char *s = "abc";
s = "def";
Run Code Online (Sandbox Code Playgroud)

第二个字符串的赋值会导致内存泄漏,还是会被正确替换?如果前者是真的,我该如何替换s正确方法的价值?

c string memory-leaks c-strings string-literals

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

将double转换为Char Array C++

我试图将一个double转换(和舍入)到一个char数组,而不是先在double上转换为std :: to_string.但是,我正在接收随机内存文本.我究竟做错了什么?

这是我的代码:

double d = 1.0929998; 
d = std::round(d * 100) / 100;

char s[sizeof(d)];
std::memcpy(s,&d,sizeof(d));
Run Code Online (Sandbox Code Playgroud)

结果:

s: q =×£pñ?

预期价值:

s: 1.09

c++ string double c-strings

5
推荐指数
1
解决办法
3932
查看次数

char [](c-string)的初始化标准

考虑以下代码:

int main(){
    char hi[5] = "Hi!";
    printf("%d", hi[4]);    
}
Run Code Online (Sandbox Code Playgroud)

将打印什么?更重要的是,是否有什么会被印制的标准一提,在最新的 C ++标准?如果有提及,它在哪里?

由于某种原因,关于此的最新信息非常难以找到,并且各种来源的信息相互矛盾。我尝试过en.cppreference.com和cplusplus.com,前者没有任何信息,后者声称这些值不确定。然而,

using namespace std;
int main(){
    char mySt[1000000] = "Hi!";
    for(int i=0;i<1000000;++i){
        if(mySt[i]!='\0') cout << i <<endl;
    }
}
Run Code Online (Sandbox Code Playgroud)

这只会在我的系统上打印0、1和2,因此我希望将“ rest”初始化为“ \ 0”。据我所知char mySt[100000]=""初始化了整个数组'\0',并且说它与任何其他字符串文字都不同感到很尴尬。但是,如果我的讲师另有声明(他声称这是“未定义的”),则我需要用最新的具体证据(如有)来备份我的主张。

我在MinGW g ++ 8.2.0上编译。先感谢您。

澄清,

c++ standards c-strings

4
推荐指数
1
解决办法
114
查看次数

What(): basic_string::_M_construct null 无效

我正在制作一个程序,我需要使用一个将字符串的标记存储在向量中的函数。该功能无法正常工作,因此我在较小的程序上尝试了该功能。当然,我使用了字符串分词器函数。但它没有按预期工作。首先,这是代码:

#include <向量>
#include <字符串>
#include <cstring>
使用命名空间 std;

int main()
{
    矢量<字符串> v;
    字符串输入=“我的名字是阿曼库马尔”;
    
    char* ptr = strtok((char*)input.c_str(), " ");
    v.push_back((字符串)ptr);

    而(指针)
    {
        ptr = strtok(NULL," ");
        v.push_back((字符串)ptr);
    }
    cout<<"出来了";
    for(字符串 s:v)
    {
        cout<<s<<endl;
    }
}

现在问题来了。我认为这个问题与命令有关:

(string)ptr
Run Code Online (Sandbox Code Playgroud)

这个东西在第一次调用中工作得很好,但在 while 循环中出现时会出错。如果我将其注释掉并打印 ptr,那么它可以正常工作,但是程序会在 while 循环后终止,甚至不会执行

cout<<"coming out";
Run Code Online (Sandbox Code Playgroud)

不考虑向量的内容。但同样,如果我也不打印 ptr,则会打印存储在向量中的第一个令牌“My”。我确实找不到造成这种情况的原因。任何建议都会有帮助。

c++ string vector c-strings strtok

4
推荐指数
1
解决办法
1万
查看次数

为什么 strnlen() 不考虑包含在 C23 中?

功能strdup()strndup()最终纳入即将推出的 C23 标准:

7.24.6.4strdup函数

概要

#include <string.h>
char *strdup(const char *s);
Run Code Online (Sandbox Code Playgroud)

strdup函数在分配的空间中创建由 指向的字符串的副本s,就像通过调用 一样malloc

返回
strdup函数返回一个指向重复字符串的第一个字符的指针。返回的指针可以传递给free. 如果无法分配空间,则该strdup函数返回空指针。

7.24.6.5strndup函数

概要

#include <string.h>
char *strndup(const char *s, size_t size);
Run Code Online (Sandbox Code Playgroud)

strndup函数创建一个字符串,该字符串初始化为不超过size由 指向的数组的初始字符s,直到第一个空字符(以先到者为准),在分配的空间中,就像调用 一样malloc。如果 by 指向的数组的s第一个字符中不包含 null size,则将 null 附加到数组的副本中。

返回
strndup函数返回一个指向所创建字符串的第一个字符的指针。返回的指针可以传递给free. 如果无法分配空间,则该strndup函数返回空指针。

strnlen为什么不考虑包含POSIX-2008 函数?

#include …
Run Code Online (Sandbox Code Playgroud)

c c-strings language-lawyer c23

4
推荐指数
1
解决办法
377
查看次数

range::starts_with 对 C 样式字符串参数有何作用?

下面的代码打印1010

#include <iostream>
#include <range/v3/algorithm/starts_with.hpp>

int main () {
    using namespace std::literals;
    std::cout << ranges::starts_with("hello world"s, "hello"s)
              << ranges::starts_with("hello world"s, "hello")
              << ranges::starts_with("hello world",  "hello"s)
              << ranges::starts_with("hello world",  "hello") << std::endl; // prints 1010

}
Run Code Online (Sandbox Code Playgroud)

这是为什么?

我还注意到,如果两个刺是相同的,那么最后一个案例也会返回1

    std::cout << ranges::starts_with("hello world"s, "hello world"s)
              << ranges::starts_with("hello world"s, "hello world")
              << ranges::starts_with("hello world",  "hello world"s)
              << ranges::starts_with("hello world",  "hello world") << std::endl; // prints 1011
Run Code Online (Sandbox Code Playgroud)

我知道 a std::string,例如"bla"s,是一个范围,并且char[N]给定一个常数 , a 也是一个范围N。所以我不明白为什么

c++ string substring c-strings range-v3

4
推荐指数
1
解决办法
87
查看次数