标签: c-strings

检查 std::string.c_str() 的返回值

在 C++ Primer 第五版中,它说:

c_str 返回的数组不保证无限期有效。

于是我做了一个测试:

//  c_str exploration
std::string strTest = "This is a test";
const char* s1 = strTest.c_str();
strTest = "This is b test";
std::cout << s1 << std::endl;
Run Code Online (Sandbox Code Playgroud)

由于 s1 是一个指针,因此它肯定会显示新值。但是,当我将值更改为不同长度的字符串时,它通常会显示一些垃圾:

//  c_str exploration
std::string strTest = "This is a test";
const char* s1 = strTest.c_str();
strTest = "This is b testsssssssssssssssssssssssssss";
std::cout << s1 << std::endl;
Run Code Online (Sandbox Code Playgroud)

我认为这是因为返回的 C String 已经固定了结束空字符的位置,所以当长度改变时,它会使所有内容无效。令我惊讶的是,有时即使我将字符串更改为新长度,它仍然有效:

//  c_str exploration
std::string strTest = "This is a test";
const char* s1 = …
Run Code Online (Sandbox Code Playgroud)

c++ c-strings

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

c 中函数调用中的参数太多?

我正在尝试创建一个颜色菜单,但最后一行报告了一些错误。

puts("I don't know about the color %c",input);
Run Code Online (Sandbox Code Playgroud)

这是声明

char input[7];
Run Code Online (Sandbox Code Playgroud)

和初始化

scanf("%c",&input);
Run Code Online (Sandbox Code Playgroud)

错误在这里

Too many arguments in function call
error: expected declaration specifiers or '...' before '&' token
scanf("%c",&input);
        ^
error: expected declaration specifiers or '...' before string constant
scanf("%c",&input);
   ^~~~
Run Code Online (Sandbox Code Playgroud)

为什么?

c printf scanf c-strings char

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

C 字符串指针。int 指针 vs char 指针

我想知道为什么 * p+1输出 k 而不是 "o"?我试过用调试器运行它,但似乎仍然无法解决这个问题。

    char name[] = "john q public";
    int *p = name;
    printf("%c\n", *p+1); // output expected -> o, but got k
    printf("%c", name[0]); // output as expected -> j
Run Code Online (Sandbox Code Playgroud)

当我尝试在 for 循环中输出名称时,我得到了奇怪的值。

    int  len = strlen(name);
    for(int i=0; i<len; i++ ){
        printf("%c", *p++); //expected -> john q public, but got -> j ucem n 1 
    }
Run Code Online (Sandbox Code Playgroud)

c pointers c-strings

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

printf 在 C 中使用是否不安全

我是 C 的新手,所以我开始玩一些代码。我的代码中有一个错误,因为我认为使用使用printf(pass)不安全,因为它可以覆盖该值,因此对用户不安全。我想知道printf(pass)我的代码不安全是否正确?另外,我怎样才能让用户在finally logged in不更改代码的情况下打印消息。有没有办法做到这一点?

我的代码:

#include <stdio.h>

char pass[100];

char getPass() {
  int value = 'G';
  int * j = & value;
  fgets(pass, sizeof(pass), stdin);
  printf("your entered pass is ");
  printf(pass);
  return (char)( * j);
}

void main() {
  printf("enter the pass here ");
  if (getPass() == 'K') {
    printf("finally logged in\n");
    exit(0);
  } else {
    printf("Wrong password\n");
    exit(1);
  }
}
Run Code Online (Sandbox Code Playgroud)

c printf c-strings fgets

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

超过 15 个字符的字符字符串出错

为什么 gcc 不允许我使用大于 15 的字符维数?

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

