标签: c-strings

c风格的字符串是否安全?

在c/c ++中,有些人使用c风格的字符串,如:

char *str = "This is a c-styled string";
Run Code Online (Sandbox Code Playgroud)

我的问题是这样安全吗?我看到它的方式是他们创建了一个char指针,指向const数组的字符的第一个字母,但不能其他一些东西,例如另一个变量覆盖内存中char数组的一部分?因此导致str在逻辑上无效?

c c++ c-strings

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

c程序,通过字符串递增

我发现这个程序通过字符串运行并打印出来.我知道有一个错误,但我不能100%确定它是什么.

char *stringOptions[] = {"one", "two", "three"};

void incrementString(char *input) 
{
    static int i = 0;
    input = stringOptions[i % 3];
    i = (i + 1) % 3;
}

void print_string(void) 
{
    char *string = "initial";
    int i;

    for(i = 0; i < 3; ++i) 
    {
        incrementString(string);
        printf("%s ", string);
    }
}
Run Code Online (Sandbox Code Playgroud)

应打印出来:一两三

对不起,我不太了解它,它是基于我想要做的事情,但我读完字符串是不成功的.至关重要的是,这应该是这样的单独功能.谢谢,

c string c-strings increment

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

为什么argv不起作用?

我是cpp的初学者,并且遇到以下代码的问题.通常它应该根据argv [1]显示文本,但它不显示任何内容.

我究竟做错了什么?

#include <stdio.h>

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

  if(argv[1] == "a" && argc > 1)  printf("hello world, argc: %d\n", argc);
  else if(argv[1] == "2" && argc > 1) printf("hello everyone, argc: %d\n", argc);

  for(int i=0; i<argc; i++) printf("%s\n", argv[i]);

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

c c++ compare c-strings

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

如何在C中的字符串前面附加一个字符串

我在C中有一个字符串:"电话/错误.即信用卡pmnt".它的长度是30个字符.我想在这个"R*"前面添加一个字符串.所以我的最终输出为"R*Phone/mis.即Credit Card p".字符串在结尾处截断,因为其最大长度为30个字符.我试过下面的代码,其中iov_pPymArr-> pymInfoArray [i] .type_desc变量包含字符串"Phone/mis.即Credit Card pmnt":

sprintf (iov_pPymArr->pymInfoArray[i].type_desc,"R*%s",iov_pPymArr->pymInfoArray[i].type_desc);

But it gives me output as : "R*R*e/mis. ie Credit Card pmnt". 
Run Code Online (Sandbox Code Playgroud)

R*被追加两次,字符串从开头而不是结尾被截断.请告知可能的解决方案.

c string printf c-strings

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

分配c-string的元素

我正在尝试对c-string进行冒泡排序 - 请参阅下面的函数.目的是根据ASCII码对字符进行排序.

编译器在运行时不会抱怨,此时我会遇到分段错误.我运行了gdb,程序运行正常,直到第12行.在第12行,gdb调试器声明'Process gdb-inferior killed'.

为什么第12行不正确?我在程序的其他地方遇到过这个问题 - 无法将字符分配给c字符串的元素.我知道strcpy()需要字符串赋值,但是对于特定的元素,我认为它没问题.任何帮助很多人赞赏.

    1. void bubblesort(char *str)
    2. {
    3.   int length = strlen(str);
    4.
    5.   for (int i = length - 1; i > 0; i--)
    6.     {
    7.       for (int j = 0; j < i; j++)
    8.         {
    9.           if (str[j] > str[j + 1])
    10.            {
    11.              char temp = str[j + 1];
    12.              str[j + 1] = str[j];
    13.              str[j] = temp;
    14.            }
    15.         }
    16.     }
    17. …
Run Code Online (Sandbox Code Playgroud)

c element c-strings variable-assignment bubble-sort

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

C:如何在不使用malloc的情况下编辑函数中的C字符串

我有一个stringin main,我想将该字符串的地址传递给一个将编辑该字符串内容的函数.我不想使用malloc.

我的代码目前看起来像这样:

int main(){
        char* str = "Hello";
        char* para1[] = {"Tea", "Coffee"};

        printf("str: %s\n para1: %s\n", str, para1[0]);

        my_func(str, &para1);

        printf("str: %s\n para1: %s\n", str, para1[0]);

        return 1;
}

int my_func(char* str, char** para1){
        printf("str: %s\n", str);
        str[0] = 'W';
        para1[0] = "Popcorn";

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

我不明白如何将字符串的第一个字母更改HW

你能解释一下我做错了什么吗?

c string pointers function c-strings

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

这种访问C-String中字符的方式如何工作?

在最近的考试中询问了这个问题,候选人必须找到以下代码的输出:

#include<stdio.h>
#include<string.h>
int main()
{
    char * c = "GATECSIT2017";
    char *p = c;
    printf("%d", (int)strlen(c+2[p]-6[p]-1));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我开始用C++编写代码,所以我不是C-Strings的专家.在搞乱代码之后,我理解它是如何工作的,但有一点我不清楚,这是从char*字符串访问char的方式.

我的问题是,如何2[p]获得索引2处的角色?它会解决*(p+2)或是否有其他事情发生?此外,是否有任何文档或任何文章可以解释这种行为?

c pointers c-strings char

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

C++:在被本地字符串捕获后释放或销毁malloc'd char*?

假设我执行以下操作:

char *get_data(...) {
    char *c_style = (char *) malloc(length * sizeof(char));
    load_c_string_with_my_c_function(c_style, length, input);
    return c_style;
}

int main() {
   std::string data(get_data(...));
   // free(data.c_str()); ?? -- are the malloc'd bytes now managed?
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

有没有办法释放get_data()分配的内存?评论free(data.c_str());工作会吗?

c++ memory-management c-strings

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

为什么我的阵列没有正确清空?

所以我将这个主数组保存在一个时间数组中。然后,我使用'\ 0'来“删除”其内部的主数组。在这一点上,如果我打印主数组,它将只打印空白。但是,如果那样的话,我使用for喜欢主数组=时间数组。x = 0 x <4 x ++时,它确实从另一个数组复制了四件事,但同时也打印了我执行“ \ 0”之前的内容

香港专业教育学院尝试了一段时间的柜台,但也没有工作。香港专业教育学院以前使用\ 0,它的工作idk为什么现在不工作

for(int y = 0; y <= strlen(numeros); y++){
    numeros[y] = '\0';
}

printf("%s\n", numeros);

for(int z = 0; z <= 4; z++){
    numeros[z] = numerosTemp[z];
}
    printf("%s\n", numeros);
Run Code Online (Sandbox Code Playgroud)

我的预期结果只是numerosTemp数组的前四个字母,但是它包含了我在\ 0之前曾经拥有的所有内容。但是在\ 0之后的printf中,它只会打印空白,所以我不明白为什么会这样

c arrays c-strings

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

Weird qsort behavior, what's wrong?

Why doesn't the code below sort the month names? Base address is correct, number of elements is correct, size of each element is correct, cmp function is correct. gcc -W -Wall -ansi -pedantic doesn't complain a peep.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int by_name(const void *a, const void *b) {
    fprintf(stderr, "%p %p\n", a, b);
    return strcmp((const char *)a, (const char *)b);
}
int main(void) {
    char *months[] = {
        "January",
        "Februar",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September", …
Run Code Online (Sandbox Code Playgroud)

c sorting c-strings qsort string-literals

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