为什么编译器没有显示*标记被用作标记[I]的错误? [英] Why doesn't the compiler show an error for *marks being used as marks[I]?
问题描述
#include< stdio.h>
int main()
{
int * marks;
printf(输入四个标记:);
for(int i = 0; i< 4; i ++)
scanf(%d,& marks [i]);
printf(你输入的标记是:);
for(int i = 0; i< 4; i ++)
printf(%d,marks [i]);
返回0;
}
我尝试了什么:
程序没有给出任何输出也没有显示任何错误
#include <stdio.h>
int main()
{
int *marks;
printf("Enter four marks:");
for(int i = 0;i<4;i++)
scanf("%d",&marks[i]);
printf("Marks you entered are:");
for(int i=0;i<4;i++)
printf("%d ",marks[i]);
return 0;
}
What I have tried:
The program did not give any output neither did it show any error
推荐答案
你宣布
int * marks;
你没有为它分配任何内存。 &NBSP;你有点幸运,你的应用程序不只是崩溃。
类似
marks =(int *)malloc(4 * sizeof (int));
You declared
int *marks;
You didn't allocate any memory for it. You're somewhat fortunate your application didn't just crash.
Something like
marks = (int *)malloc(4*sizeof(int));
为什么编译器没有显示*标记被用作标记[I]的错误?
Why doesn't the compiler show an error for *marks being used as marks[I]?
因为语言定义说的是数组的名称是指向第一个元素的指针。所以标记
同时是一个指针和一个数组;使用方式可以通过声明来实现。
Because the language definition says that the name of an array is a pointer to the first element. So marks
is both a pointer and an array t the same time; the usage is available either way you declare it.
int arr[10];
int i1 = arr[0];
int i2 = *arr;
全部有效,原样:
Is all valid, as is:
int *arr = (int*) malloc(40);
int i1 = arr[0];
int i2 = *arr;
这篇关于为什么编译器没有显示*标记被用作标记[I]的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!