int main()
{
    char alphabet[]={'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
    int dim_alph;
    dim_alph=strlen(alphabet);
    printf("%s has %d characters\n", alphabet, dim_alph);
    return 0;
}`
Run Code Online (Sandbox Code Playgroud)

它在字符串和维度上都给了我无效的输出(如果我停在“P”字符而不是“O”字符上)

c printf c-strings char undefined-behavior

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

检查给定字符串是否为有效 IP 地址的建议/帮助/更好的解决方案

老实说,我认为我编写的代码是垃圾,而且我不认为这是解决问题的最佳方法。我需要一些建议或改进来解决这个问题。我对编码还是新手。如果您能提供一些有关如何使用字符串和各种字符串函数的提示,我将不胜感激。

该字符串成为 IP 地址的条件:-

连接到互联网的设备的标识号。以点分四组表示法书写的 IPv4 地址由四个用句点分隔的 8 位整数组成。

换句话说,它是一个由四个数字组成的字符串,每个数字都在 0 到 255 之间(含 0 和 255),并带有“.”。每个数字之间的字符。所有数字都应不带前导零。

例子:

  1. 192.168.0.1 是有效的 IPv4 地址
  2. 255.255.255.255 是有效的 IPv4 地址
  3. 280.100.92.101 不是有效的 IPv4 地址,因为 280 太大,无法成为 8 位整数(最大的 8 位整数为 255)
  4. 255.100.81.160.172 不是有效的 IPv4 地址,因为它包含 5 个整数,而不是 4 个
  5. 1..0.1 不是有效的 IPv4 地址,因为格式不正确
  6. 17.233.00.131 和 17.233.01.131 不是有效的 IPv4 地址,因为它们包含前导零

这是我的代码(我知道它是垃圾并且没有任何意义):-

bool isIPv4Address(char * inputString) {
    int count = 0, period = 0;
    int k = 0, i = 0;
    int digit = …
Run Code Online (Sandbox Code Playgroud)

c function c-strings

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

string_view 与 const char* 性能

一个std::string_view参数比const char*下面代码中的一个参数更好吗?

void func( const std::string_view str )
{
    std::istringstream iss( str.data( ) ); // str is passed to the ctor of istringstream


    std::size_t pos { };
    int num { std::stoi( str.data( ), &pos, 10 ) }; // and here it's passed to std::stoi
}

int main()
{
    std::array<char, 20> buffer { };
    std::cin.getline( buffer.data( ), 20 );
    func( buffer.data( ) );
}
Run Code Online (Sandbox Code Playgroud)

std::istringstreamctor 和都std::stoi需要 aconst std::string&作为参数。但我std::string_view使用其data()成员函数向他们传递一个对象。这是不好的做法吗?我应该回到 …

c++ c-strings stdstring micro-optimization string-view

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

查找C程序中的漏洞

在准备软件安全考试时遇到了这个问题,但找不到漏洞。如果您输入姓名“John Doe”和工资 4000,则程序将写入字符串“John Doe: $4000”。据我所知,程序占 \0,没有格式字符串错误,没有缓冲区溢出。不知道我错过了什么。

编辑:忘记添加“名称”参数可以假定为有效的空终止字符串。

/* Calculates the number of letters (i.e. digits) that are needed to represent a decimal number as an ASCII string */
size_t count_digits(unsigned int number)
{
    unsigned int left = number;
    size_t n= 0;
    while(left != 0) {
        left = left / 10;
        n++;
    }
    return n;
}

void add_record(const char* name, unsigned int salary)
{
    char buffer[256];

    size_t len = strlen(name);
    size_t num_digits = count_digits(salary);

    /* 5 extra bytes required for colon …
Run Code Online (Sandbox Code Playgroud)

c security c-strings

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

如何更改 char *name = "exemple"; 中的第二个字母 与 char *name

我正在学习 C++ 以更好地理解内存。

char *name = "example"; 

*(&name+1) = (char*)'A';

std::cout << name << std::endl;
Run Code Online (Sandbox Code Playgroud)

返回示例而不是eAample

我的想法是获取“x”的内存地址并更改它。

不是 const 所以我应该能够改变地址的值对吗?

obs:我知道用其他方法怎么做,我很好奇用这种方法。

c++ pointers c-strings char string-literals

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

\0 不打印它旁边的数字

我在网上发现了这个技巧/问题,想了解发生了什么。

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

int main(){
    char s[] = "S\0C5AB";
    printf("%s", s);
    printf("%d", sizeof(s)); // 7
    printf("  -- %d", strlen(s)); // 1
}
Run Code Online (Sandbox Code Playgroud)

一切都按预期进行。

但是当在后面放一个数字时\0 sizeofstrlen两者都会忽略\0它旁边的数字。

int main(){
    char s[] = "S\065AB";
    printf("%s   ", s); // S5AB
    printf("--- %d", sizeof(s)); // 5
    printf("  -- %d", strlen(s)); // 4
}
Run Code Online (Sandbox Code Playgroud)

上述代码链接: https: //godbolt.org/z/7qfYq51E4

这里发生了什么事?

c escaping c-strings

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