相关疑难解决方法(0)

int main()函数必须在所有编译器中返回一个值吗?

为什么在C++的某些编译器中使用int main()时不必包含return语句?Turbo C++怎么样?

c++ turbo-c++

20
推荐指数
3
解决办法
1万
查看次数

为什么我可以通过指针转换而不是C中的全局变量来更改本地const变量?

我想通过使用指针来改变常量的值.

请考虑以下代码

int main()
{
    const int const_val = 10;
    int *ptr_to_const = &const_val;

    printf("Value of constant is %d",const_val);
    *ptr_to_const = 20;
    printf("Value of constant is %d",const_val);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

正如预期的那样,常量的值被修改.

但是当我尝试使用全局常量的相同代码时,我遇到了运行时错误.Windows崩溃记者正在开放.在此语句"*ptr_to_const = 20;"中打印第一个printf语句后,可执行文件暂停

请考虑以下代码

const int const_val = 10;
int main()
{
    int *ptr_to_const = &const_val;
    printf("Value of constant is %d",const_val);
    *ptr_to_const = 20;
    printf("Value of constant is %d",const_val);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

该程序使用codeblocks IDE在mingw环境中编译.

谁能解释一下发生了什么?

c pointers constants

4
推荐指数
2
解决办法
2087
查看次数

Const变量在C中用指针改变

该变量i被声明为const但我仍然可以使用指向它的内存位置的指针来更改该值.这怎么可能?

int main()
{

    const int i = 11;
    int *ip = &i;
    *ip=100;
    printf("%d\n",*ip);
    printf("%d\n",i);
}
Run Code Online (Sandbox Code Playgroud)

当我编译时,我得到这个警告:

test.c: In function ‘main’:
test.c:11: warning: initialization discards qualifiers from pointer target type
Run Code Online (Sandbox Code Playgroud)

输出就是这个

100
100
Run Code Online (Sandbox Code Playgroud)

c c++ pointers const

4
推荐指数
2
解决办法
1298
查看次数

const&指的是非易失性变量.变量发生了变化.更改是否使const&?无效?

在C++中,可以const &改变的价值吗?

好吧,当然不能改变,可以吗?这const意味着什么.而且,听听Stroustrup:

const左值参考指的是恒定的,这是从视角的参照的用户的点不可变的.

但是这个怎么样?

#include <iostream>

int main() {
    int           a = 0;
    const int&    r = a;
    const int old_r = r;
    ++a;
    const int new_r = r;
    std::cout
      <<      "old_r == " << old_r
      << " but new_r == " << new_r << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在我的机器上,这输出,old_r == 0 but new_r == 1.

这是我真正的问题.在上面的代码中,查看该行

    const int new_r = r;
Run Code Online (Sandbox Code Playgroud)

只要

  • 地址&new_r既不在此行也不在代码的其他地方提取
  • 代码什么都没有volatile,

没有任何东西阻止优化编译器从合并old_r …

c++ const reference undefined-behavior const-reference

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

为什么我得到EXC_BAD_ACCESS?

我想反转一个字符串,但我在swap char上出错了.

有人可以帮我吗?

char*  reverse_char(char* src){
    char *p,*q;
    p = q = src;
    while(*(p) != '\0'){
        p++;
    }
    p--;

    char  temp = '\0';
    while (p > q) {
        temp = *p;
        *p = *q; // I got exec bad access here ??? why 
        *q = temp;
        p--;
        q++;
    }
    return src;
}
Run Code Online (Sandbox Code Playgroud)

这是主要方法.

int main(int argc, char const* argv[])
{
    char *hi = "hello world!\n";
    printf("%s", hi);

    reverse_char(hi);
    printf("%s", hi);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c exc-bad-access

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

为什么下面的程序没有报错?

#include<stdio.h>
void main ()
{
     int a=4;
     const int *p=&a; 
     *p--;      
}
Run Code Online (Sandbox Code Playgroud)

在上面的行中,这意味着我们不能通过 p 更改值 a,因此在减量语句中它应该给出错误,但它没有给出错误。谁能解释为什么?

c constants

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