标签: c-strings

C++中的Cstring - 使用标准C函数导致分段错误

对于某些函数我想在函数中创建一个字符串的副本,然后操纵它 - 由于一些奇怪的原因,我不能strcpy工作(给我一个分段错误) - 我也尝试将arg作为字符串传递,这也不起作用(g ++抛出一个错误,说它期望一个char*)

#include <iostream>
#include <cstring>

using namespace std;
void copy_string(char* stri);

int main ()
{
  copy_string("sample string");

  return 0;
}

void copy_string(char* stri) {
  char* stri_copy;

  strcpy(stri_copy, stri);

  cout << "String: " << stri_copy;

}
Run Code Online (Sandbox Code Playgroud)

我不确定我明白为什么会这样.

所以我的两个问题是:

  1. 为什么会这样 - 是否有一个简单的解决方案?
  2. 什么是最简单/最有效的方法来创建传递给函数的字符串的本地副本?

谢谢!

c++ c-strings

0
推荐指数
1
解决办法
1186
查看次数

反转c型字符串

这是我的代码:

    void reverseStr(char *str)
{
    if (str == NULL) return;
    int i=0, j=strlen(str) -1;
    while(i<j)
    {
        char temp = str[j];  //i think this is the cause of the problem
        str[j] = str[i];
        str[i] = temp;
        i++;
        j--;
    }
}
Run Code Online (Sandbox Code Playgroud)

所以这里是它的所在:

int main()
{   
    char *str = "Forest Gump";
    reverseStr(str);
    cout << str;
}
Run Code Online (Sandbox Code Playgroud)

这是我的错误:

/Applications/TextMate.app/Contents/SharedSupport/Bundles/C.tmbundle/Support/bin/bootstrap.sh:line 7:1931总线错误"$ 3".out

有什么想法吗?提前致谢.

c++ c-strings

0
推荐指数
1
解决办法
975
查看次数

如何比较c中的字符

我有一个小项目,我需要比较一个流的第一个字节.问题是该字节可以是0xe5或任何其他不可打印的字符,因此表示该特定数据是坏的(一次读取32位).我可以允许的有效字符是AZ,az,0-9,'.' 和空间.

目前的代码是:

FILE* fileDescriptor; //assume this is already open and coming as an input to this function.
char entry[33];

if( fread(entry, sizeof(unsigned char), 32, fileDescriptor) != 32 )
{
    return -1; //error occured
}

entry[32] = '\0';  //set the array to be a "true" cstring.

int firstByte = (int)entry[0];

if( firstByte == 0 ){
    return -1;    //the entire 32 bit chunk is empty.
}

if( (firstByte & 0xe5) == 229 ){       //denotes deleted.
    return -1;    //denotes deleted.
}
Run Code Online (Sandbox Code Playgroud)

所以问题在于,当我尝试执行以下操作时: …

c linux c-strings cstring standard-library

0
推荐指数
1
解决办法
1896
查看次数

如何初始化c-strings数组(无stl)

我想在MSVC2010中初始化零指针的c字符串数组

// Foo.h
#pragma once
class Foo {
  int sz_;
  char **arr_; 
public:
  Foo();
  ~Foo();
  // ... some other functions
};

// Foo.cpp
#include "Foo.h"
#define INITIAL_SZ 20

Foo::Foo() : sz_(INITIAL_SZ) {
  // there I have to initialize arr_ (dynamic array and can be enlarged later)
  arr_ = (char **)calloc(INITIAL_SZ * sizeof (char *)); // ??? 
  // or maybe arr_ = new ...
}
Run Code Online (Sandbox Code Playgroud)

如何正确初始化arr_?我不被允许使用STL,MFC等.

c++ arrays c-strings crt c++11

0
推荐指数
1
解决办法
449
查看次数

