M K Q 2023-09-28 14:07 采纳率: 44.4%
浏览 3

c语言简简单单的问题

下面虚线内的程序有几处错误

#include<stdio.h>
int main() {
int m = 10;
const int n = 20;
/*---------------------------------------------------------------------------*/
const int *ptr1 = &m;
int * const ptr2 = &m;
*ptr1 = 3;
ptr2 = &n;
int *ptr3 = &n;
const int * ptr4 = &n;
int * const ptr5;
ptr5 = &m;
const int * const ptr6 = &m;
*ptr6 = 5;
ptr6 = &n;
const int * ptr7;
ptr7 = &m;
/*---------------------------------------------------------------------------*/
return 0

求详细解释,刚刚学c。

  • 写回答

2条回答 默认 最新

  • threenewbee 2023-09-28 14:10
    关注

    看编译器提示即可:

    编译错误:main.c: In function ‘main’:
    main.c:8:7: error: assignment of read-only location ‘*ptr1’
     *ptr1 = 3;
           ^
    main.c:9:6: error: assignment of read-only variable ‘ptr2’
     ptr2 = &n;
          ^
    main.c:10:13: warning: initialization discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
     int *ptr3 = &n;
                 ^
    main.c:13:6: error: assignment of read-only variable ‘ptr5’
     ptr5 = &m;
          ^
    main.c:15:7: error: assignment of read-only location ‘*ptr6’
     *ptr6 = 5;
           ^
    main.c:16:6: error: assignment of read-only variable ‘ptr6’
     ptr6 = &n;
          ^
    main.c:20:1: error: expected ‘;’ at end of input
     return 0
     ^~~~~~
    main.c:20:1: error: expected declaration or statement at end of input
    
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 9月28日