标签: char-pointer

Strcmp的行为与预期不符,比较两个不相等的字符串时返回0

我正在理解strcmp函数的奇怪行为,将通过以下代码进行说明:

#include <iostream> 
#include <cstring>

using namespace std;

int main()
{
    char *p = "no";

    cout << p << endl;                      //Output: no
    cout << &p << endl;                     //Output: 0x28ac64
    cout << strlen(p) << endl;              //Output: 2
    cout << strcmp(p, "no") << endl;        //Output: 0

    cin >> p;                               //Input: bo

    cout << p << endl;                      //Output: bo
    cout << &p << endl;                     //Output: 0x28ac64
    cout << strlen(p) << endl;              //Output: 2

    cout << strcmp(p, "no") << endl;        //Output: 0 …
Run Code Online (Sandbox Code Playgroud)

c++ cstring strcmp char-pointer

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

为什么我的程序在使用堆分配的内存时会抛出分段错误?

在编写一个程序来反转一个字符串后,我无法理解为什么在尝试反转字符串时出现了seg故障.我在下面列出了我的程序.

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

void reverse(char *);

int main() {
  char *str = calloc(1,'\0');
  strcpy(str,"mystring0123456789");
  reverse(str);
  printf("Reverse String is: %s\n",str);
  return 0;
}

void reverse(char *string) {
  char ch, *start, *end;
  int c=0;
  int length = strlen(string);
  start = string;
  end = string;

  while (c < length-1){
    end++;
    c++;
  }
  c=0;

  while(c < length/2){
    ch = *end;
    *end = *start;
    *start = ch;
    start++;
    end--;
    c++;
  }
}
Run Code Online (Sandbox Code Playgroud)

第一个问题:

即使我只将1个字节的内存分配给char指针str(calloc(1,'\0')),并且我将18个字节的字符串复制mystring0123456789到其中,并且它没有抛出任何错误,并且程序在没有任何SEGFAULT的情况下工作正常.

为什么我的程序没有抛出错误?理想情况下它应该抛出一些错误,因为它没有任何内存来存储那个大字符串.有人可以对此有所了解吗?

该程序运行完美,并给我输出Reverse …

c memory-management char-pointer

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

C++ tolower/toupper char指针

你们知道为什么下面的代码在运行时崩溃了吗?

char* word;
word = new char[20];
word = "HeLlo"; 
for (auto it = word; it != NULL; it++){        
    *it = (char) tolower(*it);
Run Code Online (Sandbox Code Playgroud)

我正在尝试小写char*(字符串).我正在使用visual studio.

谢谢

c++ char-pointer toupper tolower

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

c/c ++中指向char数组的指针

在过去,我一直在使用Visual Studio 2010/2013/2015,这种语法是可行的:

char* szString = "This works!";
Run Code Online (Sandbox Code Playgroud)

我决定继续并改变我对Linux的编码生活方式,因为我安装了g ++并将SlickEdit作为我的IDE.

似乎这句话不再适用了.任何人都可以说明原因吗?

但这有效:

char strString[] = "This works!";
Run Code Online (Sandbox Code Playgroud)

这个错误与c ++ 11有关.

有谁知道为什么会这样?不是如何修复它,导致在我的工作区中没有任何方法来安装c ++ 11编译器,我只是好奇它是否与后台有关编译器工作方式的内容有关.我对第一行代码的了解是,它在堆栈上创建一个常量变量,并创建一个新的指针设置自己朝向ESP的值,但在第二行,它计算常量变量上的字母数量然后最后设置一个null终止符作为结果.

哦,还有一件事 - >第一个在GCC/GPP中设置的方式似乎也有区别,因为第一个类型是{char*&},第二个类型是{char(*)[12]对此也有任何解释?谢谢!

c++ char-pointer

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

Caesar Cipher使用char*?C++

所以我想尝试使用Ceasar Cipher char *,我写了一个简单的函数:

char * Encrypt(char * s, int k)
{
    char * c = s;

    for(int i = 0; i < strlen(s); i++)
       c[i] += k;

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

这似乎看起来应该有效,但事实并非如此.它在运行程序时抛出错误.

以下是我如何调用此函数的示例:

int main()
{
    cout << Encrypt("hello", 2) << endl;
    system("pause");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在你说"为什么不只是使用string?"之前,答案是我在某个SDK上编写C++,在使用时会导致编译器错误string.好的但是,任何形式的帮助都将非常感谢,谢谢!

c++ encryption char-pointer caesar-cipher

-3
推荐指数
1
解决办法
464
查看次数

为什么它会给出错误"分段错误"?

我想在不同的内存位置输入两个字符串,但在第一次输入后,它显示错误"分段错误(核心转储").我没有弄清楚这段代码有什么问题.

#include <stdio.h>
#include<iostream>
using namespace std;

int main()
{
    char *str;
    int i;
    scanf("%s",str+0);
    scanf("%s",str+1);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是,当我只接受一个输入时它工作正常.

#include <stdio.h>
#include<iostream>
using namespace std;

int main()
{
    char *str;
    int i;
    scanf("%s",str+0);

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

为什么?

c segmentation-fault char-pointer

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