随机播放用户输入的字符串

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char rand(char x);
int main()
{
char input[80] = {0};
char rando[80] = {0};
char choice = 0;
char rando2[80] = {0};
if(strlen(input) >= 10 && strlen(input) <= 80); {
    printf("Please enter a string with 10-80 characters: ");
    scanf("%s", input);
    printf("Orginal string: %s\n", input);
    rando = my_rand(input);
    printf("New string: %s\n", rando);  }
else{
    return 0; }
printf("Would you like to shuffle this string again?(y or n): ");
scanf("%c\n", &choice);
if( choice == 'y') { …
Run Code Online (Sandbox Code Playgroud)

c random shuffle c-strings

0
推荐指数
1
解决办法
1942
查看次数

以这种方式比较字符串是一种好的风格吗?

我有这个想法在.cpp文件中使用:

namespace
{
    bool operator==(char const* const a, char const* const b) noexcept
    {
        return !::std::strcmp(a, b);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是好风格吗?

编辑:

我认为有条件的c ++ 1z方式,即完成同样的事情,将使用新std::string_view类进行比较.

c++ comparison operator-overloading c-strings c++17

0
推荐指数
1
解决办法
104
查看次数

关于strcmp()程序的C代码有什么问题?

我正在编写一个程序来比较两个字符串而不使用strcmp().但是,我无法得到我想要的结果.这是我的程序的代码.

#include<stdio.h>
int main(int argc, char const *argv[]) {
   int i,j;
   char a[90],b[90];
   printf("Enter the first string:");
   scanf("%s", &a[90]);
   printf("Enter the second string:");
   scanf("%s", &b[90]);
   for ( i = 0; a[i] != '\0' && b[i] != '\0'; i++) {
      if (a[i] == b[i]) {
         /* code */
         printf("Equal %d \n", a[i]-b[i]);
continue;
      } if (a[i] > b[i]) {
         /* code */
         printf("ai is big %d \n", a[i]-b[i]);
         break;
      }
      if (a[i] < b[i]) {
         /* code */
         printf("%d bi is …
Run Code Online (Sandbox Code Playgroud)

c string c-strings strcmp

0
推荐指数
1
解决办法
109
查看次数

为什么/何时包含C字符串的终止'\ 0'字符?

我是C的新手,我们需要手动将终止'\ 0'字符添加到字符串时有点困惑.给定此功能来计算字符串长度(为清楚起见):

int stringLength(char string[])
{
    int i = 0;
    while (string[i] != '\0') {
        i++;
}
    return i;
}
Run Code Online (Sandbox Code Playgroud)

它根据空终止字符计算字符串的长度.那么,使用以下情况,'\ 0'字符的作用是什么(如果有的话)?

情况1:

char * stack1 = "stack"; 
printf("WORD %s\n", stack1);
printf("Length %d\n", stringLength(stack1));
Run Code Online (Sandbox Code Playgroud)

打印:

WORD stack
Length 5
Run Code Online (Sandbox Code Playgroud)

案例2:

char stack2[5] = "stack";
printf("WORD %s\n", stack2);
printf("Length %d\n", stringLength(stack2));
Run Code Online (Sandbox Code Playgroud)

打印:

WORD stack???
Length 8
Run Code Online (Sandbox Code Playgroud)

(这些结果每次都有所不同,但永远不正确).

案例3:

char stack3[6] = "stack";
printf("WORD %s\n", stack3);
printf("Length %d\n", stringLength(stack3));
Run Code Online (Sandbox Code Playgroud)

打印:

WORD stack
Length 5
Run Code Online (Sandbox Code Playgroud)

案例4:

char stack4[6] = "stack";
stack4[5] = '\0'; …
Run Code Online (Sandbox Code Playgroud)

c string c-strings null-terminated

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

不应该直接指向C中的文本吗?

我正在学习C,我遇到了指针.尽管我在本教程中学到的东西比从教科书中学到的东西还要多,但我仍然对这些教程有所了解.

如果我编程这个

#include <stdio.h>

int main()
{
    char *ptr_str;

    ptr_str = "Hello World";

    printf(ptr_str);

    return 0;

}
Run Code Online (Sandbox Code Playgroud)

结果是

Hello World
Run Code Online (Sandbox Code Playgroud)

我不明白编译时是怎么回事,因为指针ptr_str直接指向文本而不指向文本的第一个字符.我以为只有这样才行

#include <stdio.h>

int main()
{
    char *ptr_str;
    char var_str[] = "Hello World";

    ptr_str = var_str;

    printf(ptr_str);

    return 0;

}
Run Code Online (Sandbox Code Playgroud)

所以在第一个例子中我是如何直接指向文本的?

c pointers c-strings

0
推荐指数
1
解决办法
79
查看次数

动态分配的C风格字符串比给定长度有更多的字符?

我正在使用动态C风格的字符串来读取文件中的数据,但出于某种原因,当我使用给定的长度动态分配C风格的字符串时,它会出现四个可以使用的额外字符strlen().这些空白空间中的垃圾被添加到读入字符串的末尾并显示在上面cout.究竟是什么造成了这种情况,我该如何解决?

C样式字符串在代码的开头声明,并在此之前使用一次.在此之前使用的时间也太大,但在这种情况下,它不会在末尾添加额外的信息.使用后,它将被删除,直到此时才会再次使用.我很困惑,因为我没有发生过这种情况或以前遇到过问题.

// Length read as 14, which is correct
iFile.read(reinterpret_cast<char *>(&length), sizeof(int)); 

tempCstring = new char[length]; // Length still 14
cout << strlen(tempCstring); // Console output: 18

// In tempCstring: Powerful Blockýýýý
iFile.read(reinterpret_cast<char *>(tempCstring), length);

// Custom String class takes in value Powerful Blockýýýý and is 
// initialized to that
tempString = String(tempCstring);

// Temp character value takes in messed up string
temp.setSpecial(tempString); 
delete[] tempCstring; // Temp cString is deleted for next use
Run Code Online (Sandbox Code Playgroud)

写入文件时:

// …
Run Code Online (Sandbox Code Playgroud)

c++ arrays file c-strings

0
推荐指数
1
解决办法
209
查看次数