当我编译这个简单的C代码时它很好但是在取消注释该行后它显示了分段错误.我不知道这有什么问题.请帮忙.
#include<stdio.h>
int main()
{
int arr[10002][10002];
int color[10002];
int neigh;
// scanf("%d",&neigh);
return 0;
}
Run Code Online (Sandbox Code Playgroud) C函数malloc()是在stdlib.h下定义的.如果我们不包含这个文件应该给出错误,但是这个代码可以正常工作并带一点警告.我的问题是如果malloc()在没有这个头文件的情况下工作,那么为什么要包含它呢?请清楚我的概念.
# include <stdio.h>
int main()
{
int a, b, *p;
p = (int*)malloc(sizeof(int)*5);
for(a=0;a<5;a++)p[a]=a*9;
for(b=0;b<5;b++)printf("%d ",p[b]);
}
Run Code Online (Sandbox Code Playgroud) 在C运算符优先表指出的更高的优先级().
码:
# include <stdio.h>
int main()
{
int temp=2;
(temp += 23)++; //Statement 1
++(temp += 23); //Statement 2
printf("%d",temp);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,虽然括号中的优先级高于语句2中的预先修复运算符,但为什么会出现错误.在声明1中,两者具有相同的优先级,但评估顺序是从左到右.还是一样的错误.第三个疑问:operator + =优先级低得多,那么为什么它会导致错误.
error: lvalue required as increment operand
Run Code Online (Sandbox Code Playgroud) 这里给出的代码在由g ++编译时运行正常但在使用gcc进行编译时出错.显然,这对于C++是正确的,但不适用于C.请帮我纠正C的语法.
# include <stdio.h>
typedef struct demo
{
int arr[20], i;
void setvalue(int num)
{for(i=0;i<20;i++)arr[i]=num;}
void printvalue()
{for(i=0;i<20;i++)printf("%d ",arr[i]);}
} example;
int main()
{
example e;
e.setvalue(100);
e.printvalue();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误日志:
stov.c:7:2: error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘{’ token
stov.c: In function ‘main’:
stov.c:18:3: error: ‘example’ has no member named ‘setvalue’
stov.c:19:3: error: ‘example’ has no member named ‘printvalue’
Run Code Online (Sandbox Code Playgroud) #include<stdio.h>
int * fun(int a1,int b)
{
int a[2];
a[0]=a1;
a[1]=b;
return a;
}
int main()
{
int *r=fun(3,5);
printf("%d\n",*r);
printf("%d\n",*r);
}
Run Code Online (Sandbox Code Playgroud)
运行代码后输出:
3
-1073855580
Run Code Online (Sandbox Code Playgroud)
我知道a [2]对于fun()来说是局部的,但是为什么值会改变相同的指针?