相关疑难解决方法(0)

为什么编译器在尝试修改 char * 字符串文字时不检测并产生错误?

假设有以下两段代码:

char *c = "hello world";
c[1] = 'y';
Run Code Online (Sandbox Code Playgroud)

上面那个不行。

char c[] = "hello world";
c[1] = 'y';
Run Code Online (Sandbox Code Playgroud)

这个可以。

关于第一个,我知道字符串“hello world”可能存储在只读内存部分,因此无法更改。然而,第二个在堆栈上创建一个字符数组,因此可以修改。

我的问题是 - 为什么编译器不检测到第一种类型的错误?为什么这不是 C 标准的一部分?这有什么特殊原因吗?

c standards c99 c11

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

char *array 和 char array[] 之间的内存差异是什么?

当我执行下一个代码时

int main()
{
    char tmp[] = "hello";
    printf("%lp, %lp\n", tmp, &tmp);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我得到了相同的地址。但对于下一个代码,它们会有所不同

int main()
{
    char *tmp = "hello";
    printf("%lp, %lp\n", tmp, &tmp);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

您能解释一下这些示例之间的内存差异吗?

c memory arrays pointers

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

修改C中函数内的char数组

所以我最近一直在玩C,并且一直试图理解通过值/引用传递的复杂性以及在函数内操作传入变量的能力.然而,我遇到了以下问题:

void modifyCharArray(char *input)
{
    //change input[0] to 'D'
    input[0] = 'D';
}

int main()
{
    char *test = "Bad";
    modifyCharArray(test);
    printf("Bad --> %s\n", test);
}
Run Code Online (Sandbox Code Playgroud)

所以想法只是修改函数内的char数组,然后在修改完成后打印出所有数组.但是,这会失败,因为我所做的只是修改input传入的值,而不是实际的内存地址.

简而言之,有没有什么方法可以让我char *input进入一个函数并修改它的原始内存地址而不使用像memcpystring.h 这样的东西?

c arrays

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

C&C++ String Literal对char*="stringLiteral"的弃用;

我理解语法char*="stringLiteral"; 已被弃用,未来甚至可能无法使用.我不明白的是为什么.

我搜索网和堆栈,虽然有许多回声确认char*="stringLiteral"; 是错的,那个const char*="stringLiteral"; 是核心,我还没有找到有关WHY表示语法错误的信息.换句话说,我想知道问题究竟是什么.

我的困惑

CODE SEGMENT 1 - EVIL WAY(已弃用)

char* szA = "stringLiteralA";     //Works fine as expected. Auto null terminated.
std::cout << szA << std::endl;    
szA = "stringLiteralB";          //Works, so change by something same length OK.
std::cout << szA << std::endl;    
szA = "stringLiteralC_blahblah"; //Works, so change by something longer OK also.
std::cout << szA << std::endl;    

Ouput:
stringLiteralA
stringLiteralB
stringLiteralC_blahblah
Run Code Online (Sandbox Code Playgroud)

那究竟是什么问题呢?似乎工作得很好.

CODE SEGMENT 2("OK"方式)

const char* szA = "stringLiteralA";  //Works fine …
Run Code Online (Sandbox Code Playgroud)

c c++

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

访问冲突,memset

以下代码在运行时在第L2行引发访问冲突,这发生在第二次调用setword期间.

问:我在L2中哪里出错了,为什么第一行的第一个memset没有问题?

注意:我试图将问题区域与更大的代码隔离开来,希望这能提供足够的信息.

void setword( char ** word )
{
    if ( *word == NULL )
    {
       *word = (char *)malloc(30);
        memset( *word, '\0', 30 ); //L1: OK 
    }
    else
    {   
        memset( *word, '\0', 30 );//L2: Access violation
    }

    *word = "Hello";
    //*word shall be freed when operations are complete.
}
int main()
{
    char * word = NULL;

    setword( &word );  //Call 1: OK
    printf( "%s\n", word );

    setword( &word );  //Call 2: NOK!
    printf( "%s\n", word );
}  
Run Code Online (Sandbox Code Playgroud)

c access-violation

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

为什么第二次打印没有显示任何内容

我是 C 初学者,有以下代码。

#include <stdio.h>
#include<string.h>
int main(void){
    char *s1="abcd";
    char s2[30]="abcd";
    if(strcmp(s1,s2)){
        printf("sirurile sunt identice\n");
    }else printf("sirurile sunt diferite\n");
    strcpy(s2,s1);
    printf("%s\n",s1);
    strcpy(s1,s2);
    printf("%s\n",s2);
    return 0; 
}
Run Code Online (Sandbox Code Playgroud)

我试图理解为什么输出不考虑第二次打印并仅显示第一个“abcd”。输出为: Sirurile sunt differite abcd

我预计 abcd 会打印两次,因为字符串似乎被复制到另一个上

c

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

标签 统计

c ×6

arrays ×2

access-violation ×1

c++ ×1

c11 ×1

c99 ×1

memory ×1

pointers ×1

standards ×1