#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<math.h> int main() { int n=0; scanf("%d",&n); int i = 0; int j = 0; int arr[1000][1000] = { {0} }; for (i; i < n; i++) { for (j; j < n; j++) { arr[i][j] = i + j; } } for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { printf("%d", arr[i][j]); } } return 0; }看看这串代码错在哪里
时间: 2024-01-12 14:05:45 浏览: 31
在第二个循环中,变量 `j` 的初始值应该是0,而不是从上一个循环中的值继续增加。修改后的代码如下:
```c
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <math.h>
int main() {
int n = 0;
scanf("%d", &n);
int arr[1000][1000] = { {0} };
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = i + j;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}
```
修改后的代码中,我将两个循环的变量 `i` 和 `j` 的定义放在了循环语句内部,并且在第二个循环中将变量 `j` 的初始值设为0,这样程序就可以正确地输出二维数组了。
相关问题
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<windows.h> #include<string.h> #include<stdlib.h> #include<math.h> #include<malloc.h> typedef struct character_name { char name[100]; struct character_name* next; }char_name; typedef struct character_title { char title[100]; struct character_title* next; }char_title; typedef struct character_identity { char identity[100]; struct character_identity* next; }char_iden; typedef struct character_attribute { int strength; struct character_attribute* next; }char_att; typedef struct character_information { char_name* _name; char_title* _title; char_iden* _iden; char_att* _att; struct character_information* next; }char_inf; void initialization(char_inf*node) { node = (char_inf*)malloc(sizeof(char_inf)); node->_name = (char_name*)malloc(sizeof(char_name)); node->_title = (char_title*)malloc(sizeof(char_title)); node->_iden = (char_iden*)malloc(sizeof(char_iden)); node->_att = (char_att*)malloc(sizeof(char_att)); } char_inf* ceshi; int main() { initialization(ceshi); strcpy(ceshi->_name->name, "ceshi_1"); strcpy(ceshi->_title->title, "ceshi_2"); strcpy(ceshi->_iden->identity, "ceshi_3"); ceshi->_att->strength = 4; printf("%s\n%s\n%s\n%d\n", ceshi->_name->name, ceshi->_title->title, ceshi->_iden->identity, ceshi->_att->strength); return 0; } 为什么报错访问权限冲突
在你的代码中,报错访问权限冲突是因为你在初始化函数`initialization`中使用了错误的方式来分配内存。具体来说,你传递了一个指向`char_inf`结构体指针的指针(`char_inf* node`),然后在函数内部尝试为它分配内存。
然而,这样的分配方式是错误的,因为当你在函数内部为`node`分配内存时,它只是一个局部变量,对外部的`ceshi`指针没有影响。因此,在`main`函数中访问`ceshi`时,它仍然是一个未初始化的指针,导致访问权限冲突。
为了解决这个问题,你可以将`initialization`函数修改为返回分配好的内存块的指针,然后将其赋值给`ceshi`指针。具体代码如下所示:
```c
char_inf* initialization()
{
char_inf* node = (char_inf*)malloc(sizeof(char_inf));
node->_name = (char_name*)malloc(sizeof(char_name));
node->_title = (char_title*)malloc(sizeof(char_title));
node->_iden = (char_iden*)malloc(sizeof(char_iden));
node->_att = (char_att*)malloc(sizeof(char_att));
return node;
}
```
然后在`main`函数中调用该函数,并将返回的指针赋值给`ceshi`,如下所示:
```c
ceshi = initialization();
```
这样,你就可以正确地分配内存并访问`ceshi`结构体的成员了,不再会出现访问权限冲突的错误。
严重性 代码 说明 项目 文件 行 禁止显示状态 错误 C4996 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. Project1 C:\Users\wangy\source\repos\Project1\Project1\源.cpp 16
这是一个警告,提示使用scanf_s函数代替scanf函数,因为scanf函数可能存在安全问题。可以按照提示使用scanf_s函数来避免这个警告。修改后的代码如下:
```
#include <stdio.h>
#include <math.h>
void DigitalComputation(int n) {
int i;
double S;
S = 0;
for (i = 1; i <= n; i++) {
S = S + pow(-1, i + 1) / (i * (i + 1));
}
printf("%lf\n", S);
}
int main() {
int n;
printf("请输入n的值:");
scanf_s("%d", &n);
DigitalComputation(n);
return 0;
}
```
另外,如果不想使用scanf_s函数,可以在文件开头加入以下代码来禁止警告:
```
#define _CRT_SECURE_NO_WARNINGS
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-gzip](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)